add auth
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { createFeature, createReducer, on } from '@ngrx/store';
|
||||
|
||||
import { readAuthSession } from '../auth/auth-session';
|
||||
import { GorodarActions } from './gorodar.actions';
|
||||
import type { ClaimState, GorodarState } from './gorodar.models';
|
||||
import { RESEND_SECONDS, sessionToAuth, type AuthState, type ClaimState, type GorodarState } from './gorodar.models';
|
||||
|
||||
const initialClaim: ClaimState = {
|
||||
landmarkId: null,
|
||||
@@ -10,11 +11,32 @@ const initialClaim: ClaimState = {
|
||||
step: 1,
|
||||
};
|
||||
|
||||
const blankAuth: AuthState = {
|
||||
email: '',
|
||||
accessToken: null,
|
||||
tokenType: 'Bearer',
|
||||
authenticated: false,
|
||||
step: 'email',
|
||||
resendSeconds: 0,
|
||||
pending: false,
|
||||
error: '',
|
||||
sheetVisible: false,
|
||||
intent: 'collection',
|
||||
pendingLandmarkId: null,
|
||||
codeRequestId: 0,
|
||||
};
|
||||
|
||||
const restoredAuth: AuthState = {
|
||||
...blankAuth,
|
||||
...sessionToAuth(readAuthSession()),
|
||||
};
|
||||
|
||||
export const initialState: GorodarState = {
|
||||
collectedIds: [],
|
||||
photos: {},
|
||||
collectedAt: {},
|
||||
claim: initialClaim,
|
||||
auth: restoredAuth,
|
||||
profile: {
|
||||
displayName: 'Иван Иванович',
|
||||
firstName: 'Иван',
|
||||
@@ -80,6 +102,164 @@ const gorodarReducer = createReducer(
|
||||
claim: initialClaim,
|
||||
}),
|
||||
),
|
||||
on(GorodarActions.authSheetOpened, (state, { intent, landmarkId }): GorodarState => {
|
||||
if (state.auth.authenticated) {
|
||||
return state;
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
auth: {
|
||||
...state.auth,
|
||||
sheetVisible: true,
|
||||
intent,
|
||||
pendingLandmarkId: landmarkId ?? null,
|
||||
error: '',
|
||||
},
|
||||
};
|
||||
}),
|
||||
on(
|
||||
GorodarActions.authSheetDismissed,
|
||||
(state): GorodarState => ({
|
||||
...state,
|
||||
auth: {
|
||||
...state.auth,
|
||||
sheetVisible: false,
|
||||
error: '',
|
||||
},
|
||||
}),
|
||||
),
|
||||
on(
|
||||
GorodarActions.authErrorCleared,
|
||||
(state): GorodarState => ({
|
||||
...state,
|
||||
auth: {
|
||||
...state.auth,
|
||||
error: '',
|
||||
},
|
||||
}),
|
||||
),
|
||||
on(
|
||||
GorodarActions.authEmailChangeRequested,
|
||||
(state): GorodarState => ({
|
||||
...state,
|
||||
auth: {
|
||||
...state.auth,
|
||||
step: 'email',
|
||||
error: '',
|
||||
resendSeconds: 0,
|
||||
},
|
||||
}),
|
||||
),
|
||||
on(
|
||||
GorodarActions.authCodeRequested,
|
||||
(state): GorodarState => ({
|
||||
...state,
|
||||
auth: {
|
||||
...state.auth,
|
||||
pending: true,
|
||||
error: '',
|
||||
},
|
||||
}),
|
||||
),
|
||||
on(
|
||||
GorodarActions.authCodeRequestSucceeded,
|
||||
(state, { email }): GorodarState => ({
|
||||
...state,
|
||||
auth: {
|
||||
...state.auth,
|
||||
email,
|
||||
step: 'code',
|
||||
pending: false,
|
||||
error: '',
|
||||
resendSeconds: RESEND_SECONDS,
|
||||
codeRequestId: state.auth.codeRequestId + 1,
|
||||
},
|
||||
profile: {
|
||||
...state.profile,
|
||||
email,
|
||||
},
|
||||
}),
|
||||
),
|
||||
on(
|
||||
GorodarActions.authCodeRequestFailed,
|
||||
(state, { error }): GorodarState => ({
|
||||
...state,
|
||||
auth: {
|
||||
...state.auth,
|
||||
pending: false,
|
||||
error,
|
||||
},
|
||||
}),
|
||||
),
|
||||
on(
|
||||
GorodarActions.authCodeConfirmed,
|
||||
(state): GorodarState => ({
|
||||
...state,
|
||||
auth: {
|
||||
...state.auth,
|
||||
pending: true,
|
||||
error: '',
|
||||
},
|
||||
}),
|
||||
),
|
||||
on(
|
||||
GorodarActions.authCodeConfirmSucceeded,
|
||||
(state, { email, accessToken, tokenType, refreshToken, expiresAt }): GorodarState => ({
|
||||
...state,
|
||||
auth: {
|
||||
...state.auth,
|
||||
email,
|
||||
accessToken,
|
||||
tokenType,
|
||||
refreshToken,
|
||||
expiresAt,
|
||||
authenticated: true,
|
||||
step: 'email',
|
||||
pending: false,
|
||||
error: '',
|
||||
resendSeconds: 0,
|
||||
sheetVisible: false,
|
||||
pendingLandmarkId: null,
|
||||
},
|
||||
profile: {
|
||||
...state.profile,
|
||||
email,
|
||||
},
|
||||
}),
|
||||
),
|
||||
on(
|
||||
GorodarActions.authCodeConfirmFailed,
|
||||
(state, { error }): GorodarState => ({
|
||||
...state,
|
||||
auth: {
|
||||
...state.auth,
|
||||
pending: false,
|
||||
error,
|
||||
},
|
||||
}),
|
||||
),
|
||||
on(
|
||||
GorodarActions.authResendTicked,
|
||||
(state, { seconds }): GorodarState => ({
|
||||
...state,
|
||||
auth: {
|
||||
...state.auth,
|
||||
resendSeconds: seconds,
|
||||
},
|
||||
}),
|
||||
),
|
||||
on(
|
||||
GorodarActions.logout,
|
||||
(state): GorodarState => ({
|
||||
...state,
|
||||
auth: blankAuth,
|
||||
profile: {
|
||||
...state.profile,
|
||||
email: '',
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const gorodarFeature = createFeature({
|
||||
|
||||
Reference in New Issue
Block a user