RC-4: move AuthContext to src/app/contexts and update it and userSlice
This commit is contained in:
@@ -4,7 +4,7 @@ import { useDispatch } from 'react-redux';
|
||||
import FuseSplashScreen from '@fuse/core/FuseSplashScreen';
|
||||
import { showMessage } from 'app/store/fuse/messageSlice';
|
||||
import { logoutUser, setUser } from 'app/store/userSlice';
|
||||
import jwtService from './services/jwtService';
|
||||
import { authService, firebase } from '../services';
|
||||
|
||||
const AuthContext = React.createContext();
|
||||
|
||||
@@ -14,44 +14,35 @@ function AuthProvider({ children }) {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
useEffect(() => {
|
||||
jwtService.on('onAutoLogin', () => {
|
||||
dispatch(showMessage({ message: 'Signing in with JWT' }));
|
||||
|
||||
/**
|
||||
* Sign in and retrieve user data with stored token
|
||||
*/
|
||||
jwtService
|
||||
.signInWithToken()
|
||||
.then((user) => {
|
||||
success(user, 'Signed in with JWT');
|
||||
})
|
||||
.catch((error) => {
|
||||
pass(error.message);
|
||||
});
|
||||
});
|
||||
|
||||
jwtService.on('onLogin', (user) => {
|
||||
success(user, 'Signed in');
|
||||
});
|
||||
|
||||
jwtService.on('onLogout', () => {
|
||||
authService.on('onLogout', () => {
|
||||
pass('Signed out');
|
||||
|
||||
dispatch(logoutUser());
|
||||
});
|
||||
|
||||
jwtService.on('onAutoLogout', (message) => {
|
||||
pass(message);
|
||||
authService.init(firebase.auth, firebase.db);
|
||||
|
||||
dispatch(logoutUser());
|
||||
authService.onAuthStateChanged((authUser) => {
|
||||
dispatch(showMessage({ message: 'Signing...' }));
|
||||
if (authUser) {
|
||||
authService
|
||||
.getUserData(authUser.uid)
|
||||
.then((user) => {
|
||||
if (user) {
|
||||
success(user, 'Signed in');
|
||||
} else {
|
||||
const { displayName, photoURL, email } = authUser;
|
||||
success({ role: 'user', data: { displayName, photoURL, email } }, 'Signed in');
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
pass(error.message);
|
||||
});
|
||||
} else {
|
||||
pass('Signed out');
|
||||
}
|
||||
});
|
||||
|
||||
jwtService.on('onNoAccessToken', () => {
|
||||
pass();
|
||||
});
|
||||
|
||||
jwtService.init();
|
||||
|
||||
function success(user, message) {
|
||||
if (message) {
|
||||
dispatch(showMessage({ message }));
|
||||
@@ -5,7 +5,7 @@ import _ from '@lodash';
|
||||
import { setInitialSettings } from 'app/store/fuse/settingsSlice';
|
||||
import { showMessage } from 'app/store/fuse/messageSlice';
|
||||
import settingsConfig from 'app/configs/settingsConfig';
|
||||
import jwtService from '../auth/services/jwtService';
|
||||
import { authService } from '../services';
|
||||
|
||||
export const setUser = createAsyncThunk('user/setUser', async (user, { dispatch, getState }) => {
|
||||
/*
|
||||
@@ -30,24 +30,6 @@ export const updateUserSettings = createAsyncThunk(
|
||||
}
|
||||
);
|
||||
|
||||
export const updateUserShortcuts = createAsyncThunk(
|
||||
'user/updateShortucts',
|
||||
async (shortcuts, { dispatch, getState }) => {
|
||||
const { user } = getState();
|
||||
const newUser = {
|
||||
...user,
|
||||
data: {
|
||||
...user.data,
|
||||
shortcuts,
|
||||
},
|
||||
};
|
||||
|
||||
dispatch(updateUserData(newUser));
|
||||
|
||||
return newUser;
|
||||
}
|
||||
);
|
||||
|
||||
export const logoutUser = () => async (dispatch, getState) => {
|
||||
const { user } = getState();
|
||||
|
||||
@@ -71,10 +53,10 @@ export const updateUserData = (user) => async (dispatch, getState) => {
|
||||
return;
|
||||
}
|
||||
|
||||
jwtService
|
||||
authService
|
||||
.updateUserData(user)
|
||||
.then(() => {
|
||||
dispatch(showMessage({ message: 'User data saved with api' }));
|
||||
dispatch(showMessage({ message: 'User data saved' }));
|
||||
})
|
||||
.catch((error) => {
|
||||
dispatch(showMessage({ message: error.message }));
|
||||
@@ -87,7 +69,6 @@ const initialState = {
|
||||
displayName: 'John Doe',
|
||||
photoURL: 'assets/images/avatars/brian-hughes.jpg',
|
||||
email: 'johndoe@withinpixels.com',
|
||||
shortcuts: ['apps.calendar', 'apps.mailbox', 'apps.contacts', 'apps.tasks'],
|
||||
},
|
||||
};
|
||||
|
||||
@@ -99,7 +80,6 @@ const userSlice = createSlice({
|
||||
},
|
||||
extraReducers: {
|
||||
[updateUserSettings.fulfilled]: (state, action) => action.payload,
|
||||
[updateUserShortcuts.fulfilled]: (state, action) => action.payload,
|
||||
[setUser.fulfilled]: (state, action) => action.payload,
|
||||
},
|
||||
});
|
||||
@@ -108,6 +88,4 @@ export const { userLoggedOut } = userSlice.actions;
|
||||
|
||||
export const selectUser = ({ user }) => user;
|
||||
|
||||
export const selectUserShortcuts = ({ user }) => user.data.shortcuts;
|
||||
|
||||
export default userSlice.reducer;
|
||||
|
||||
Reference in New Issue
Block a user