chukwuyem

react

Oct 30th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { createContext, useState } from "react";
  2. import axios from "axios";
  3.  
  4. export const ApplicationContext = createContext();
  5.  
  6. const ApplicationContextProvider = (props) => {
  7.   const [globalstate, setGlobalstate] = useState({
  8.     error: null,
  9.     token: null,
  10.     IsAuth: false,
  11.   });
  12.  
  13.   //   const [token, setToken] = useState(null);
  14.  
  15.   //   const [error, setError] = useState(null)
  16.  
  17.   const LoginUsers = (email, password) => {
  18.     //     axios
  19.     //       .post("http://127.0.0.1:8000/token", {
  20.     //         username: email,
  21.     //         password: password,
  22.     //       })
  23.     //       .then((response) => {
  24.     //         console.log(response.data);
  25.     //         //const expirationDate = new
  26.     //       })
  27.     //       .catch((error) => {
  28.     //         console.log(error);
  29.     //       });
  30.     //
  31.  
  32.     fetch(
  33.       `http://127.0.0.1:8000/token?username=${email}&password=${password}`,
  34.       {
  35.         method: "POST",
  36.         headers: {
  37.           "Content-Type": "application/x-www-form-urlencoded",
  38.           accept: "application/json",
  39.           //"sec-fetch-site": "same-origin",
  40.           "X-CSRFToken": "csrftoken",
  41.         },
  42.         //body: JSON.stringify({ username: email, password: password }),
  43.       }
  44.     )
  45.       .then((response) => {
  46.         console.log(response);
  47.       })
  48.       .catch((error) => {
  49.         console.log(error);
  50.       });
  51.   };
  52.  
  53.   return (
  54.     <ApplicationContext.Provider value={{ globalstate, LoginUsers }}>
  55.       {props.children}
  56.     </ApplicationContext.Provider>
  57.   );
  58. };
  59.  
  60. export default ApplicationContextProvider;
  61.  
Add Comment
Please, Sign In to add comment