-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathapp.js
More file actions
141 lines (118 loc) · 4.42 KB
/
app.js
File metadata and controls
141 lines (118 loc) · 4.42 KB
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
import { EventEmitter } from 'events';
import { LocalStorage } from './storage';
import { StormpathCookieUserService, OAuthTokenUserService} from './services';
import { UserConstants, TokenConstants } from './constants';
import { UserStore, SessionStore, TokenStore } from './stores';
import { FluxDispatcher, ReduxDispatcher } from './dispatchers';
import utils from './utils';
import context from './context';
class App extends EventEmitter {
constructor() {
super();
this.initialized = false;
}
isInitialized() {
return this.initialized;
}
init(options) {
options = options || {};
if (this.isInitialized()) {
throw new Error('React Stormpath already initialized.');
}
this.initialized = true;
let tokenStore = null;
let userService = null;
let sessionStore = new SessionStore();
if (!options.endpoints) {
options.endpoints = {};
}
if (!options.storage) {
options.storage = new LocalStorage('session');
}
let baseUri = options.endpoints.baseUri;
if (options.tokenStrategy === 'cookie') {
userService = new StormpathCookieUserService(options.endpoints);
} else {
tokenStore = new TokenStore(options.storage, 'stormpath:token');
userService = new OAuthTokenUserService(options.endpoints);
userService.setToken('access_token', tokenStore.get('access_token'));
userService.setToken('refresh_token', tokenStore.get('refresh_token'));
}
let userStore = new UserStore(userService, sessionStore);
// If there is a jwtResponse in the URL, it's from a social login callback
// from the Client API, so if we have a configured baseUrl, we need to authenticate
// with this JWT, using the Client API.
if (baseUri && window.location.href.match(/jwtResponse/)) {
userService.authenticate({
grant_type: 'stormpath_token',
token: utils.parseQueryString(window.location.href.split('?')[1]).jwtResponse
}, (err) => {
if (err) {
return console.error(err);
}
window.location.replace(window.location.href.replace(/jwtResponse=[^&]+/,''));
});
}
context.setTokenStore(tokenStore);
context.setSessionStore(sessionStore);
context.setUserStore(userStore);
// If there's no specified dispatcher, then default to flux.
let dispatcher = options.dispatcher || { type: 'flux' };
let appReducer = (payload) => {
switch(payload.type) {
case UserConstants.USER_LOGIN:
userStore.login(payload.options, payload.callback);
break;
case UserConstants.USER_LOGOUT:
userStore.logout(payload.callback);
break;
case UserConstants.USER_REGISTER:
userStore.register(payload.options, payload.callback);
break;
case UserConstants.USER_FORGOT_PASSWORD:
userStore.forgotPassword(payload.options, payload.callback);
break;
case UserConstants.USER_CHANGE_PASSWORD:
userStore.changePassword(payload.options, payload.callback);
break;
case UserConstants.USER_UPDATE_PROFILE:
userStore.updateProfile(payload.options.data, payload.callback);
break;
case UserConstants.USER_VERIFY_EMAIL:
userStore.verifyEmail(payload.options.spToken, payload.callback);
break;
case TokenConstants.TOKEN_SET:
userService.setToken(payload.options.type, payload.options.token);
if (payload.options.token !== null) {
tokenStore
.set(payload.options.type, payload.options.token)
.then(() => payload.callback && payload.callback());
} else {
tokenStore
.reset(payload.options.type)
.then(() => payload.callback && payload.callback());
}
break;
case TokenConstants.TOKEN_REFRESH:
userService.refreshToken(payload.options.token, payload.callback);
break;
}
return true;
};
switch (dispatcher.type) {
case 'flux':
dispatcher = new FluxDispatcher(appReducer);
break;
case 'redux':
dispatcher = new ReduxDispatcher(appReducer, dispatcher.store);
break;
default:
throw new Error('Stormpath SDK: Invalid dispatcher type ' + dispatcher.type);
}
context.setDispatcher(dispatcher);
}
getAccessToken() {
return context.userStore.getAccessToken();
}
}
export default new App()