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