chukwuyem

Authfun

Oct 28th, 2020 (edited)
66
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.  
  5. export const AuthContext = createContext();
  6.  
  7.  
  8. const AuthContextProvider = (props) => {
  9.  
  10.   const [token, setToken] = useState(null);
  11.  
  12.   const [error, setError] = useState(null)
  13.  
  14.  
  15. const LoginUsers =  (email, password) => {
  16.     const config = {
  17.       headers: {
  18.         'Content-Type' : 'application/json'
  19.       }
  20.     }
  21.  
  22.  
  23.     axios.post('http://127.0.0.1:8000/token/', {username:email, password:password}, config)
  24.         .then(response => {
  25.           setToken(response.data.key);
  26.           console.log(response.data)
  27.           //const expirationDate = new
  28.         })
  29.         .catch(error => {
  30.           setError(error.response)
  31.           console.log(error)
  32.         })
  33.     }
  34.  
  35.  
  36.  
  37.  
  38.     return(
  39.         <AuthContext.Provider value={{ error, token, LoginUsers}}>
  40.             {props.children}
  41.         </AuthContext.Provider>
  42.     );
  43. }
  44.  
  45. export default AuthContextProvider;
  46.  
RAW Paste Data