/
sd-login-actions.ts
executable file
·137 lines (121 loc) · 5.36 KB
/
sd-login-actions.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
130
131
132
133
134
135
136
137
import { ProfileModel } from './../models/profile.model';
import { AppState, SwissdataUser } from './interfaces';
import { getLogger, Logger } from 'aurelia-logging';
let log: Logger = getLogger('sd-login-actions');
export function reset(state: AppState) {
const newState = Object.assign({}, state);
if (newState.sdlogin && newState.sdlogin.username) delete newState.sdlogin.username;
if (newState.sdlogin && newState.sdlogin.createAccountValidationToken) delete newState.sdlogin.createAccountValidationToken;
if (newState.sdlogin && newState.sdlogin.requireDoubleAuthValidation) delete newState.sdlogin.requireDoubleAuthValidation;
if (newState.sdlogin && newState.sdlogin.doubleAuthValidationToken) delete newState.sdlogin.doubleAuthValidationToken;
return newState;
}
export function setUsername(state: AppState, username: string) {
const newState = Object.assign({}, state);
if (!newState.sdlogin) newState.sdlogin = {};
newState.sdlogin.username = username
return newState;
}
export function passwordStep(state: AppState, username: string) {
const newState = Object.assign({}, state);
if (!newState.sdlogin) newState.sdlogin = {};
newState.sdlogin.username = username
return newState;
}
export function validateAccountStep(state: AppState, token: string, tokenExpiry?: string) {
const newState = Object.assign({}, state);
newState.sdlogin.createAccountValidationToken = token;
newState.sdlogin.createAccountValidationTokenExpiry = tokenExpiry;
return newState;
}
export function doubleAuthStep(state: AppState, token: string) {
const newState = Object.assign({}, state);
newState.sdlogin.doubleAuthValidationToken = token;
newState.sdlogin.requireDoubleAuthValidation = true;
return newState;
}
export function setAccessToken(state: AppState, accessToken: string) {
const newState = Object.assign({}, state);
newState.swissdata.accessToken = accessToken;
newState.swissdata.requireDoubleAuthValidation = false;
return newState;
}
export function authenticate(state: AppState, user: SwissdataUser, accessToken?: string, profile?: ProfileModel) {
const newState = Object.assign({}, state);
newState.swissdata.authenticated = true;
newState.swissdata.user = user;
newState.sdlogin.doubleAuthValidationToken = '';
newState.swissdata.requireDoubleAuthValidation = false;
if (accessToken) newState.swissdata.accessToken = accessToken;
if (profile) newState.swissdata.profile = profile;
return newState;
}
export function setCurrentProfile(state: AppState, profile: ProfileModel) {
const newState = Object.assign({}, state);
newState.swissdata.profile = profile;
return newState;
}
export function resetPasswordStep(state: AppState, token: string) {
const newState = Object.assign({}, state);
newState.sdlogin.resetPasswordToken = token;
return newState;
}
export function logout(state: AppState) {
const newState = Object.assign({}, state);
newState.swissdata.authenticated = false;
newState.swissdata.user = null;
newState.sdlogin.doubleAuthValidationToken = '';
newState.swissdata.requireDoubleAuthValidation = false;
newState.swissdata.accessToken = null;
newState.swissdata.profile = null;
return newState;
}
export function registerUserId(state: AppState, userId: string, firstname: string, lastname: string, username: string, profileUrl: string) {
const newState = Object.assign({}, state);
if (!newState.sdlogin.accounts) newState.sdlogin.accounts = [];
let index = newState.sdlogin.accounts.map(i => i.userId).indexOf(userId);
if (index !== -1) {
newState.sdlogin.accounts.splice(index, 1);
}
newState.sdlogin.accounts.unshift({
firstname: firstname,
lastname: lastname,
username: username,
userId: userId,
profileUrl: profileUrl,
});
return newState;
}
export function updateRegisteredUserId(state: AppState, userId: string, firstname: string | undefined, lastname: string | undefined, username: string | undefined, profileUrl: string | undefined) {
const newState = Object.assign({}, state);
if (newState.sdlogin.accounts) return newState;
let index = newState.sdlogin.accounts.map(i => i.userId).indexOf(userId);
if (index !== -1) {
if (firstname !== undefined) newState.sdlogin.accounts[index].firstname = firstname;
if (lastname !== undefined) newState.sdlogin.accounts[index].lastname = lastname;
if (username !== undefined) newState.sdlogin.accounts[index].username = username;
if (profileUrl !== undefined) newState.sdlogin.accounts[index].profileUrl = profileUrl;
}
return newState;
}
export function registerCurrentUserId(state: AppState, username: string) {
if (!state.swissdata.authenticated || !state.swissdata.user || !state.swissdata.user.id) return state;
const profileUrl = state.swissdata.profile && state.swissdata.profile.id && state.swissdata.profile.picture ?
`/profile/${state.swissdata.profile.id}?download=picture` : undefined;
return registerUserId(state,
state.swissdata.user.id,
state.swissdata.user.firstname,
state.swissdata.user.lastname,
username,
profileUrl,
);
}
export function removeRegisteredUserId(state: AppState, userId: string) {
const newState = Object.assign({}, state);
if (!newState.sdlogin.accounts) newState.sdlogin.accounts = [];
let index = newState.sdlogin.accounts.map(i => i.userId).indexOf(userId);
if (index !== -1) {
newState.sdlogin.accounts.splice(index, 1);
}
return newState;
}