Skip to content

Commit a9b6dc5

Browse files
committed
SB-1196 Use InvalidCredentialsAction to notify UI when submitted login credentials are invalid.
1 parent da84ab6 commit a9b6dc5

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

src/app/modules/core/auth/state/session.state.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {AuthenticatedUser, UserAuthority, UserCredentials} from '../auth.model';
1010
import {LoginAction, LogoutAction} from './auth.actions';
1111
import {AuthService} from '../auth.service';
1212

13-
describe('SessionState', () => {
13+
describe('State: SessionState', () => {
1414

1515
describe('selectors should work properly', () => {
1616
it('token() should return a proper value', () => {

src/app/modules/core/auth/state/session.state.ts

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import {catchError, tap} from 'rxjs/operators';
55
import {of} from 'rxjs';
66
import {plainToClass} from 'class-transformer';
77

8-
import {CheckCredentialsAction, CredentialsExpiredAction, LoginAction, LogoutAction, UnauthorizedAccessAction} from './auth.actions';
8+
import {
9+
CheckCredentialsAction,
10+
CredentialsExpiredAction,
11+
InvalidCredentialsAction,
12+
LoginAction,
13+
LogoutAction,
14+
UnauthorizedAccessAction
15+
} from './auth.actions';
916
import {AuthenticatedUser} from '../auth.model';
1017
import {AuthService} from '../auth.service';
1118
import {HideSideNavAction, OpenLoginDialogAction} from '../../../../state/app.actions';
@@ -20,34 +27,38 @@ export interface SessionStateModel {
2027
export const defaultSessionState: SessionStateModel = {
2128
user: null,
2229
token: null,
23-
state: 'guest',
24-
response: null
30+
state: 'guest'
2531
};
2632

27-
let initialState: SessionStateModel = defaultSessionState;
28-
if (localStorage.getItem('session') !== '') {
29-
let session: SessionStateModel = defaultSessionState;
33+
function initialSessionState() {
34+
let state: SessionStateModel = defaultSessionState;
35+
36+
if (localStorage.getItem('session') !== '') {
37+
let session: SessionStateModel = defaultSessionState;
38+
39+
try {
40+
const rawSession: any = JSON.parse(localStorage.getItem('session'));
41+
session = {
42+
user: plainToClass(AuthenticatedUser, rawSession.user) as any as AuthenticatedUser,
43+
token: rawSession.token,
44+
state: rawSession.state
45+
};
46+
} catch (e) {
47+
console.warn('Invalid session found in localStorage.');
48+
session = defaultSessionState;
49+
}
3050

31-
try {
32-
const rawSession: any = JSON.parse(localStorage.getItem('session'));
33-
session = {
34-
user: plainToClass(AuthenticatedUser, rawSession) as any as AuthenticatedUser,
35-
token: rawSession.token,
36-
state: rawSession.state
37-
};
38-
} catch (e) {
39-
console.warn('Invalid session found in localStorage.');
40-
session = defaultSessionState;
51+
if (session !== null && session.token !== '') {
52+
state = session;
53+
}
4154
}
4255

43-
if (session !== null && session.token !== '') {
44-
initialState = session;
45-
}
56+
return state;
4657
}
4758

4859
@State<SessionStateModel>({
4960
name: 'session',
50-
defaults: initialState
61+
defaults: initialSessionState()
5162
})
5263
export class SessionState implements NgxsOnInit {
5364

@@ -151,5 +162,9 @@ export class SessionState implements NgxsOnInit {
151162
this.store.dispatch([new OpenLoginDialogAction(payload)]);
152163
}
153164

165+
@Action(InvalidCredentialsAction)
166+
invalidCredentialsSessionState(ctx: StateContext<SessionStateModel>) {
167+
ctx.patchState({state: 'invalid.credentials'});
168+
}
154169

155170
}

0 commit comments

Comments
 (0)