@@ -8,7 +8,7 @@ import { getConfigProperty, getComponentMessages, getMessage } from '../util/con
8
8
9
9
const clientID = process . env . AUTH0_CLIENT_ID
10
10
const domain = process . env . AUTH0_DOMAIN
11
- const auth0client = new auth0 . WebAuth ( { domain, clientID } )
11
+ const auth0Client = new auth0 . WebAuth ( { domain, clientID } )
12
12
13
13
class Auth0Manager {
14
14
/**
@@ -23,6 +23,8 @@ class Auth0Manager {
23
23
this . lock = new Auth0Lock (
24
24
clientID ,
25
25
domain ,
26
+ // NOTE: The `this.lockOptions` variable is meant to be injected in a
27
+ // testing environment.
26
28
this . lockOptions || {
27
29
allowSignUp : false ,
28
30
auth : {
@@ -47,20 +49,27 @@ class Auth0Manager {
47
49
return this . lock
48
50
}
49
51
50
- getProfileFromToken ( idToken ) {
52
+ getProfileFromToken ( accessToken ) {
51
53
return new Promise ( ( resolve , reject ) => {
52
- this . getLock ( ) . getProfile ( idToken , ( err , profile ) => {
54
+ this . getLock ( ) . getUserInfo ( accessToken , ( err , profile ) => {
53
55
if ( err ) reject ( err )
54
56
else resolve ( profile )
55
57
} )
56
58
} )
57
59
}
58
60
59
- getToken ( ) {
60
- // Retrieves the user token from localStorage
61
+ /**
62
+ * Retrieves the user token from localStorage.
63
+ * @return {String } auth0 user token
64
+ */
65
+ getAccessToken ( ) {
61
66
return window . localStorage . getItem ( 'userToken' )
62
67
}
63
68
69
+ getIdToken ( ) {
70
+ return window . localStorage . getItem ( 'idToken' )
71
+ }
72
+
64
73
hideLock ( ) {
65
74
this . getLock ( ) . hide ( )
66
75
}
@@ -76,6 +85,9 @@ class Auth0Manager {
76
85
* The string of the route to redirect to upon login success
77
86
*/
78
87
loginWithLock ( { onHide = ( ) => { } , push, receiveTokenAndProfile, redirectOnSuccess = false } ) {
88
+ // Store location for location return after callback
89
+ window . localStorage . setItem ( 'randomStateValue' , window . location . href )
90
+ // FIXME: redirect to /login
79
91
const lock = this . getLock ( )
80
92
81
93
let hideFn = onHide
@@ -139,9 +151,10 @@ class Auth0Manager {
139
151
} ) {
140
152
if ( ! authResult ) return receiveTokenAndProfile ( )
141
153
142
- this . setToken ( authResult . idToken )
154
+ this . setAccessToken ( authResult . accessToken )
155
+ this . setIdToken ( authResult . idToken )
143
156
144
- return this . getProfileFromToken ( authResult . idToken )
157
+ return this . getProfileFromToken ( authResult . accessToken )
145
158
. then ( ( profile ) => {
146
159
const actions = [
147
160
receiveTokenAndProfile ( {
@@ -159,7 +172,7 @@ class Auth0Manager {
159
172
return actions
160
173
} )
161
174
. catch ( ( err ) => {
162
- console . error ( 'an error occurred while trying to get the user profile' , err )
175
+ console . error ( 'An error occurred while trying to get the user profile' , err )
163
176
this . removeToken ( )
164
177
return receiveTokenAndProfile ( )
165
178
} )
@@ -177,7 +190,7 @@ class Auth0Manager {
177
190
renewAuth ( ) {
178
191
return new Promise ( ( resolve , reject ) => {
179
192
const nonce = uuidv4 ( )
180
- auth0client . renewAuth ( {
193
+ auth0Client . renewAuth ( {
181
194
audience : '' ,
182
195
nonce,
183
196
postMessageDataType : 'auth0:silent-authentication' ,
@@ -188,11 +201,11 @@ class Auth0Manager {
188
201
if ( err ) {
189
202
console . log ( 'Failed to renew log in.' )
190
203
reject ( err )
191
- } else if ( ! authResult . idToken ) {
192
- const err = new Error ( 'idToken not received from auth0' )
204
+ } else if ( ! authResult . accessToken ) {
205
+ const err = new Error ( 'accessToken not received from auth0' )
193
206
console . log ( authResult )
194
207
reject ( err )
195
- } else if ( ! nonceMathces ( authResult . idToken , nonce ) ) {
208
+ } else if ( ! nonceMathces ( authResult . accessToken , nonce ) ) {
196
209
const err = new Error ( 'Nonce string does not match!' )
197
210
reject ( err )
198
211
} else {
@@ -220,7 +233,7 @@ class Auth0Manager {
220
233
userIsLoggedIn
221
234
} ) {
222
235
// Get the user token if we've saved it in localStorage before
223
- const userToken = this . getToken ( )
236
+ const userToken = this . getIdToken ( )
224
237
225
238
if ( userToken ) {
226
239
// user has logged in before
@@ -246,9 +259,8 @@ class Auth0Manager {
246
259
} )
247
260
)
248
261
} else {
249
- // token is still valid
250
-
251
- // see if it's been long enough to try again
262
+ // Token is still valid.
263
+ // Check that enough time has passed to try again.
252
264
const profileRefreshTime = getConfigProperty ( 'application.profile_refresh_time' )
253
265
if (
254
266
userIsLoggedIn &&
@@ -264,7 +276,9 @@ class Auth0Manager {
264
276
265
277
this . isTryingToGetProfileFromToken = true
266
278
267
- return this . getProfileFromToken ( userToken )
279
+ const accessToken = this . getAccessToken ( )
280
+ if ( ! accessToken ) return logout ( userIsLoggedIn )
281
+ return this . getProfileFromToken ( accessToken )
268
282
. then ( ( profile ) => {
269
283
this . isTryingToGetProfileFromToken = false
270
284
return receiveTokenAndProfile ( {
@@ -291,9 +305,13 @@ class Auth0Manager {
291
305
} )
292
306
}
293
307
294
- setToken ( token ) {
308
+ setAccessToken ( token ) {
295
309
window . localStorage . setItem ( 'userToken' , token )
296
310
}
311
+
312
+ setIdToken ( token ) {
313
+ window . localStorage . setItem ( 'idToken' , token )
314
+ }
297
315
}
298
316
299
317
function nonceMathces ( token , nonce ) {
0 commit comments