This repository has been archived by the owner on Sep 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
auth.js
198 lines (170 loc) · 4.29 KB
/
auth.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* @flow
*/
import { remote } from 'electron';
import { User as UserManager } from 'xdl';
import { asyncAction, reduceAsync, composeReducers } from 'xde/state/utils';
import type { AppAction, AppDispatch, AppState } from 'xde/state/types';
import type { UserOrLegacyUser, RegistrationData } from 'xdl/build/User';
// const XDE_CLIENT_ID = '2Gej4rsLcTCRuhKKzn8xkAHsD1kiPAGS';
UserManager.initialize();
/** Action Types **/
export type ActionTypes = 'CHECK_SESSION' | 'REGISTER' | 'LOGIN' | 'FORGOT_PASSWORD' | 'LOGOUT';
/** Actions **/
export type LoginType = 'user-pass' | 'github';
export const actions = {
checkForExistingSession: () =>
asyncAction(
'CHECK_SESSION',
async () => {
const user = await UserManager.getCurrentUserAsync();
if (!user) {
throw new Error('');
}
return { user };
},
(actionType, payload) => ({
...payload,
loginType: 'user-pass',
})
),
login: (loginType: LoginType, loginArgs?: { username: string, password: string }) =>
asyncAction(
'LOGIN',
async () => {
const currentWindow = remote.getCurrentWindow();
try {
const user = await UserManager.loginAsync(loginType, loginArgs);
currentWindow.show();
return {
user,
};
} catch (e) {
currentWindow.show();
throw e;
}
},
(actionType, payload) => ({
...payload,
loginType,
})
),
logout: () => asyncAction('LOGOUT', async () => await UserManager.logoutAsync()),
forgotPassword: (usernameOrEmail: string) =>
asyncAction('FORGOT_PASSWORD', async () => {
let result = await UserManager.forgotPasswordAsync(usernameOrEmail);
if (result) {
// Oddly, this string includes double quotes around it
alert(result.replace(/"/g, ''));
}
return result;
}),
register: (userData: RegistrationData) =>
asyncAction(
'REGISTER',
async (dispatch: AppDispatch, getState: () => AppState) => {
const state = getState();
const user = await UserManager.registerAsync(userData, state.auth.user);
return {
user,
};
},
(actionType, payload) => ({
...payload,
loginType: 'user-pass',
})
),
};
/** Reducers **/
type State = {
loginType: ?LoginType,
pendingAction: ?ActionTypes,
error: ?string,
authenticated: boolean,
user: ?UserOrLegacyUser,
};
const initialState: State = {
loginType: null,
pendingAction: null,
error: null,
authenticated: false,
user: null,
};
type LoginAction = AppAction & {
payload: {
loginType: LoginType,
pending?: ActionTypes,
user: UserOrLegacyUser,
error?: Error,
},
};
function authPending(state: State, action: LoginAction): State {
return {
...state,
pendingAction: action.payload.pending,
loginType: action.payload.loginType,
error: null,
};
}
function authComplete(state: State, action: LoginAction): State {
const { user } = action.payload;
return {
...state,
pendingAction: null,
error: null,
authenticated: !!user.sessionSecret,
user,
};
}
function authFailed(state: State, action: LoginAction): State {
return {
...state,
pendingAction: null,
error: _transformError(action.payload.error),
};
}
export const reducer = composeReducers(
[
reduceAsync('CHECK_SESSION', {
pending: authPending,
complete: authComplete,
failed: authFailed,
}),
reduceAsync('REGISTER', {
pending: authPending,
complete: authComplete,
failed: authFailed,
}),
reduceAsync('LOGIN', {
pending: authPending,
complete: authComplete,
failed: authFailed,
}),
reduceAsync('LOGOUT', {
complete: (state: State, action: LoginAction) => _resetToInitial(),
}),
],
initialState
);
function _resetToInitial() {
return {
...initialState,
};
}
type ErrorWithCode = Error & {
code?: string,
};
const _transformError = (err: ?ErrorWithCode) => {
if (!err) {
return null;
}
if (err.code) {
switch (err.code) {
case 'invalid_user_password':
return `Invalid username/password. Please try again!`;
default:
return `${err.message}`;
}
}
return err.message;
};