-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseAuth.js
56 lines (47 loc) · 1.69 KB
/
useAuth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React, { useEffect, useState, useContext, createContext } from 'react';
import { setAccessToken } from './accessToken';
const AuthContext = createContext({})
// Intended to wrap something high level, probably <App />, so children can access auth object
export const AuthProvider = ({ children }) => {
const auth = useProvideAuth();
return <AuthContext.Provider value={auth}>{ children }</AuthContext.Provider>
}
// Hook intended for child components to access the auth object and re-render when it changes
export const useAuth = () => {
return useContext(AuthContext)
}
const useProvideAuth = () => {
// loading defaults to true; intended to run when <App /> mounts
const [loading, setLoading] = useState(true);
// const [user, setUser] = useState({});
const signIn = () => {};
const signOut = () => {};
const signUp = () => {};
const sendPasswordResetEmail = () => {};
const confirmPasswordReset = () => {};
useEffect(() => {
// intended to be called when <App /> mounts, normally init time.
// If a valid refresh_token is in the cookie, we fetch and store an access_token
fetch("http://localhost:4000/access_token", {
method: "POST",
credentials: "include"
}).then(async response => {
const { access_token } = await response.json();
setAccessToken(access_token); // store in browser/page memory only, not persisted to local storage
// setUser({email})
}).catch(err => {
console.error('unable to connect to auth server', err)
}).finally(() => {
setLoading(false);
});
}, []);
return {
loading,
// user,
signIn,
signOut,
signUp,
sendPasswordResetEmail,
confirmPasswordReset
}
}