Skip to content

Commit

Permalink
feat(auth)!: migrate auth state to operation state (#2453)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: auth state structure and selectors have changed
  • Loading branch information
griest024 committed May 24, 2023
1 parent 229d4d0 commit 9afcabd
Show file tree
Hide file tree
Showing 29 changed files with 203 additions and 798 deletions.
2 changes: 1 addition & 1 deletion libs/auth/state/src/facades/auth/facade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { DaffStateError } from '@daffodil/core/state';

import { DaffAuthFacade } from './facade';

describe('DaffAuthFacade', () => {
describe('@daffodil/auth/state | DaffAuthFacade', () => {
let store: Store<any>;
let facade: DaffAuthFacade;
let authFactory: DaffAuthTokenFactory;
Expand Down
17 changes: 8 additions & 9 deletions libs/auth/state/src/facades/auth/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import {
} from '@ngrx/store';
import { Observable } from 'rxjs';

import { DaffAuthToken } from '@daffodil/auth';
import { DaffStateError } from '@daffodil/core/state';

import { DaffAuthFeatureState } from '../../reducers/public_api';
import { getDaffAuthSelectors } from '../../selectors/public_api';
import { DaffAuthStateRootSlice } from '../../reducers/public_api';
import { getAuthSelectors } from '../../selectors/auth/auth.selector';
import { DaffAuthFacadeInterface } from './facade.interface';

/**
Expand All @@ -24,15 +23,15 @@ export class DaffAuthFacade implements DaffAuthFacadeInterface {
errors$: Observable<DaffStateError[]>;
loggedIn$: Observable<boolean>;

constructor(private store: Store<DaffAuthFeatureState>) {
constructor(private store: Store<DaffAuthStateRootSlice>) {
const {
selectAuthLoading,
selectAuthErrors,
selectLoading,
selectErrors,
selectAuthLoggedIn,
} = getDaffAuthSelectors();
} = getAuthSelectors();

this.loading$ = this.store.pipe(select(selectAuthLoading));
this.errors$ = this.store.pipe(select(selectAuthErrors));
this.loading$ = this.store.pipe(select(selectLoading));
this.errors$ = this.store.pipe(select(selectErrors));
this.loggedIn$ = this.store.pipe(select(selectAuthLoggedIn));
}

Expand Down
16 changes: 8 additions & 8 deletions libs/auth/state/src/facades/login/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Observable } from 'rxjs';

import { DaffStateError } from '@daffodil/core/state';

import { DaffAuthFeatureState } from '../../reducers/public_api';
import { getDaffAuthSelectors } from '../../selectors/public_api';
import { DaffAuthStateRootSlice } from '../../reducers/public_api';
import { getDaffAuthLoginSelectors } from '../../selectors/login/login.selector';
import { DaffAuthLoginFacadeInterface } from './facade.interface';

/**
Expand All @@ -22,14 +22,14 @@ export class DaffAuthLoginFacade implements DaffAuthLoginFacadeInterface {
loading$: Observable<boolean>;
errors$: Observable<DaffStateError[]>;

constructor(private store: Store<DaffAuthFeatureState>) {
constructor(private store: Store<DaffAuthStateRootSlice>) {
const {
selectAuthLoginLoading,
selectAuthLoginErrors,
} = getDaffAuthSelectors();
selectLoading,
selectErrors,
} = getDaffAuthLoginSelectors();

this.loading$ = this.store.pipe(select(selectAuthLoginLoading));
this.errors$ = this.store.pipe(select(selectAuthLoginErrors));
this.loading$ = this.store.pipe(select(selectLoading));
this.errors$ = this.store.pipe(select(selectErrors));
}

dispatch(action: Action) {
Expand Down
16 changes: 8 additions & 8 deletions libs/auth/state/src/facades/register/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Observable } from 'rxjs';

import { DaffStateError } from '@daffodil/core/state';

import { DaffAuthFeatureState } from '../../reducers/public_api';
import { getDaffAuthSelectors } from '../../selectors/public_api';
import { DaffAuthStateRootSlice } from '../../reducers/public_api';
import { getDaffAuthRegisterSelectors } from '../../selectors/register/register.selector';
import { DaffAuthRegisterFacadeInterface } from './facade.interface';

/**
Expand All @@ -22,14 +22,14 @@ export class DaffAuthRegisterFacade implements DaffAuthRegisterFacadeInterface {
loading$: Observable<boolean>;
errors$: Observable<DaffStateError[]>;

constructor(private store: Store<DaffAuthFeatureState>) {
constructor(private store: Store<DaffAuthStateRootSlice>) {
const {
selectAuthRegisterLoading,
selectAuthRegisterErrors,
} = getDaffAuthSelectors();
selectLoading,
selectErrors,
} = getDaffAuthRegisterSelectors();

this.loading$ = this.store.pipe(select(selectAuthRegisterLoading));
this.errors$ = this.store.pipe(select(selectAuthRegisterErrors));
this.loading$ = this.store.pipe(select(selectLoading));
this.errors$ = this.store.pipe(select(selectErrors));
}

dispatch(action: Action) {
Expand Down
16 changes: 8 additions & 8 deletions libs/auth/state/src/facades/reset-password/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { Observable } from 'rxjs';
import { DaffAuthResetPasswordInfo } from '@daffodil/auth';
import { DaffStateError } from '@daffodil/core/state';

import { DaffAuthFeatureState } from '../../reducers/public_api';
import { getDaffAuthSelectors } from '../../selectors/public_api';
import { DaffAuthStateRootSlice } from '../../reducers/public_api';
import { getDaffAuthResetPasswordSelectors } from '../../selectors/reset-password/selector';
import { DaffAuthResetPasswordFacadeInterface } from './facade.interface';

/**
Expand All @@ -25,17 +25,17 @@ export class DaffAuthResetPasswordFacade implements DaffAuthResetPasswordFacadeI
loading$: Observable<boolean>;
errors$: Observable<DaffStateError[]>;

constructor(private store: Store<DaffAuthFeatureState>) {
constructor(private store: Store<DaffAuthStateRootSlice>) {
const {
selectAuthResetPasswordLoading,
selectAuthResetPasswordErrors,
selectLoading,
selectErrors,
selectAuthResetPasswordToken,
} = getDaffAuthSelectors();
} = getDaffAuthResetPasswordSelectors();

this.token$ = this.store.pipe(select(selectAuthResetPasswordToken));

this.loading$ = this.store.pipe(select(selectAuthResetPasswordLoading));
this.errors$ = this.store.pipe(select(selectAuthResetPasswordErrors));
this.loading$ = this.store.pipe(select(selectLoading));
this.errors$ = this.store.pipe(select(selectErrors));
}

dispatch(action: Action) {
Expand Down
5 changes: 3 additions & 2 deletions libs/auth/state/src/reducers/auth/auth-initial-state.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { daffOperationInitialState } from '@daffodil/core/state';

import { DaffAuthReducerState } from './auth-reducer-state.interface';

export const daffAuthInitialState: DaffAuthReducerState = {
loading: false,
errors: [],
...daffOperationInitialState,
loggedIn: false,
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { DaffStateError } from '@daffodil/core/state';
import { DaffOperationState } from '@daffodil/core/state';

export interface DaffAuthReducerState {
loading: boolean;
errors: DaffStateError[];
export interface DaffAuthReducerState extends DaffOperationState {
loggedIn: boolean;
}
77 changes: 33 additions & 44 deletions libs/auth/state/src/reducers/auth/auth.reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
DaffAuthGuardLogout,
DaffAuthRegisterSuccess,
} from '@daffodil/auth/state';
import { DaffStateError } from '@daffodil/core/state';
import {
DaffState,
DaffStateError,
} from '@daffodil/core/state';

import { daffAuthReducer as reducer } from './auth.reducer';

Expand Down Expand Up @@ -82,45 +85,31 @@ describe('@daffodil/auth/state | daffAuthReducer', () => {
});
});

describe('when AuthCheckAction is triggered', () => {
let result: DaffAuthReducerState;

beforeEach(() => {
const authCheckAction = new DaffAuthCheck();

result = reducer(initialState, authCheckAction);
});

it('sets loading state to true', () => {
expect(result.loading).toEqual(true);
});
});

describe('when AuthCheckSuccessAction is triggered', () => {
let result: DaffAuthReducerState;
let state: DaffAuthReducerState;

beforeEach(() => {
state = {
...initialState,
loading: true,
errors: [{ code: 'firstErrorCode', message: 'firstErrorMessage' }],
daffState: DaffState.Resolving,
daffErrors: [{ code: 'firstErrorCode', message: 'firstErrorMessage' }],
};

const authCheckSuccess = new DaffAuthCheckSuccess();
result = reducer(state, authCheckSuccess);
});

it('sets loading to false', () => {
expect(result.loading).toEqual(false);
it('sets loading to stable', () => {
expect(result.daffState).toEqual(DaffState.Stable);
});

it('sets loggedIn state to true', () => {
expect(result.loggedIn).toBeTrue();
});

it('resets errors', () => {
expect(result.errors).toEqual([]);
expect(result.daffErrors).toEqual([]);
});
});

Expand All @@ -135,21 +124,21 @@ describe('@daffodil/auth/state | daffAuthReducer', () => {
beforeEach(() => {
state = {
...initialState,
loading: true,
errors: [{ code: 'firstErrorCode', message: 'firstErrorMessage' }],
daffState: DaffState.Resolving,
daffErrors: [{ code: 'firstErrorCode', message: 'firstErrorMessage' }],
};

const authCheckFailure = new DaffAuthCheckFailure(error);

result = reducer(state, authCheckFailure);
});

it('sets loading to false', () => {
expect(result.loading).toEqual(false);
it('sets loading to stable', () => {
expect(result.daffState).toEqual(DaffState.Stable);
});

it('adds an error to state.errors', () => {
expect(result.errors).toEqual([error]);
it('adds an error to state.daffErrors', () => {
expect(result.daffErrors).toEqual([error]);
});

it('sets loggedIn state to false', () => {
Expand All @@ -168,21 +157,21 @@ describe('@daffodil/auth/state | daffAuthReducer', () => {
beforeEach(() => {
state = {
...initialState,
loading: true,
errors: [{ code: 'firstErrorCode', message: 'firstErrorMessage' }],
daffState: DaffState.Resolving,
daffErrors: [{ code: 'firstErrorCode', message: 'firstErrorMessage' }],
};

const authCheckFailure = new DaffAuthGuardLogout(error);

result = reducer(state, authCheckFailure);
});

it('sets loading to false', () => {
expect(result.loading).toEqual(false);
it('sets loading to stable', () => {
expect(result.daffState).toEqual(DaffState.Stable);
});

it('adds an error to state.errors', () => {
expect(result.errors).toEqual([error]);
it('adds an error to state.daffErrors', () => {
expect(result.daffErrors).toEqual([error]);
});

it('sets loggedIn state to false', () => {
Expand All @@ -201,21 +190,21 @@ describe('@daffodil/auth/state | daffAuthReducer', () => {
beforeEach(() => {
state = {
...initialState,
loading: true,
errors: [{ code: 'firstErrorCode', message: 'firstErrorMessage' }],
daffState: DaffState.Resolving,
daffErrors: [{ code: 'firstErrorCode', message: 'firstErrorMessage' }],
};

const authServerSide = new DaffAuthServerSide(error);

result = reducer(state, authServerSide);
});

it('sets loading to false', () => {
expect(result.loading).toEqual(false);
it('sets loading to stable', () => {
expect(result.daffState).toEqual(DaffState.Stable);
});

it('adds an error to state.errors', () => {
expect(result.errors).toEqual([error]);
it('adds an error to state.daffErrors', () => {
expect(result.daffErrors).toEqual([error]);
});
});

Expand All @@ -230,21 +219,21 @@ describe('@daffodil/auth/state | daffAuthReducer', () => {
beforeEach(() => {
state = {
...initialState,
loading: true,
errors: [{ code: 'firstErrorCode', message: 'firstErrorMessage' }],
daffState: DaffState.Resolving,
daffErrors: [{ code: 'firstErrorCode', message: 'firstErrorMessage' }],
};

const authStorageFailure = new DaffAuthStorageFailure(error);

result = reducer(state, authStorageFailure);
});

it('sets loading to false', () => {
expect(result.loading).toEqual(false);
it('sets loading to stable', () => {
expect(result.daffState).toEqual(DaffState.Stable);
});

it('adds an error to state.errors', () => {
expect(result.errors).toEqual([error]);
it('adds an error to state.daffErrors', () => {
expect(result.daffErrors).toEqual([error]);
});
});
});
25 changes: 10 additions & 15 deletions libs/auth/state/src/reducers/auth/auth.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
daffCompleteOperation,
daffOperationFailed,
daffStartResolution,
} from '@daffodil/core/state';

import {
DaffAuthActionTypes,
DaffAuthActions,
Expand All @@ -17,35 +23,24 @@ export function daffAuthReducer(
): DaffAuthReducerState {
switch (action.type) {
case DaffAuthActionTypes.AuthCheckAction:
return {
...state,
loading: true,
};
return daffStartResolution(state);

case DaffAuthActionTypes.AuthCheckSuccessAction:
return {
...state,
...daffCompleteOperation(state),
loggedIn: true,
loading: false,
errors: [],
};

case DaffAuthActionTypes.AuthCheckFailureAction:
case DaffAuthActionTypes.AuthGuardLogoutAction:
return {
...state,
...daffOperationFailed([action.errorMessage], state),
loggedIn: false,
loading: false,
errors: [action.errorMessage],
};

case DaffAuthActionTypes.AuthServerSideAction:
case DaffAuthActionTypes.AuthStorageFailureAction:
return {
...state,
loading: false,
errors: [action.errorMessage],
};
return daffOperationFailed([action.errorMessage], state);

case DaffAuthLoginActionTypes.LoginSuccessAction:
return {
Expand Down
7 changes: 3 additions & 4 deletions libs/auth/state/src/reducers/login/login-initial-state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { daffOperationInitialState } from '@daffodil/core/state';

import { DaffAuthLoginReducerState } from './login-reducer-state.interface';

export const daffAuthLoginInitialState: DaffAuthLoginReducerState = {
loading: false,
errors: [],
};
export const daffAuthLoginInitialState: DaffAuthLoginReducerState = daffOperationInitialState;
Loading

0 comments on commit 9afcabd

Please sign in to comment.