blob: a159cb6272eaa7ec9fa65f717537574135ee0680 (
plain)
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
|
import { IActionHandler } from '../flux/action';
import { UpdateAuthentication } from '../actions/authentication';
import { User } from '../models/authentication';
import { onLogin, onLogout } from '../services/applicationApi';
export interface IAuthenticationState {
user?: User;
}
const initialToken = localStorage.getItem("userToken");
const authenticationStateInit: IAuthenticationState = {
user: initialToken && User.fromString(initialToken) || undefined
};
export const authenticationStateHandler: IActionHandler<IAuthenticationState> = (state = authenticationStateInit, action) => {
if (action instanceof UpdateAuthentication) {
const user = action.bearerToken && new User(action.bearerToken) || undefined;
if (user) {
localStorage.setItem("userToken", user.toString());
onLogin();
} else {
localStorage.removeItem("userToken");
onLogout();
}
state = {
...state,
user
};
}
return state;
};
|