forked from extern/bruno
64 lines
1.4 KiB
JavaScript
64 lines
1.4 KiB
JavaScript
import React, { useEffect, useReducer } from 'react';
|
|
import { useRouter } from 'next/router';
|
|
import IdentityApi from 'api/identity';
|
|
import reducer from './reducer';
|
|
|
|
const AuthContext = React.createContext();
|
|
|
|
const initialState = {
|
|
isLoading: true,
|
|
lastStateTransition: null,
|
|
currentUser: null
|
|
};
|
|
|
|
export const AuthProvider = props => {
|
|
const router = useRouter();
|
|
const [state, dispatch] = useReducer(reducer, initialState);
|
|
|
|
useEffect(() => {
|
|
IdentityApi
|
|
.whoami()
|
|
.then((response) => {
|
|
let data = response.data;
|
|
dispatch({
|
|
type: 'WHOAMI_SUCCESS',
|
|
user : {
|
|
id: data.id,
|
|
name: data.name,
|
|
username: data.username
|
|
}
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
dispatch({
|
|
type: 'WHOAMI_ERROR',
|
|
error: error
|
|
});
|
|
});
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if(state.lastStateTransition === 'LOGIN_SUCCESS') {
|
|
router.push('/home');
|
|
}
|
|
if(state.lastStateTransition === 'WHOAMI_ERROR') {
|
|
// Todo: decide action
|
|
// router.push('/login');
|
|
}
|
|
}, [state]);
|
|
|
|
return <AuthContext.Provider value={[state, dispatch]} {...props} />;
|
|
};
|
|
|
|
export const useAuth = () => {
|
|
const context = React.useContext(AuthContext);
|
|
|
|
if (context === undefined) {
|
|
throw new Error(`useAuth must be used within a AuthProvider`);
|
|
}
|
|
|
|
return context;
|
|
};
|
|
|
|
export default AuthProvider;
|