@daffodil/auth 0.0.0-PLACEHOLDER
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +51 -0
- package/README.md +14 -0
- package/driver/in-memory/ng-package.json +8 -0
- package/driver/in-memory/package.json +3 -0
- package/driver/in-memory/src/backend/auth.service.integration.spec.ts +125 -0
- package/driver/in-memory/src/backend/auth.service.ts +131 -0
- package/driver/in-memory/src/backend/auth.service.unit.spec.ts +119 -0
- package/driver/in-memory/src/drivers/auth/auth.service.spec.ts +57 -0
- package/driver/in-memory/src/drivers/auth/auth.service.ts +24 -0
- package/driver/in-memory/src/drivers/auth-driver.module.ts +48 -0
- package/driver/in-memory/src/drivers/login/login.service.spec.ts +93 -0
- package/driver/in-memory/src/drivers/login/login.service.ts +36 -0
- package/driver/in-memory/src/drivers/public_api.ts +4 -0
- package/driver/in-memory/src/drivers/register/register.service.spec.ts +113 -0
- package/driver/in-memory/src/drivers/register/register.service.ts +38 -0
- package/driver/in-memory/src/drivers/reset-password/service.spec.ts +149 -0
- package/driver/in-memory/src/drivers/reset-password/service.ts +42 -0
- package/driver/in-memory/src/index.ts +1 -0
- package/driver/in-memory/src/models/db-customer.type.ts +8 -0
- package/driver/in-memory/src/public_api.ts +3 -0
- package/driver/magento/ng-package.json +8 -0
- package/driver/magento/package.json +3 -0
- package/driver/magento/src/apollo-bearer-token-link-generator.spec.ts +107 -0
- package/driver/magento/src/apollo-bearer-token-link-generator.ts +39 -0
- package/driver/magento/src/auth-driver.module.ts +48 -0
- package/driver/magento/src/auth.service.spec.ts +154 -0
- package/driver/magento/src/auth.service.ts +40 -0
- package/driver/magento/src/errors/codes.ts +5 -0
- package/driver/magento/src/errors/map.ts +14 -0
- package/driver/magento/src/errors/transform.spec.ts +41 -0
- package/driver/magento/src/errors/transform.ts +6 -0
- package/driver/magento/src/index.ts +1 -0
- package/driver/magento/src/login.service.spec.ts +261 -0
- package/driver/magento/src/login.service.ts +65 -0
- package/driver/magento/src/models/public_api.ts +4 -0
- package/driver/magento/src/models/response/auth.ts +3 -0
- package/driver/magento/src/models/response/register.type.ts +7 -0
- package/driver/magento/src/models/response/reset-password.type.ts +3 -0
- package/driver/magento/src/models/response/send-reset-email.type.ts +3 -0
- package/driver/magento/src/public_api.ts +12 -0
- package/driver/magento/src/queries/check-token.ts +10 -0
- package/driver/magento/src/queries/create-customer.ts +24 -0
- package/driver/magento/src/queries/generate-token.ts +13 -0
- package/driver/magento/src/queries/public_api.ts +8 -0
- package/driver/magento/src/queries/reset-password.ts +12 -0
- package/driver/magento/src/queries/responses/check-token-response.ts +5 -0
- package/driver/magento/src/queries/responses/generate-token-response.ts +5 -0
- package/driver/magento/src/queries/responses/public_api.ts +3 -0
- package/driver/magento/src/queries/responses/revoke-customer-token-response.ts +5 -0
- package/driver/magento/src/queries/revoke-customer-token.ts +10 -0
- package/driver/magento/src/queries/send-reset-email.ts +10 -0
- package/driver/magento/src/register.service.spec.ts +189 -0
- package/driver/magento/src/register.service.ts +65 -0
- package/driver/magento/src/reset-password.service.spec.ts +342 -0
- package/driver/magento/src/reset-password.service.ts +78 -0
- package/driver/magento/src/transforms/auth-transformer.service.spec.ts +42 -0
- package/driver/magento/src/transforms/auth-transformer.service.ts +25 -0
- package/driver/magento/src/validators/check-token.spec.ts +40 -0
- package/driver/magento/src/validators/check-token.ts +13 -0
- package/driver/magento/src/validators/generate-token.spec.ts +40 -0
- package/driver/magento/src/validators/generate-token.ts +13 -0
- package/driver/magento/src/validators/public_api.ts +5 -0
- package/driver/magento/src/validators/reset-password.spec.ts +38 -0
- package/driver/magento/src/validators/reset-password.ts +13 -0
- package/driver/magento/src/validators/revoke-token.spec.ts +40 -0
- package/driver/magento/src/validators/revoke-token.ts +13 -0
- package/driver/magento/src/validators/send-reset-email.spec.ts +38 -0
- package/driver/magento/src/validators/send-reset-email.ts +13 -0
- package/driver/ng-package.json +8 -0
- package/driver/package.json +3 -0
- package/driver/src/errors/authentication-failed.ts +14 -0
- package/driver/src/errors/codes.enum.ts +6 -0
- package/driver/src/errors/invalid-api-response.ts +14 -0
- package/driver/src/errors/map.ts +15 -0
- package/driver/src/errors/public_api.ts +6 -0
- package/driver/src/errors/registration-failed.ts +17 -0
- package/driver/src/errors/unauthorized.ts +14 -0
- package/driver/src/index.ts +1 -0
- package/driver/src/interfaces/auth-service.interface.ts +11 -0
- package/driver/src/interfaces/login-service.interface.ts +26 -0
- package/driver/src/interfaces/public_api.ts +4 -0
- package/driver/src/interfaces/register-service.interface.ts +26 -0
- package/driver/src/interfaces/reset-password-service.interface.ts +26 -0
- package/driver/src/public_api.ts +3 -0
- package/driver/src/services/auth-check.service.spec.ts +81 -0
- package/driver/src/services/auth-check.service.ts +50 -0
- package/driver/src/services/public_api.ts +1 -0
- package/driver/testing/ng-package.json +8 -0
- package/driver/testing/package.json +3 -0
- package/driver/testing/src/drivers/auth/auth.service.spec.ts +40 -0
- package/driver/testing/src/drivers/auth/auth.service.ts +19 -0
- package/driver/testing/src/drivers/auth-driver.module.ts +48 -0
- package/driver/testing/src/drivers/login/login.service.spec.ts +59 -0
- package/driver/testing/src/drivers/login/login.service.ts +30 -0
- package/driver/testing/src/drivers/public_api.ts +4 -0
- package/driver/testing/src/drivers/register/register.service.spec.ts +49 -0
- package/driver/testing/src/drivers/register/register.service.ts +32 -0
- package/driver/testing/src/drivers/reset-password/service.spec.ts +48 -0
- package/driver/testing/src/drivers/reset-password/service.ts +33 -0
- package/driver/testing/src/index.ts +1 -0
- package/driver/testing/src/public_api.ts +1 -0
- package/karma.conf.js +10 -0
- package/ng-package.json +8 -0
- package/ng-package.prod.json +7 -0
- package/package.json +39 -0
- package/routing/ng-package.json +8 -0
- package/routing/package.json +3 -0
- package/routing/src/config/default.ts +9 -0
- package/routing/src/config/interface.ts +33 -0
- package/routing/src/config/public_api.ts +6 -0
- package/routing/src/config/token.ts +24 -0
- package/routing/src/effects/redirect.effects.spec.ts +236 -0
- package/routing/src/effects/redirect.effects.ts +85 -0
- package/routing/src/guards/guest-only-guard-redirect.token.ts +3 -0
- package/routing/src/guards/guest-only.guard.spec.ts +127 -0
- package/routing/src/guards/guest-only.guard.ts +69 -0
- package/routing/src/guards/member-only-guard-redirect.token.ts +3 -0
- package/routing/src/guards/member-only.guard.spec.ts +127 -0
- package/routing/src/guards/member-only.guard.ts +70 -0
- package/routing/src/guards/public_api.ts +6 -0
- package/routing/src/guards/reset-password.guard.spec.ts +113 -0
- package/routing/src/guards/reset-password.guard.ts +44 -0
- package/routing/src/index.ts +1 -0
- package/routing/src/module.ts +34 -0
- package/routing/src/public_api.ts +5 -0
- package/routing/src/redirect.module.ts +13 -0
- package/src/errors/codes.enum.ts +3 -0
- package/src/errors/map.ts +9 -0
- package/src/errors/missing-token.ts +18 -0
- package/src/errors/public_api.ts +3 -0
- package/src/index.ts +1 -0
- package/src/injection-tokens/error-matcher.token.ts +12 -0
- package/src/injection-tokens/public_api.ts +1 -0
- package/src/models/account-registration.ts +18 -0
- package/src/models/auth-token.ts +3 -0
- package/src/models/login-info.ts +4 -0
- package/src/models/public_api.ts +4 -0
- package/src/models/reset-password-info.type.ts +14 -0
- package/src/public_api.ts +5 -0
- package/src/storage/auth-storage.service.spec.ts +43 -0
- package/src/storage/auth-storage.service.ts +30 -0
- package/state/ng-package.json +8 -0
- package/state/package.json +3 -0
- package/state/src/actions/auth.actions.ts +75 -0
- package/state/src/actions/login.actions.ts +85 -0
- package/state/src/actions/public_api.ts +4 -0
- package/state/src/actions/register.actions.ts +50 -0
- package/state/src/actions/reset-password.actions.ts +92 -0
- package/state/src/auth-state.module.ts +45 -0
- package/state/src/config/default.ts +7 -0
- package/state/src/config/public_api.ts +6 -0
- package/state/src/config/token.ts +14 -0
- package/state/src/config/type.ts +7 -0
- package/state/src/effects/auth.effects.spec.ts +232 -0
- package/state/src/effects/auth.effects.ts +125 -0
- package/state/src/effects/login.effects.spec.ts +258 -0
- package/state/src/effects/login.effects.ts +126 -0
- package/state/src/effects/register.effects.spec.ts +206 -0
- package/state/src/effects/register.effects.ts +93 -0
- package/state/src/effects/reset-password.effects.spec.ts +238 -0
- package/state/src/effects/reset-password.effects.ts +110 -0
- package/state/src/facades/auth/facade.interface.ts +18 -0
- package/state/src/facades/auth/facade.spec.ts +135 -0
- package/state/src/facades/auth/facade.ts +42 -0
- package/state/src/facades/auth/public_api.ts +2 -0
- package/state/src/facades/login/facade.interface.ts +14 -0
- package/state/src/facades/login/facade.spec.ts +111 -0
- package/state/src/facades/login/facade.ts +38 -0
- package/state/src/facades/login/public_api.ts +2 -0
- package/state/src/facades/public_api.ts +4 -0
- package/state/src/facades/register/facade.interface.ts +14 -0
- package/state/src/facades/register/facade.spec.ts +103 -0
- package/state/src/facades/register/facade.ts +38 -0
- package/state/src/facades/register/public_api.ts +2 -0
- package/state/src/facades/reset-password/facade.interface.ts +17 -0
- package/state/src/facades/reset-password/facade.spec.ts +112 -0
- package/state/src/facades/reset-password/facade.ts +44 -0
- package/state/src/facades/reset-password/public_api.ts +2 -0
- package/state/src/index.ts +1 -0
- package/state/src/public_api.ts +8 -0
- package/state/src/reducers/auth/auth-initial-state.ts +7 -0
- package/state/src/reducers/auth/auth-reducer-state.interface.ts +7 -0
- package/state/src/reducers/auth/auth.reducer.spec.ts +250 -0
- package/state/src/reducers/auth/auth.reducer.ts +72 -0
- package/state/src/reducers/auth/public_api.ts +3 -0
- package/state/src/reducers/auth-feature-state.interface.ts +16 -0
- package/state/src/reducers/auth-reducers.ts +14 -0
- package/state/src/reducers/auth-store-feature-key.ts +1 -0
- package/state/src/reducers/login/login-initial-state.ts +6 -0
- package/state/src/reducers/login/login-reducer-state.interface.ts +6 -0
- package/state/src/reducers/login/login.reducer.spec.ts +190 -0
- package/state/src/reducers/login/login.reducer.ts +47 -0
- package/state/src/reducers/login/public_api.ts +3 -0
- package/state/src/reducers/public_api.ts +11 -0
- package/state/src/reducers/register/public_api.ts +3 -0
- package/state/src/reducers/register/register-initial-state.ts +6 -0
- package/state/src/reducers/register/register-reducer-state.interface.ts +6 -0
- package/state/src/reducers/register/register.reducer.spec.ts +111 -0
- package/state/src/reducers/register/register.reducer.ts +40 -0
- package/state/src/reducers/reset-password/initial-state.ts +7 -0
- package/state/src/reducers/reset-password/public_api.ts +3 -0
- package/state/src/reducers/reset-password/reducer.spec.ts +196 -0
- package/state/src/reducers/reset-password/reducer.ts +49 -0
- package/state/src/reducers/reset-password/state.interface.ts +8 -0
- package/state/src/selectors/auth/auth.selector.spec.ts +87 -0
- package/state/src/selectors/auth/auth.selector.ts +51 -0
- package/state/src/selectors/auth-all.selector.ts +40 -0
- package/state/src/selectors/auth-feature.selector.ts +18 -0
- package/state/src/selectors/login/login.selector.spec.ts +88 -0
- package/state/src/selectors/login/login.selector.ts +44 -0
- package/state/src/selectors/public_api.ts +1 -0
- package/state/src/selectors/register/register.selector.spec.ts +75 -0
- package/state/src/selectors/register/register.selector.ts +45 -0
- package/state/src/selectors/reset-password/selector.spec.ts +90 -0
- package/state/src/selectors/reset-password/selector.ts +52 -0
- package/state/testing/ng-package.json +8 -0
- package/state/testing/package.json +3 -0
- package/state/testing/src/auth-testing.module.ts +23 -0
- package/state/testing/src/index.ts +1 -0
- package/state/testing/src/mock-auth-facade.ts +18 -0
- package/state/testing/src/mock-login-facade.ts +17 -0
- package/state/testing/src/mock-register-facade.ts +17 -0
- package/state/testing/src/mock-reset-password-facade.ts +18 -0
- package/state/testing/src/public_api.ts +5 -0
- package/test.ts +24 -0
- package/testing/ng-package.json +8 -0
- package/testing/package.json +3 -0
- package/testing/src/factories/account-registration.factory.spec.ts +37 -0
- package/testing/src/factories/account-registration.factory.ts +22 -0
- package/testing/src/factories/auth-token.factory.ts +18 -0
- package/testing/src/factories/auth.factory.spec.ts +45 -0
- package/testing/src/factories/public_api.ts +3 -0
- package/testing/src/factories/reset-password-info.factory.spec.ts +35 -0
- package/testing/src/factories/reset-password-info.factory.ts +20 -0
- package/testing/src/index.ts +1 -0
- package/testing/src/public_api.ts +1 -0
- package/tsconfig.json +38 -0
- package/tsconfig.lib.json +28 -0
- package/tsconfig.lib.prod.json +9 -0
- package/tsconfig.spec.json +17 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
import { provideMockActions } from '@ngrx/effects/testing';
|
|
3
|
+
import {
|
|
4
|
+
hot,
|
|
5
|
+
cold,
|
|
6
|
+
} from 'jasmine-marbles';
|
|
7
|
+
import {
|
|
8
|
+
Observable ,
|
|
9
|
+
of,
|
|
10
|
+
} from 'rxjs';
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
DaffLoginInfo,
|
|
14
|
+
DaffAuthToken,
|
|
15
|
+
DaffAccountRegistration,
|
|
16
|
+
DaffAuthStorageService,
|
|
17
|
+
} from '@daffodil/auth';
|
|
18
|
+
import {
|
|
19
|
+
DaffLoginDriver,
|
|
20
|
+
DaffLoginServiceInterface,
|
|
21
|
+
DaffAuthenticationFailedError,
|
|
22
|
+
DaffAuthInvalidAPIResponseError,
|
|
23
|
+
} from '@daffodil/auth/driver';
|
|
24
|
+
import {
|
|
25
|
+
DaffAuthStorageFailure,
|
|
26
|
+
DaffAuthLogin,
|
|
27
|
+
DaffAuthLoginSuccess,
|
|
28
|
+
DaffAuthLoginFailure,
|
|
29
|
+
DaffAuthLogout,
|
|
30
|
+
DaffAuthLogoutSuccess,
|
|
31
|
+
DaffAuthLogoutFailure,
|
|
32
|
+
DaffAuthServerSide,
|
|
33
|
+
} from '@daffodil/auth/state';
|
|
34
|
+
import {
|
|
35
|
+
DaffAccountRegistrationFactory,
|
|
36
|
+
DaffAuthTokenFactory,
|
|
37
|
+
} from '@daffodil/auth/testing';
|
|
38
|
+
import {
|
|
39
|
+
DaffServerSideStorageError,
|
|
40
|
+
DaffStorageServiceError,
|
|
41
|
+
} from '@daffodil/core';
|
|
42
|
+
import { daffTransformErrorToStateError } from '@daffodil/core/state';
|
|
43
|
+
|
|
44
|
+
import { DaffAuthLoginEffects } from './login.effects';
|
|
45
|
+
|
|
46
|
+
describe('@daffodil/auth/state | DaffAuthLoginEffects', () => {
|
|
47
|
+
let actions$: Observable<any>;
|
|
48
|
+
let effects: DaffAuthLoginEffects;
|
|
49
|
+
|
|
50
|
+
let daffLoginDriver: jasmine.SpyObj<DaffLoginServiceInterface>;
|
|
51
|
+
let daffAuthStorageService: DaffAuthStorageService;
|
|
52
|
+
let setAuthTokenSpy: jasmine.Spy<DaffAuthStorageService['setAuthToken']>;
|
|
53
|
+
let removeAuthTokenSpy: jasmine.Spy<DaffAuthStorageService['removeAuthToken']>;
|
|
54
|
+
|
|
55
|
+
let registrationFactory: DaffAccountRegistrationFactory;
|
|
56
|
+
const authFactory: DaffAuthTokenFactory = new DaffAuthTokenFactory();
|
|
57
|
+
|
|
58
|
+
const authStorageFailureAction = new DaffAuthStorageFailure(daffTransformErrorToStateError(
|
|
59
|
+
new DaffStorageServiceError('Storage of auth token has failed.')),
|
|
60
|
+
);
|
|
61
|
+
const throwStorageError = () => {
|
|
62
|
+
throw new DaffStorageServiceError('Storage of auth token has failed.');
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
let mockAuth: DaffAuthToken;
|
|
66
|
+
let mockLoginInfo: DaffLoginInfo;
|
|
67
|
+
let token: string;
|
|
68
|
+
let email: string;
|
|
69
|
+
let password: string;
|
|
70
|
+
let mockRegistration: DaffAccountRegistration;
|
|
71
|
+
|
|
72
|
+
beforeEach(() => {
|
|
73
|
+
TestBed.configureTestingModule({
|
|
74
|
+
providers: [
|
|
75
|
+
DaffAuthLoginEffects,
|
|
76
|
+
provideMockActions(() => actions$),
|
|
77
|
+
{
|
|
78
|
+
provide: DaffLoginDriver,
|
|
79
|
+
useValue: jasmine.createSpyObj('DaffLoginService', ['login', 'logout']),
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
effects = TestBed.inject(DaffAuthLoginEffects);
|
|
85
|
+
|
|
86
|
+
daffLoginDriver = TestBed.inject<jasmine.SpyObj<DaffLoginServiceInterface>>(DaffLoginDriver);
|
|
87
|
+
daffAuthStorageService = TestBed.inject(DaffAuthStorageService);
|
|
88
|
+
registrationFactory = TestBed.inject(DaffAccountRegistrationFactory);
|
|
89
|
+
|
|
90
|
+
mockRegistration = registrationFactory.create();
|
|
91
|
+
mockAuth = authFactory.create();
|
|
92
|
+
setAuthTokenSpy = spyOn(daffAuthStorageService, 'setAuthToken');
|
|
93
|
+
removeAuthTokenSpy = spyOn(daffAuthStorageService, 'removeAuthToken');
|
|
94
|
+
|
|
95
|
+
token = mockAuth.token;
|
|
96
|
+
email = mockRegistration.email;
|
|
97
|
+
password = mockRegistration.password;
|
|
98
|
+
mockLoginInfo = { email, password };
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should be created', () => {
|
|
102
|
+
expect(effects).toBeTruthy();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('login$ | when the user logs in', () => {
|
|
106
|
+
let expected;
|
|
107
|
+
|
|
108
|
+
const mockAuthLoginAction = new DaffAuthLogin(mockLoginInfo);
|
|
109
|
+
|
|
110
|
+
describe('and the login is successful', () => {
|
|
111
|
+
beforeEach(() => {
|
|
112
|
+
daffLoginDriver.login.and.returnValue(of(mockAuth));
|
|
113
|
+
const mockAuthLoginSuccessAction = new DaffAuthLoginSuccess(mockAuth);
|
|
114
|
+
|
|
115
|
+
actions$ = hot('--a', { a: mockAuthLoginAction });
|
|
116
|
+
expected = cold('--b', { b: mockAuthLoginSuccessAction });
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should notify state that the login was successful', () => {
|
|
120
|
+
expect(effects.login$).toBeObservable(expected);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('and the login fails', () => {
|
|
125
|
+
beforeEach(() => {
|
|
126
|
+
const error = new DaffAuthenticationFailedError('Failed to log in');
|
|
127
|
+
const response = cold('#', {}, error);
|
|
128
|
+
daffLoginDriver.login.and.returnValue(response);
|
|
129
|
+
const mockAuthLoginFailureAction = new DaffAuthLoginFailure(daffTransformErrorToStateError(error));
|
|
130
|
+
|
|
131
|
+
actions$ = hot('--a', { a: mockAuthLoginAction });
|
|
132
|
+
expected = cold('--b', { b: mockAuthLoginFailureAction });
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should notify state that the login failed', () => {
|
|
136
|
+
expect(effects.login$).toBeObservable(expected);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('logout$ | when the user logs out', () => {
|
|
142
|
+
let expected;
|
|
143
|
+
|
|
144
|
+
const mockAuthLogoutAction = new DaffAuthLogout();
|
|
145
|
+
|
|
146
|
+
describe('and the logout is successful', () => {
|
|
147
|
+
beforeEach(() => {
|
|
148
|
+
daffLoginDriver.logout.and.returnValue(of(undefined));
|
|
149
|
+
const mockAuthLogoutSuccessAction = new DaffAuthLogoutSuccess();
|
|
150
|
+
|
|
151
|
+
actions$ = hot('--a', { a: mockAuthLogoutAction });
|
|
152
|
+
expected = cold('--b', { b: mockAuthLogoutSuccessAction });
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should notify state that the logout succeeded', () => {
|
|
156
|
+
expect(effects.logout$).toBeObservable(expected);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe('and the logout fails', () => {
|
|
161
|
+
beforeEach(() => {
|
|
162
|
+
const error = new DaffAuthInvalidAPIResponseError('Failed to log out');
|
|
163
|
+
const response = cold('#', {}, error);
|
|
164
|
+
daffLoginDriver.logout.and.returnValue(response);
|
|
165
|
+
const mockAuthLogoutFailureAction = new DaffAuthLogoutFailure(daffTransformErrorToStateError(error));
|
|
166
|
+
|
|
167
|
+
actions$ = hot('--a', { a: mockAuthLogoutAction });
|
|
168
|
+
expected = cold('--b', { b: mockAuthLogoutFailureAction });
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('should notify state that the logout failed', () => {
|
|
172
|
+
expect(effects.logout$).toBeObservable(expected);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe('storeAuthToken$ | storing the auth token after a successful login', () => {
|
|
178
|
+
let expected;
|
|
179
|
+
let authLoginSuccessAction;
|
|
180
|
+
|
|
181
|
+
beforeEach(() => {
|
|
182
|
+
authLoginSuccessAction = new DaffAuthLoginSuccess(mockAuth);
|
|
183
|
+
actions$ = hot('--a', { a: authLoginSuccessAction });
|
|
184
|
+
expected = cold('---');
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('should set the auth token in storage', () => {
|
|
188
|
+
expect(effects.storeAuthToken$).toBeObservable(expected);
|
|
189
|
+
expect(setAuthTokenSpy).toHaveBeenCalledWith(mockAuth.token);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe('and the storage service throws an error', () => {
|
|
193
|
+
beforeEach(() => {
|
|
194
|
+
setAuthTokenSpy.and.callFake(throwStorageError);
|
|
195
|
+
|
|
196
|
+
expected = cold('--(b|)', { b: authStorageFailureAction });
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('should return a DaffAuthStorageFailure', () => {
|
|
200
|
+
expect(effects.storeAuthToken$).toBeObservable(expected);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe('and the storage service throws a server side error', () => {
|
|
204
|
+
beforeEach(() => {
|
|
205
|
+
const error = new DaffServerSideStorageError('Server side');
|
|
206
|
+
const serverSideAction = new DaffAuthServerSide(daffTransformErrorToStateError(error));
|
|
207
|
+
setAuthTokenSpy.and.throwError(error);
|
|
208
|
+
expected = cold('--(a|)', { a: serverSideAction });
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('should dispatch a server side action', () => {
|
|
212
|
+
expect(effects.storeAuthToken$).toBeObservable(expected);
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
describe('removeAuthToken$', () => {
|
|
219
|
+
let expected;
|
|
220
|
+
let authLogoutSuccessAction: DaffAuthLogoutSuccess;
|
|
221
|
+
|
|
222
|
+
beforeEach(() => {
|
|
223
|
+
authLogoutSuccessAction = new DaffAuthLogoutSuccess();
|
|
224
|
+
actions$ = hot('--a', { a: authLogoutSuccessAction });
|
|
225
|
+
expected = cold('---');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('should remove the auth token from storage', () => {
|
|
229
|
+
expect(effects.removeAuthToken$).toBeObservable(expected);
|
|
230
|
+
expect(removeAuthTokenSpy).toHaveBeenCalledWith();
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe('and the storage service throws an error', () => {
|
|
234
|
+
beforeEach(() => {
|
|
235
|
+
removeAuthTokenSpy.and.callFake(throwStorageError);
|
|
236
|
+
|
|
237
|
+
expected = cold('--(b|)', { b: authStorageFailureAction });
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('should return a DaffAuthStorageFailure', () => {
|
|
241
|
+
expect(effects.removeAuthToken$).toBeObservable(expected);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe('and the storage service throws a server side error', () => {
|
|
246
|
+
beforeEach(() => {
|
|
247
|
+
const error = new DaffServerSideStorageError('Server side');
|
|
248
|
+
const serverSideAction = new DaffAuthServerSide(daffTransformErrorToStateError(error));
|
|
249
|
+
removeAuthTokenSpy.and.throwError(error);
|
|
250
|
+
expected = cold('--(a|)', { a: serverSideAction });
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('should dispatch a server side action', () => {
|
|
254
|
+
expect(effects.removeAuthToken$).toBeObservable(expected);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
});
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Injectable,
|
|
3
|
+
Inject,
|
|
4
|
+
} from '@angular/core';
|
|
5
|
+
import {
|
|
6
|
+
Actions,
|
|
7
|
+
createEffect,
|
|
8
|
+
ofType,
|
|
9
|
+
} from '@ngrx/effects';
|
|
10
|
+
import {
|
|
11
|
+
of,
|
|
12
|
+
Observable,
|
|
13
|
+
EMPTY,
|
|
14
|
+
} from 'rxjs';
|
|
15
|
+
import {
|
|
16
|
+
switchMap,
|
|
17
|
+
map,
|
|
18
|
+
catchError,
|
|
19
|
+
tap,
|
|
20
|
+
} from 'rxjs/operators';
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
DaffLoginInfo,
|
|
24
|
+
DaffAuthToken,
|
|
25
|
+
DAFF_AUTH_ERROR_MATCHER,
|
|
26
|
+
DaffAuthStorageService,
|
|
27
|
+
} from '@daffodil/auth';
|
|
28
|
+
import {
|
|
29
|
+
DaffLoginDriver,
|
|
30
|
+
DaffLoginServiceInterface,
|
|
31
|
+
} from '@daffodil/auth/driver';
|
|
32
|
+
import {
|
|
33
|
+
DaffError,
|
|
34
|
+
DaffServerSideStorageError,
|
|
35
|
+
DaffStorageServiceError,
|
|
36
|
+
} from '@daffodil/core';
|
|
37
|
+
import { ErrorTransformer } from '@daffodil/core/state';
|
|
38
|
+
|
|
39
|
+
import {
|
|
40
|
+
DaffAuthLoginActionTypes,
|
|
41
|
+
DaffAuthLogin,
|
|
42
|
+
DaffAuthLoginSuccess,
|
|
43
|
+
DaffAuthLoginFailure,
|
|
44
|
+
DaffAuthLogoutSuccess,
|
|
45
|
+
DaffAuthLogoutFailure,
|
|
46
|
+
DaffAuthLogout,
|
|
47
|
+
DaffAuthStorageFailure,
|
|
48
|
+
DaffAuthServerSide,
|
|
49
|
+
DaffAuthLoginActions,
|
|
50
|
+
} from '../actions/public_api';
|
|
51
|
+
|
|
52
|
+
@Injectable()
|
|
53
|
+
export class DaffAuthLoginEffects<
|
|
54
|
+
T extends DaffLoginInfo = DaffLoginInfo,
|
|
55
|
+
U extends DaffAuthToken = DaffAuthToken,
|
|
56
|
+
> {
|
|
57
|
+
constructor(
|
|
58
|
+
private actions$: Actions<DaffAuthLoginActions<T, U>>,
|
|
59
|
+
@Inject(DaffLoginDriver) private loginDriver: DaffLoginServiceInterface<T, U>,
|
|
60
|
+
@Inject(DAFF_AUTH_ERROR_MATCHER) private errorMatcher: ErrorTransformer,
|
|
61
|
+
private storage: DaffAuthStorageService,
|
|
62
|
+
) {}
|
|
63
|
+
|
|
64
|
+
login$ = createEffect(() => this.actions$.pipe(
|
|
65
|
+
ofType(DaffAuthLoginActionTypes.LoginAction),
|
|
66
|
+
switchMap((action) =>
|
|
67
|
+
this.loginDriver.login(action.loginInfo).pipe(
|
|
68
|
+
map((resp) => new DaffAuthLoginSuccess<U>(resp),
|
|
69
|
+
),
|
|
70
|
+
catchError((error: DaffError) =>
|
|
71
|
+
of(new DaffAuthLoginFailure(this.errorMatcher(error))),
|
|
72
|
+
),
|
|
73
|
+
),
|
|
74
|
+
),
|
|
75
|
+
));
|
|
76
|
+
|
|
77
|
+
logout$ = createEffect(() => this.actions$.pipe(
|
|
78
|
+
ofType(DaffAuthLoginActionTypes.LogoutAction),
|
|
79
|
+
switchMap((action: DaffAuthLogout) =>
|
|
80
|
+
this.loginDriver.logout().pipe(
|
|
81
|
+
map(() => new DaffAuthLogoutSuccess()),
|
|
82
|
+
catchError((error: DaffError) =>
|
|
83
|
+
of(new DaffAuthLogoutFailure(this.errorMatcher(error))),
|
|
84
|
+
),
|
|
85
|
+
),
|
|
86
|
+
),
|
|
87
|
+
));
|
|
88
|
+
|
|
89
|
+
removeAuthToken$ = createEffect(() => this.actions$.pipe(
|
|
90
|
+
ofType(
|
|
91
|
+
DaffAuthLoginActionTypes.LogoutSuccessAction,
|
|
92
|
+
),
|
|
93
|
+
tap(() => {
|
|
94
|
+
this.storage.removeAuthToken();
|
|
95
|
+
}),
|
|
96
|
+
switchMap(() => EMPTY),
|
|
97
|
+
catchError((error: Error) => {
|
|
98
|
+
switch (true) {
|
|
99
|
+
case error instanceof DaffServerSideStorageError:
|
|
100
|
+
return of(new DaffAuthServerSide(this.errorMatcher(error)));
|
|
101
|
+
|
|
102
|
+
case error instanceof DaffStorageServiceError:
|
|
103
|
+
default:
|
|
104
|
+
return of(new DaffAuthStorageFailure(this.errorMatcher(error)));
|
|
105
|
+
}
|
|
106
|
+
}),
|
|
107
|
+
), { dispatch: false });
|
|
108
|
+
|
|
109
|
+
storeAuthToken$ = createEffect(() => this.actions$.pipe(
|
|
110
|
+
ofType(DaffAuthLoginActionTypes.LoginSuccessAction),
|
|
111
|
+
tap((action) => {
|
|
112
|
+
this.storage.setAuthToken(action.auth.token);
|
|
113
|
+
}),
|
|
114
|
+
switchMap(() => EMPTY),
|
|
115
|
+
catchError((error: Error) => {
|
|
116
|
+
switch (true) {
|
|
117
|
+
case error instanceof DaffServerSideStorageError:
|
|
118
|
+
return of(new DaffAuthServerSide(this.errorMatcher(error)));
|
|
119
|
+
|
|
120
|
+
case error instanceof DaffStorageServiceError:
|
|
121
|
+
default:
|
|
122
|
+
return of(new DaffAuthStorageFailure(this.errorMatcher(error)));
|
|
123
|
+
}
|
|
124
|
+
}),
|
|
125
|
+
), { dispatch: false });
|
|
126
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
import { provideMockActions } from '@ngrx/effects/testing';
|
|
3
|
+
import {
|
|
4
|
+
hot,
|
|
5
|
+
cold,
|
|
6
|
+
} from 'jasmine-marbles';
|
|
7
|
+
import {
|
|
8
|
+
Observable ,
|
|
9
|
+
of,
|
|
10
|
+
} from 'rxjs';
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
DaffLoginInfo,
|
|
14
|
+
DaffAuthToken,
|
|
15
|
+
DaffAccountRegistration,
|
|
16
|
+
DaffAuthStorageService,
|
|
17
|
+
} from '@daffodil/auth';
|
|
18
|
+
import {
|
|
19
|
+
DaffAuthInvalidAPIResponseError,
|
|
20
|
+
DaffRegisterDriver,
|
|
21
|
+
DaffRegisterServiceInterface,
|
|
22
|
+
} from '@daffodil/auth/driver';
|
|
23
|
+
import {
|
|
24
|
+
DaffAuthRegister,
|
|
25
|
+
DaffAuthRegisterSuccess,
|
|
26
|
+
DaffAuthRegisterFailure,
|
|
27
|
+
DaffAuthServerSide,
|
|
28
|
+
DaffAuthStorageFailure,
|
|
29
|
+
} from '@daffodil/auth/state';
|
|
30
|
+
import {
|
|
31
|
+
DaffAccountRegistrationFactory,
|
|
32
|
+
DaffAuthTokenFactory,
|
|
33
|
+
} from '@daffodil/auth/testing';
|
|
34
|
+
import {
|
|
35
|
+
DaffServerSideStorageError,
|
|
36
|
+
DaffStorageServiceError,
|
|
37
|
+
} from '@daffodil/core';
|
|
38
|
+
import { daffTransformErrorToStateError } from '@daffodil/core/state';
|
|
39
|
+
|
|
40
|
+
import { DaffAuthRegisterEffects } from './register.effects';
|
|
41
|
+
|
|
42
|
+
describe('@daffodil/auth/state | DaffAuthRegisterEffects', () => {
|
|
43
|
+
let actions$: Observable<any>;
|
|
44
|
+
let effects: DaffAuthRegisterEffects;
|
|
45
|
+
|
|
46
|
+
let daffRegisterDriver: jasmine.SpyObj<DaffRegisterServiceInterface>;
|
|
47
|
+
let daffAuthStorageService: DaffAuthStorageService;
|
|
48
|
+
|
|
49
|
+
let registrationFactory: DaffAccountRegistrationFactory;
|
|
50
|
+
const authFactory: DaffAuthTokenFactory = new DaffAuthTokenFactory();
|
|
51
|
+
|
|
52
|
+
let setAuthTokenSpy: jasmine.Spy;
|
|
53
|
+
let mockAuth: DaffAuthToken;
|
|
54
|
+
let mockLoginInfo: DaffLoginInfo;
|
|
55
|
+
let token: string;
|
|
56
|
+
let email: string;
|
|
57
|
+
let password: string;
|
|
58
|
+
let mockRegistration: DaffAccountRegistration;
|
|
59
|
+
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
TestBed.configureTestingModule({
|
|
62
|
+
providers: [
|
|
63
|
+
DaffAuthRegisterEffects,
|
|
64
|
+
provideMockActions(() => actions$),
|
|
65
|
+
{
|
|
66
|
+
provide: DaffRegisterDriver,
|
|
67
|
+
useValue: jasmine.createSpyObj('DaffRegisterService', ['register', 'registerOnly']),
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
effects = TestBed.inject(DaffAuthRegisterEffects);
|
|
73
|
+
|
|
74
|
+
daffRegisterDriver = TestBed.inject<jasmine.SpyObj<DaffRegisterServiceInterface>>(DaffRegisterDriver);
|
|
75
|
+
daffAuthStorageService = TestBed.inject(DaffAuthStorageService);
|
|
76
|
+
registrationFactory = TestBed.inject(DaffAccountRegistrationFactory);
|
|
77
|
+
|
|
78
|
+
mockRegistration = registrationFactory.create();
|
|
79
|
+
mockAuth = authFactory.create();
|
|
80
|
+
|
|
81
|
+
setAuthTokenSpy = spyOn(daffAuthStorageService, 'setAuthToken');
|
|
82
|
+
token = mockAuth.token;
|
|
83
|
+
email = mockRegistration.email;
|
|
84
|
+
password = mockRegistration.password;
|
|
85
|
+
mockLoginInfo = { email, password };
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should be created', () => {
|
|
89
|
+
expect(effects).toBeTruthy();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('register$ | when the user registers an account', () => {
|
|
93
|
+
let expected;
|
|
94
|
+
let mockAuthRegisterAction: DaffAuthRegister;
|
|
95
|
+
|
|
96
|
+
describe('when autoLogin is true', () => {
|
|
97
|
+
beforeEach(() => {
|
|
98
|
+
mockAuthRegisterAction = new DaffAuthRegister(mockRegistration, true);
|
|
99
|
+
actions$ = hot('--a', { a: mockAuthRegisterAction });
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('and the register is successful', () => {
|
|
103
|
+
beforeEach(() => {
|
|
104
|
+
daffRegisterDriver.register.and.returnValue(of(token));
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe('and setToken is successful', () => {
|
|
108
|
+
beforeEach(() => {
|
|
109
|
+
const mockAuthResetPasswordSuccessAction = new DaffAuthRegisterSuccess(token);
|
|
110
|
+
|
|
111
|
+
expected = cold('--a', { a: mockAuthResetPasswordSuccessAction });
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should notify state that the register was successful', () => {
|
|
115
|
+
expect(effects.register$).toBeObservable(expected);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should store the auth token', () => {
|
|
119
|
+
expect(effects.register$).toBeObservable(expected);
|
|
120
|
+
expect(setAuthTokenSpy).toHaveBeenCalledWith(token);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('and the storage service throws a server side error', () => {
|
|
125
|
+
beforeEach(() => {
|
|
126
|
+
const error = new DaffServerSideStorageError('Server side');
|
|
127
|
+
const serverSideAction = new DaffAuthServerSide(daffTransformErrorToStateError(error));
|
|
128
|
+
const mockAuthResetPasswordFailureAction = new DaffAuthRegisterFailure(daffTransformErrorToStateError(error));
|
|
129
|
+
setAuthTokenSpy.and.throwError(error);
|
|
130
|
+
expected = cold('--(ab)', { a: serverSideAction, b: mockAuthResetPasswordFailureAction });
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should dispatch a server side and a failure action', () => {
|
|
134
|
+
expect(effects.register$).toBeObservable(expected);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe('and the storage service throws a storage error', () => {
|
|
139
|
+
beforeEach(() => {
|
|
140
|
+
const error = new DaffStorageServiceError('Storage error');
|
|
141
|
+
const storageAction = new DaffAuthStorageFailure(daffTransformErrorToStateError(error));
|
|
142
|
+
const mockAuthResetPasswordFailureAction = new DaffAuthRegisterFailure(daffTransformErrorToStateError(error));
|
|
143
|
+
setAuthTokenSpy.and.throwError(error);
|
|
144
|
+
expected = cold('--(ab)', { a: storageAction, b: mockAuthResetPasswordFailureAction });
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should dispatch a server side action', () => {
|
|
148
|
+
expect(effects.register$).toBeObservable(expected);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('and the register fails', () => {
|
|
154
|
+
beforeEach(() => {
|
|
155
|
+
const error = new DaffAuthInvalidAPIResponseError('Failed to register a new user');
|
|
156
|
+
const response = cold('#', {}, error);
|
|
157
|
+
daffRegisterDriver.register.and.returnValue(response);
|
|
158
|
+
const mockAuthResetPasswordFailureAction = new DaffAuthRegisterFailure(daffTransformErrorToStateError(error));
|
|
159
|
+
|
|
160
|
+
actions$ = hot('--a', { a: mockAuthRegisterAction });
|
|
161
|
+
expected = cold('--b', { b: mockAuthResetPasswordFailureAction });
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('should notify state that the register failed', () => {
|
|
165
|
+
expect(effects.register$).toBeObservable(expected);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe('when autoLogin is false', () => {
|
|
171
|
+
beforeEach(() => {
|
|
172
|
+
mockAuthRegisterAction = new DaffAuthRegister(mockRegistration, false);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe('and the register is successful', () => {
|
|
176
|
+
beforeEach(() => {
|
|
177
|
+
daffRegisterDriver.registerOnly.and.returnValue(of(undefined));
|
|
178
|
+
const mockAuthResetPasswordSuccessAction = new DaffAuthRegisterSuccess();
|
|
179
|
+
|
|
180
|
+
actions$ = hot('--a', { a: mockAuthRegisterAction });
|
|
181
|
+
expected = cold('--b', { b: mockAuthResetPasswordSuccessAction });
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should notify state that the register was successful', () => {
|
|
185
|
+
expect(effects.register$).toBeObservable(expected);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe('and the register fails', () => {
|
|
190
|
+
beforeEach(() => {
|
|
191
|
+
const error = new DaffAuthInvalidAPIResponseError('Failed to register a new user');
|
|
192
|
+
const response = cold('#', {}, error);
|
|
193
|
+
daffRegisterDriver.registerOnly.and.returnValue(response);
|
|
194
|
+
const mockAuthResetPasswordFailureAction = new DaffAuthRegisterFailure(daffTransformErrorToStateError(error));
|
|
195
|
+
|
|
196
|
+
actions$ = hot('--a', { a: mockAuthRegisterAction });
|
|
197
|
+
expected = cold('--b', { b: mockAuthResetPasswordFailureAction });
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should notify state that the register failed', () => {
|
|
201
|
+
expect(effects.register$).toBeObservable(expected);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Injectable,
|
|
3
|
+
Inject,
|
|
4
|
+
} from '@angular/core';
|
|
5
|
+
import {
|
|
6
|
+
Actions,
|
|
7
|
+
createEffect,
|
|
8
|
+
ofType,
|
|
9
|
+
} from '@ngrx/effects';
|
|
10
|
+
import {
|
|
11
|
+
of,
|
|
12
|
+
Observable,
|
|
13
|
+
iif,
|
|
14
|
+
defer,
|
|
15
|
+
} from 'rxjs';
|
|
16
|
+
import {
|
|
17
|
+
switchMap,
|
|
18
|
+
map,
|
|
19
|
+
catchError,
|
|
20
|
+
tap,
|
|
21
|
+
} from 'rxjs/operators';
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
DaffAccountRegistration,
|
|
25
|
+
DaffAuthStorageService,
|
|
26
|
+
DAFF_AUTH_ERROR_MATCHER,
|
|
27
|
+
} from '@daffodil/auth';
|
|
28
|
+
import {
|
|
29
|
+
DaffRegisterDriver,
|
|
30
|
+
DaffRegisterServiceInterface,
|
|
31
|
+
} from '@daffodil/auth/driver';
|
|
32
|
+
import {
|
|
33
|
+
DaffError,
|
|
34
|
+
DaffServerSideStorageError,
|
|
35
|
+
DaffStorageServiceError,
|
|
36
|
+
} from '@daffodil/core';
|
|
37
|
+
import { ErrorTransformer } from '@daffodil/core/state';
|
|
38
|
+
|
|
39
|
+
import {
|
|
40
|
+
DaffAuthRegisterActionTypes,
|
|
41
|
+
DaffAuthRegisterSuccess,
|
|
42
|
+
DaffAuthRegisterFailure,
|
|
43
|
+
DaffAuthServerSide,
|
|
44
|
+
DaffAuthStorageFailure,
|
|
45
|
+
DaffAuthRegisterActions,
|
|
46
|
+
} from '../actions/public_api';
|
|
47
|
+
|
|
48
|
+
@Injectable()
|
|
49
|
+
export class DaffAuthRegisterEffects<
|
|
50
|
+
T extends DaffAccountRegistration = DaffAccountRegistration,
|
|
51
|
+
> {
|
|
52
|
+
constructor(
|
|
53
|
+
private actions$: Actions<DaffAuthRegisterActions<T>>,
|
|
54
|
+
@Inject(DaffRegisterDriver) private registerDriver: DaffRegisterServiceInterface<T>,
|
|
55
|
+
@Inject(DAFF_AUTH_ERROR_MATCHER) private errorMatcher: ErrorTransformer,
|
|
56
|
+
private storage: DaffAuthStorageService,
|
|
57
|
+
) {}
|
|
58
|
+
|
|
59
|
+
register$: Observable<any> = createEffect(() => this.actions$.pipe(
|
|
60
|
+
ofType(DaffAuthRegisterActionTypes.RegisterAction),
|
|
61
|
+
switchMap((action) =>
|
|
62
|
+
iif(
|
|
63
|
+
() => action.autoLogin,
|
|
64
|
+
defer(() => this.registerDriver.register(action.registration).pipe(
|
|
65
|
+
tap(token => this.storage.setAuthToken(token)),
|
|
66
|
+
map((token) => new DaffAuthRegisterSuccess(token)),
|
|
67
|
+
)),
|
|
68
|
+
defer(() => this.registerDriver.registerOnly(action.registration).pipe(
|
|
69
|
+
map(() => new DaffAuthRegisterSuccess()),
|
|
70
|
+
)),
|
|
71
|
+
).pipe(
|
|
72
|
+
catchError((error: DaffError) => {
|
|
73
|
+
switch (true) {
|
|
74
|
+
case error instanceof DaffServerSideStorageError:
|
|
75
|
+
return of(
|
|
76
|
+
new DaffAuthServerSide(this.errorMatcher(error)),
|
|
77
|
+
new DaffAuthRegisterFailure(this.errorMatcher(error)),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
case error instanceof DaffStorageServiceError:
|
|
81
|
+
return of(
|
|
82
|
+
new DaffAuthStorageFailure(this.errorMatcher(error)),
|
|
83
|
+
new DaffAuthRegisterFailure(this.errorMatcher(error)),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
default:
|
|
87
|
+
return of(new DaffAuthRegisterFailure(this.errorMatcher(error)));
|
|
88
|
+
}
|
|
89
|
+
}),
|
|
90
|
+
),
|
|
91
|
+
),
|
|
92
|
+
));
|
|
93
|
+
}
|