@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,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Inject,
|
|
3
|
+
Injectable,
|
|
4
|
+
} from '@angular/core';
|
|
5
|
+
import {
|
|
6
|
+
CanActivate,
|
|
7
|
+
Router,
|
|
8
|
+
} from '@angular/router';
|
|
9
|
+
import { Store } from '@ngrx/store';
|
|
10
|
+
import {
|
|
11
|
+
Observable,
|
|
12
|
+
of,
|
|
13
|
+
} from 'rxjs';
|
|
14
|
+
import {
|
|
15
|
+
catchError,
|
|
16
|
+
map,
|
|
17
|
+
tap,
|
|
18
|
+
} from 'rxjs/operators';
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
DAFF_AUTH_ERROR_MATCHER,
|
|
22
|
+
DaffAuthStorageService,
|
|
23
|
+
} from '@daffodil/auth';
|
|
24
|
+
import {
|
|
25
|
+
DaffAuthDriverErrorCodes,
|
|
26
|
+
DaffAuthDriverTokenCheck,
|
|
27
|
+
} from '@daffodil/auth/driver';
|
|
28
|
+
import { DaffAuthGuardLogout } from '@daffodil/auth/state';
|
|
29
|
+
import { DaffError } from '@daffodil/core';
|
|
30
|
+
import { ErrorTransformer } from '@daffodil/core/state';
|
|
31
|
+
|
|
32
|
+
import { DaffAuthMemberOnlyGuardRedirectUrl } from './member-only-guard-redirect.token';
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@Injectable({
|
|
36
|
+
providedIn: 'any',
|
|
37
|
+
})
|
|
38
|
+
export class MemberOnlyGuard implements CanActivate {
|
|
39
|
+
constructor(
|
|
40
|
+
private tokenCheck: DaffAuthDriverTokenCheck,
|
|
41
|
+
private storage: DaffAuthStorageService,
|
|
42
|
+
private router: Router,
|
|
43
|
+
private store: Store,
|
|
44
|
+
@Inject(DAFF_AUTH_ERROR_MATCHER) private errorMatcher: ErrorTransformer,
|
|
45
|
+
@Inject(DaffAuthMemberOnlyGuardRedirectUrl) private redirectUrl: string,
|
|
46
|
+
) {}
|
|
47
|
+
|
|
48
|
+
canActivate(): Observable<boolean> {
|
|
49
|
+
return this.isAuthenticated().pipe(
|
|
50
|
+
tap(result => {
|
|
51
|
+
if (!result) {
|
|
52
|
+
this.router.navigateByUrl(this.redirectUrl);
|
|
53
|
+
}
|
|
54
|
+
}),
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private isAuthenticated(): Observable<boolean> {
|
|
59
|
+
return this.tokenCheck.check().pipe(
|
|
60
|
+
map(() => true),
|
|
61
|
+
catchError((error: DaffError) => {
|
|
62
|
+
if (error.code === DaffAuthDriverErrorCodes.UNAUTHORIZED) {
|
|
63
|
+
this.storage.removeAuthToken();
|
|
64
|
+
this.store.dispatch(new DaffAuthGuardLogout(this.errorMatcher(error)));
|
|
65
|
+
}
|
|
66
|
+
return of(false);
|
|
67
|
+
}),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { MemberOnlyGuard } from './member-only.guard';
|
|
2
|
+
export { GuestOnlyGuard } from './guest-only.guard';
|
|
3
|
+
export { DaffAuthResetPasswordGuard } from './reset-password.guard';
|
|
4
|
+
|
|
5
|
+
export { DaffAuthGuestOnlyGuardRedirectUrl } from './guest-only-guard-redirect.token';
|
|
6
|
+
export { DaffAuthMemberOnlyGuardRedirectUrl } from './member-only-guard-redirect.token';
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Component } from '@angular/core';
|
|
2
|
+
import {
|
|
3
|
+
fakeAsync,
|
|
4
|
+
TestBed,
|
|
5
|
+
tick,
|
|
6
|
+
} from '@angular/core/testing';
|
|
7
|
+
import {
|
|
8
|
+
ActivatedRoute,
|
|
9
|
+
Router,
|
|
10
|
+
} from '@angular/router';
|
|
11
|
+
import { RouterTestingModule } from '@angular/router/testing';
|
|
12
|
+
import { Observable } from 'rxjs';
|
|
13
|
+
|
|
14
|
+
import { DAFF_AUTH_ROUTING_CONFIG } from '@daffodil/auth/routing';
|
|
15
|
+
import { DaffResetPasswordLanding } from '@daffodil/auth/state';
|
|
16
|
+
import {
|
|
17
|
+
DaffAuthTestingModule,
|
|
18
|
+
MockDaffAuthFacade,
|
|
19
|
+
} from '@daffodil/auth/state/testing';
|
|
20
|
+
|
|
21
|
+
import { DaffAuthResetPasswordGuard } from './reset-password.guard';
|
|
22
|
+
|
|
23
|
+
@Component({ template: '' })
|
|
24
|
+
class TestComponent {}
|
|
25
|
+
|
|
26
|
+
describe('@daffodil/auth/routing | DaffAuthResetPasswordGuard', () => {
|
|
27
|
+
let guard: DaffAuthResetPasswordGuard;
|
|
28
|
+
|
|
29
|
+
let mockFacade: MockDaffAuthFacade;
|
|
30
|
+
let router: Router;
|
|
31
|
+
|
|
32
|
+
let param: string;
|
|
33
|
+
let token: string;
|
|
34
|
+
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
param = 'token';
|
|
37
|
+
token = '290384runfei9usnrg0ew9rgm';
|
|
38
|
+
|
|
39
|
+
TestBed.configureTestingModule({
|
|
40
|
+
imports: [
|
|
41
|
+
DaffAuthTestingModule,
|
|
42
|
+
RouterTestingModule.withRoutes([
|
|
43
|
+
{
|
|
44
|
+
path: 'reset',
|
|
45
|
+
component: TestComponent,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
path: '**',
|
|
49
|
+
component: TestComponent,
|
|
50
|
+
},
|
|
51
|
+
]),
|
|
52
|
+
],
|
|
53
|
+
providers: [
|
|
54
|
+
{
|
|
55
|
+
provide: DAFF_AUTH_ROUTING_CONFIG,
|
|
56
|
+
useValue: {
|
|
57
|
+
resetPasswordTokenParam: param,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
guard = TestBed.inject(DaffAuthResetPasswordGuard);
|
|
64
|
+
mockFacade = TestBed.inject(MockDaffAuthFacade);
|
|
65
|
+
router = TestBed.inject(Router);
|
|
66
|
+
|
|
67
|
+
spyOn(mockFacade, 'dispatch');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('canActivate | checking if the route can be activated', () => {
|
|
71
|
+
let result: Observable<boolean>;
|
|
72
|
+
|
|
73
|
+
describe('when there is a token set to the param', () => {
|
|
74
|
+
beforeEach(fakeAsync(() => {
|
|
75
|
+
router.navigateByUrl(`/reset?${param}=${token}`);
|
|
76
|
+
tick();
|
|
77
|
+
result = guard.canActivate(TestBed.inject(ActivatedRoute).snapshot);
|
|
78
|
+
}));
|
|
79
|
+
|
|
80
|
+
it('should allow activation', done => {
|
|
81
|
+
result.subscribe(res => {
|
|
82
|
+
expect(res).toBeTrue();
|
|
83
|
+
done();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should dispatch DaffResetPasswordLanding with the token', () => {
|
|
88
|
+
result.subscribe();
|
|
89
|
+
expect(mockFacade.dispatch).toHaveBeenCalledWith(new DaffResetPasswordLanding(token));
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('when there is a token not set to the param', () => {
|
|
94
|
+
beforeEach(fakeAsync(() => {
|
|
95
|
+
router.navigateByUrl(`/reset?not${param}=${token}`);
|
|
96
|
+
tick();
|
|
97
|
+
result = guard.canActivate(TestBed.inject(ActivatedRoute).snapshot);
|
|
98
|
+
}));
|
|
99
|
+
|
|
100
|
+
it('should not allow activation', done => {
|
|
101
|
+
result.subscribe(res => {
|
|
102
|
+
expect(res).toBeFalse();
|
|
103
|
+
done();
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should not dispatch DaffResetPasswordLanding with the token', () => {
|
|
108
|
+
result.subscribe();
|
|
109
|
+
expect(mockFacade.dispatch).not.toHaveBeenCalledWith(new DaffResetPasswordLanding(token));
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Inject,
|
|
3
|
+
Injectable,
|
|
4
|
+
} from '@angular/core';
|
|
5
|
+
import {
|
|
6
|
+
ActivatedRouteSnapshot,
|
|
7
|
+
CanActivate,
|
|
8
|
+
} from '@angular/router';
|
|
9
|
+
import {
|
|
10
|
+
Observable,
|
|
11
|
+
of,
|
|
12
|
+
} from 'rxjs';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
DaffAuthFacade,
|
|
16
|
+
DaffResetPasswordLanding,
|
|
17
|
+
} from '@daffodil/auth/state';
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
DaffAuthRoutingConfig,
|
|
21
|
+
DAFF_AUTH_ROUTING_CONFIG,
|
|
22
|
+
} from '../config/public_api';
|
|
23
|
+
|
|
24
|
+
@Injectable({
|
|
25
|
+
providedIn: 'any',
|
|
26
|
+
})
|
|
27
|
+
export class DaffAuthResetPasswordGuard implements CanActivate {
|
|
28
|
+
constructor(
|
|
29
|
+
private facade: DaffAuthFacade,
|
|
30
|
+
@Inject(DAFF_AUTH_ROUTING_CONFIG) private config: DaffAuthRoutingConfig,
|
|
31
|
+
) {}
|
|
32
|
+
|
|
33
|
+
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
|
|
34
|
+
const token = route.queryParamMap.get(this.config.resetPasswordTokenParam);
|
|
35
|
+
|
|
36
|
+
if (!token) {
|
|
37
|
+
return of(false);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.facade.dispatch(new DaffResetPasswordLanding(token));
|
|
41
|
+
|
|
42
|
+
return of(true);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './public_api';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ModuleWithProviders,
|
|
3
|
+
NgModule,
|
|
4
|
+
} from '@angular/core';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
DaffAuthRoutingConfig,
|
|
8
|
+
DAFF_AUTH_ROUTING_CONFIG,
|
|
9
|
+
DAFF_AUTH_ROUTING_CONFIG_DEFAULT,
|
|
10
|
+
provideDaffAuthRoutingConfig,
|
|
11
|
+
} from './config/public_api';
|
|
12
|
+
import {
|
|
13
|
+
DaffAuthResetPasswordGuard,
|
|
14
|
+
GuestOnlyGuard,
|
|
15
|
+
MemberOnlyGuard,
|
|
16
|
+
} from './guards/public_api';
|
|
17
|
+
|
|
18
|
+
@NgModule({
|
|
19
|
+
providers: [
|
|
20
|
+
DaffAuthResetPasswordGuard,
|
|
21
|
+
MemberOnlyGuard,
|
|
22
|
+
GuestOnlyGuard,
|
|
23
|
+
],
|
|
24
|
+
})
|
|
25
|
+
export class DaffAuthRoutingModule {
|
|
26
|
+
static withConfig(config?: Partial<DaffAuthRoutingConfig>): ModuleWithProviders<DaffAuthRoutingModule> {
|
|
27
|
+
return {
|
|
28
|
+
ngModule: DaffAuthRoutingModule,
|
|
29
|
+
providers: [
|
|
30
|
+
provideDaffAuthRoutingConfig(config),
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { NgModule } from '@angular/core';
|
|
2
|
+
import { EffectsModule } from '@ngrx/effects';
|
|
3
|
+
|
|
4
|
+
import { DaffAuthRedirectEffects } from './effects/redirect.effects';
|
|
5
|
+
|
|
6
|
+
@NgModule({
|
|
7
|
+
imports: [
|
|
8
|
+
EffectsModule.forFeature([
|
|
9
|
+
DaffAuthRedirectEffects,
|
|
10
|
+
]),
|
|
11
|
+
],
|
|
12
|
+
})
|
|
13
|
+
export class DaffAuthRoutingRedirectModule {}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DaffAuthErrorCodes } from './codes.enum';
|
|
2
|
+
import { DaffAuthMissingTokenError } from './missing-token';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A mapping from error codes to error class constructors.
|
|
6
|
+
*/
|
|
7
|
+
export const DaffAuthErrorMap = {
|
|
8
|
+
[DaffAuthErrorCodes.MISSING_TOKEN]: DaffAuthMissingTokenError,
|
|
9
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DaffError,
|
|
3
|
+
DaffInheritableError,
|
|
4
|
+
} from '@daffodil/core';
|
|
5
|
+
|
|
6
|
+
import { DaffAuthErrorCodes } from './codes.enum';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* An error thrown when an authenticated action was initiated
|
|
10
|
+
* but an auth token was not present.
|
|
11
|
+
*/
|
|
12
|
+
export class DaffAuthMissingTokenError extends DaffInheritableError implements DaffError {
|
|
13
|
+
public readonly code: string = DaffAuthErrorCodes.MISSING_TOKEN;
|
|
14
|
+
|
|
15
|
+
constructor(public message: string) {
|
|
16
|
+
super(message);
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './public_api';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { InjectionToken } from '@angular/core';
|
|
2
|
+
|
|
3
|
+
import { daffTransformErrorToStateError } from '@daffodil/core/state';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Transforms `DaffError`s into `DaffStateError`s before they are serialized into state.
|
|
7
|
+
* Can be used to further refine Daffodil errors into more specific app errors.
|
|
8
|
+
*/
|
|
9
|
+
export const DAFF_AUTH_ERROR_MATCHER = new InjectionToken<typeof daffTransformErrorToStateError>(
|
|
10
|
+
'DAFF_AUTH_ERROR_MATCHER',
|
|
11
|
+
{ factory: () => daffTransformErrorToStateError },
|
|
12
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DAFF_AUTH_ERROR_MATCHER } from './error-matcher.token';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DaffLoginInfo } from './login-info';
|
|
2
|
+
|
|
3
|
+
export interface DaffAccountRegistration extends DaffLoginInfo {
|
|
4
|
+
/**
|
|
5
|
+
* Whether the customer be subscribed to the newsletter upon registration.
|
|
6
|
+
*/
|
|
7
|
+
subscribe?: boolean;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The customer's first name.
|
|
11
|
+
*/
|
|
12
|
+
firstName?: string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The customer's last name.
|
|
16
|
+
*/
|
|
17
|
+
lastName?: string;
|
|
18
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { DaffAuthStorageService } from './auth-storage.service';
|
|
4
|
+
|
|
5
|
+
describe('DaffAuthStorageService', () => {
|
|
6
|
+
let service: DaffAuthStorageService;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
TestBed.configureTestingModule({
|
|
10
|
+
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
service = TestBed.inject(DaffAuthStorageService);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
service.removeAuthToken();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should be created', () => {
|
|
21
|
+
expect(service).toBeTruthy();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should be able to persist and retrieve the auth token', () => {
|
|
25
|
+
service.setAuthToken('id');
|
|
26
|
+
expect(service.getAuthToken()).toEqual('id');
|
|
27
|
+
|
|
28
|
+
service.setAuthToken('bob');
|
|
29
|
+
expect(service.getAuthToken()).toEqual('bob');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should be able to persist a auth token', () => {
|
|
33
|
+
service.setAuthToken('id');
|
|
34
|
+
expect(service.getAuthToken()).toEqual('id');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should be able to remove a auth token', () => {
|
|
38
|
+
service.setAuthToken('id');
|
|
39
|
+
expect(service.getAuthToken()).toEqual('id');
|
|
40
|
+
service.removeAuthToken();
|
|
41
|
+
expect(service.getAuthToken()).toEqual(null);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Injectable,
|
|
3
|
+
Inject,
|
|
4
|
+
} from '@angular/core';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
DaffPersistenceService,
|
|
8
|
+
DaffPersistenceServiceToken,
|
|
9
|
+
} from '@daffodil/core';
|
|
10
|
+
|
|
11
|
+
@Injectable({
|
|
12
|
+
providedIn: 'root',
|
|
13
|
+
})
|
|
14
|
+
export class DaffAuthStorageService {
|
|
15
|
+
private readonly AUTH_STORAGE_TOKEN = 'DAFF_AUTH_TOKEN';
|
|
16
|
+
|
|
17
|
+
constructor(@Inject(DaffPersistenceServiceToken) private storageService: DaffPersistenceService) {}
|
|
18
|
+
|
|
19
|
+
getAuthToken(): string {
|
|
20
|
+
return this.storageService.getItem(this.AUTH_STORAGE_TOKEN);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
setAuthToken(value: string): void {
|
|
24
|
+
this.storageService.setItem(this.AUTH_STORAGE_TOKEN, value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
removeAuthToken(): void {
|
|
28
|
+
this.storageService.removeItem(this.AUTH_STORAGE_TOKEN);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Action } from '@ngrx/store';
|
|
2
|
+
|
|
3
|
+
import { DaffStateError } from '@daffodil/core/state';
|
|
4
|
+
|
|
5
|
+
export enum DaffAuthActionTypes {
|
|
6
|
+
AuthStorageFailureAction = '[@daffodil/auth] Auth Storage Failure Action',
|
|
7
|
+
AuthGuardLogoutAction = '[@daffodil/auth] Auth Guard Logout Action',
|
|
8
|
+
AuthServerSideAction = '[@daffodil/auth] Auth Server Side Action',
|
|
9
|
+
AuthCheckAction = '[@daffodil/auth] Auth Check Action',
|
|
10
|
+
AuthCheckSuccessAction = '[@daffodil/auth] Auth Check Success Action',
|
|
11
|
+
AuthCheckFailureAction = '[@daffodil/auth] Auth Check Failure Action',
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* An action triggered upon a failed auth storage operation.
|
|
16
|
+
*/
|
|
17
|
+
export class DaffAuthStorageFailure implements Action {
|
|
18
|
+
readonly type = DaffAuthActionTypes.AuthStorageFailureAction;
|
|
19
|
+
|
|
20
|
+
constructor(public errorMessage: DaffStateError) {}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/*
|
|
24
|
+
* An action triggered when an auth token is cleared from storage by a routing guard.
|
|
25
|
+
*/
|
|
26
|
+
export class DaffAuthGuardLogout implements Action {
|
|
27
|
+
readonly type = DaffAuthActionTypes.AuthGuardLogoutAction;
|
|
28
|
+
|
|
29
|
+
constructor(public errorMessage: DaffStateError) {}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* An action triggered when an invalid operation is performed in the server environment.
|
|
34
|
+
* A common example is trying to access the auth token from storage.
|
|
35
|
+
*/
|
|
36
|
+
export class DaffAuthServerSide implements Action {
|
|
37
|
+
readonly type = DaffAuthActionTypes.AuthServerSideAction;
|
|
38
|
+
|
|
39
|
+
constructor(public errorMessage: DaffStateError) {}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* An action triggered to initialize an auth check request.
|
|
44
|
+
*/
|
|
45
|
+
export class DaffAuthCheck implements Action {
|
|
46
|
+
readonly type = DaffAuthActionTypes.AuthCheckAction;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* An action triggered upon a successful auth check.
|
|
51
|
+
*
|
|
52
|
+
* @param token - the customer access token
|
|
53
|
+
*/
|
|
54
|
+
export class DaffAuthCheckSuccess implements Action {
|
|
55
|
+
readonly type = DaffAuthActionTypes.AuthCheckSuccessAction;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* An action triggered upon a failed auth check request.
|
|
60
|
+
*
|
|
61
|
+
* @param errorMessage - an error message
|
|
62
|
+
*/
|
|
63
|
+
export class DaffAuthCheckFailure implements Action {
|
|
64
|
+
readonly type = DaffAuthActionTypes.AuthCheckFailureAction;
|
|
65
|
+
|
|
66
|
+
constructor(public errorMessage: DaffStateError) {}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type DaffAuthActions =
|
|
70
|
+
| DaffAuthStorageFailure
|
|
71
|
+
| DaffAuthGuardLogout
|
|
72
|
+
| DaffAuthServerSide
|
|
73
|
+
| DaffAuthCheck
|
|
74
|
+
| DaffAuthCheckSuccess
|
|
75
|
+
| DaffAuthCheckFailure;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Action } from '@ngrx/store';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
DaffLoginInfo,
|
|
5
|
+
DaffAuthToken,
|
|
6
|
+
} from '@daffodil/auth';
|
|
7
|
+
import { DaffStateError } from '@daffodil/core/state';
|
|
8
|
+
|
|
9
|
+
export enum DaffAuthLoginActionTypes {
|
|
10
|
+
LoginAction = '[@daffodil/auth] Login Action',
|
|
11
|
+
LoginSuccessAction = '[@daffodil/auth] Login Success Action',
|
|
12
|
+
LoginFailureAction = '[@daffodil/auth] Login Failure Action',
|
|
13
|
+
LogoutAction = '[@daffodil/auth] Logout Action',
|
|
14
|
+
LogoutSuccessAction = '[@daffodil/auth] Logout Success Action',
|
|
15
|
+
LogoutFailureAction = '[@daffodil/auth] Logout Failure Action',
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* An action triggered to initialize a auth login request.
|
|
20
|
+
*/
|
|
21
|
+
export class DaffAuthLogin<T extends DaffLoginInfo = DaffLoginInfo> implements Action {
|
|
22
|
+
readonly type = DaffAuthLoginActionTypes.LoginAction;
|
|
23
|
+
|
|
24
|
+
constructor(public loginInfo: T) {}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* An action triggered upon a successful auth login.
|
|
29
|
+
*
|
|
30
|
+
* @param token - the customer access token
|
|
31
|
+
*/
|
|
32
|
+
export class DaffAuthLoginSuccess<T extends DaffAuthToken = DaffAuthToken> implements Action {
|
|
33
|
+
readonly type = DaffAuthLoginActionTypes.LoginSuccessAction;
|
|
34
|
+
|
|
35
|
+
constructor(public auth: T) {}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* An action triggered upon a failed auth login request.
|
|
40
|
+
*
|
|
41
|
+
* @param errorMessage - an error message
|
|
42
|
+
*/
|
|
43
|
+
export class DaffAuthLoginFailure implements Action {
|
|
44
|
+
readonly type = DaffAuthLoginActionTypes.LoginFailureAction;
|
|
45
|
+
|
|
46
|
+
constructor(public errorMessage: DaffStateError) {}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* An action triggered to initialize a logout request.
|
|
51
|
+
*/
|
|
52
|
+
export class DaffAuthLogout implements Action {
|
|
53
|
+
readonly type = DaffAuthLoginActionTypes.LogoutAction;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* An action triggered upon a successful logout.
|
|
58
|
+
*
|
|
59
|
+
* @param token - the customer access token
|
|
60
|
+
*/
|
|
61
|
+
export class DaffAuthLogoutSuccess implements Action {
|
|
62
|
+
readonly type = DaffAuthLoginActionTypes.LogoutSuccessAction;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* An action triggered upon a failed logout request.
|
|
67
|
+
*
|
|
68
|
+
* @param errorMessage - an error message
|
|
69
|
+
*/
|
|
70
|
+
export class DaffAuthLogoutFailure implements Action {
|
|
71
|
+
readonly type = DaffAuthLoginActionTypes.LogoutFailureAction;
|
|
72
|
+
|
|
73
|
+
constructor(public errorMessage: DaffStateError) {}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type DaffAuthLoginActions<
|
|
77
|
+
T extends DaffLoginInfo = DaffLoginInfo,
|
|
78
|
+
U extends DaffAuthToken = DaffAuthToken,
|
|
79
|
+
> =
|
|
80
|
+
| DaffAuthLogin<T>
|
|
81
|
+
| DaffAuthLoginSuccess<U>
|
|
82
|
+
| DaffAuthLoginFailure
|
|
83
|
+
| DaffAuthLogout
|
|
84
|
+
| DaffAuthLogoutSuccess
|
|
85
|
+
| DaffAuthLogoutFailure;
|