-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
auth.state.ts
129 lines (118 loc) · 3.45 KB
/
auth.state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { NgZone } from '@angular/core';
import { Action, Select, Selector, State, StateContext } from '@ngxs/store';
import {
Login,
LoginCanceled,
AuthMode,
ChangeAuthMode,
LogoutSuccess,
LoginSuccess,
Logout,
AuthModeChanged,
} from './auth.actions';
import { AuthService } from './auth.service';
import { Router } from '@angular/router';
import { authConfigCodeFlow, authConfigHybridFlow, authConfigImplicit, authConfigPassword } from './oauth.config';
import { OAuthService } from 'angular-oauth2-oidc';
import { map } from 'rxjs/operators';
export interface AuthStateModel {
isLoggedIn: boolean;
profile: any;
authMode: AuthMode;
}
/** @dynamic */
@State<AuthStateModel>({
name: 'auth',
defaults: {
isLoggedIn: false,
profile: {},
authMode: AuthMode.ImplicitFLow,
},
})
export class AuthState {
constructor(
private authService: AuthService,
private oauthService: OAuthService,
private router: Router,
private zone: NgZone,
) {}
@Selector()
static isLoggedIn(state: AuthStateModel) {
return state.isLoggedIn;
}
@Selector()
static profile(state: AuthStateModel) {
return state.profile;
}
@Selector()
static authMode(state: AuthStateModel) {
return state ? state.authMode : AuthMode.ImplicitFLow;
}
@Action(LoginSuccess)
loginSuccess({ getState, patchState }: StateContext<AuthStateModel>, { payload }: LoginSuccess) {
patchState({
isLoggedIn: true,
profile: payload,
});
this.authService.startAutoRefreshToken();
if (getState().authMode === AuthMode.PasswordFlow) {
this.zone.run(() => this.router.navigate(['/dashboard']));
}
}
@Action([LogoutSuccess, LoginCanceled])
logoutSuccess({ getState, setState }: StateContext<AuthStateModel>) {
setState({
isLoggedIn: false,
profile: {},
authMode: getState().authMode,
});
this.authService.stopAutoRefreshToken();
this.zone.run(() => this.router.navigate(['/home']));
}
@Action(AuthModeChanged)
authModeChanged({ getState, patchState }: StateContext<AuthStateModel>, { payload }: AuthModeChanged) {
patchState({
authMode: payload,
});
}
@Action(ChangeAuthMode)
async changeAuthMode({ getState, dispatch }: StateContext<AuthStateModel>, { payload }: ChangeAuthMode) {
if (getState().authMode !== payload) {
switch (payload) {
case AuthMode.PasswordFlow:
this.oauthService.configure(authConfigPassword);
break;
case AuthMode.CodeFLow:
this.oauthService.configure(authConfigCodeFlow);
break;
case AuthMode.HybridFlow:
this.oauthService.configure(authConfigHybridFlow);
break;
default:
this.oauthService.configure(authConfigImplicit);
break;
}
await this.oauthService.loadDiscoveryDocument();
dispatch(new AuthModeChanged(payload));
}
}
@Action(Logout)
logout({ getState }: StateContext<AuthStateModel>) {
return this.authService.logout();
}
@Action(Login)
login({ getState, dispatch }: StateContext<AuthStateModel>, { payload }: Login) {
// HINT: don't escape form zone https://github.com/angular/material2/issues/13640
return this.zone.run(() =>
this.authService.login(payload).pipe(
map(profile => {
if (profile === false) {
dispatch(new LoginCanceled());
} else {
dispatch(new LoginSuccess(profile));
}
}),
),
);
}
}