u
This commit is contained in:
@@ -31,11 +31,17 @@ export const logout = createAsyncThunk('auth/logout', async () => {
|
||||
window.history.replaceState(null, '', '/');
|
||||
});
|
||||
|
||||
function mergeUser(state, incoming) {
|
||||
if (!incoming) return state.user;
|
||||
const selectedCompanyId =
|
||||
incoming.selectedCompanyId ?? state.user?.selectedCompanyId ?? null;
|
||||
return { ...state.user, ...incoming, selectedCompanyId };
|
||||
}
|
||||
|
||||
const initialState = {
|
||||
user: null,
|
||||
googleClientId: null,
|
||||
status: 'loading',
|
||||
companyReady: false,
|
||||
};
|
||||
|
||||
const authSlice = createSlice({
|
||||
@@ -46,40 +52,47 @@ const authSlice = createSlice({
|
||||
builder
|
||||
.addCase(bootstrapSession.fulfilled, (state, action) => {
|
||||
const session = action.payload;
|
||||
state.user = session.user ?? null;
|
||||
state.googleClientId = session.googleClientId ?? null;
|
||||
state.status = session.user ? 'authed' : 'anon';
|
||||
state.companyReady = session.user?.selectedCompanyId != null;
|
||||
state.googleClientId = session.googleClientId ?? state.googleClientId;
|
||||
if (!session.user) {
|
||||
if (state.status !== 'authed') {
|
||||
state.user = null;
|
||||
state.status = 'anon';
|
||||
}
|
||||
return;
|
||||
}
|
||||
state.user = mergeUser(state, session.user);
|
||||
state.status = 'authed';
|
||||
})
|
||||
.addCase(bootstrapSession.rejected, (state) => {
|
||||
state.user = null;
|
||||
state.status = 'anon';
|
||||
state.companyReady = false;
|
||||
if (state.status === 'loading') {
|
||||
state.user = null;
|
||||
state.status = 'anon';
|
||||
}
|
||||
})
|
||||
.addCase(loginWithGoogle.fulfilled, (state, action) => {
|
||||
const session = action.payload;
|
||||
state.user = session.user;
|
||||
state.googleClientId = session.googleClientId ?? state.googleClientId;
|
||||
state.status = 'authed';
|
||||
state.companyReady = session.user?.selectedCompanyId != null;
|
||||
})
|
||||
.addCase(loginWithGoogle.rejected, (state) => {
|
||||
state.user = null;
|
||||
state.status = 'anon';
|
||||
state.companyReady = false;
|
||||
})
|
||||
.addCase(selectCompany.fulfilled, (state, action) => {
|
||||
state.user = { ...state.user, ...action.payload };
|
||||
state.companyReady = true;
|
||||
state.user = mergeUser(state, action.payload);
|
||||
state.status = 'authed';
|
||||
})
|
||||
.addCase(createCompany.fulfilled, (state, action) => {
|
||||
state.user = { ...state.user, ...action.payload };
|
||||
state.companyReady = true;
|
||||
state.user = mergeUser(state, action.payload);
|
||||
state.status = 'authed';
|
||||
})
|
||||
.addCase(createCompany.rejected, (state, action) => {
|
||||
console.error('[redux] createCompany.rejected', action.error?.message, action.payload);
|
||||
})
|
||||
.addCase(logout.fulfilled, (state) => {
|
||||
state.user = null;
|
||||
state.status = 'anon';
|
||||
state.companyReady = false;
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -88,6 +101,6 @@ export const selectAuthStatus = (state) => state.auth.status;
|
||||
export const selectAuthUser = (state) => state.auth.user;
|
||||
export const selectGoogleClientId = (state) => state.auth.googleClientId;
|
||||
export const selectNeedsCompanySetup = (state) =>
|
||||
state.auth.status === 'authed' && !state.auth.companyReady;
|
||||
state.auth.status === 'authed' && state.auth.user?.selectedCompanyId == null;
|
||||
|
||||
export default authSlice.reducer;
|
||||
|
||||
Reference in New Issue
Block a user