Skip to content

Commit 6cb047e

Browse files
committed
fix(auth0): WIP auth0 update to lock 11, auth0 9
1 parent 5469044 commit 6cb047e

File tree

3 files changed

+198
-47
lines changed

3 files changed

+198
-47
lines changed

lib/common/user/Auth0Manager.js

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getConfigProperty, getComponentMessages, getMessage } from '../util/con
88

99
const clientID = process.env.AUTH0_CLIENT_ID
1010
const domain = process.env.AUTH0_DOMAIN
11-
const auth0client = new auth0.WebAuth({ domain, clientID })
11+
const auth0Client = new auth0.WebAuth({ domain, clientID })
1212

1313
class Auth0Manager {
1414
/**
@@ -23,6 +23,8 @@ class Auth0Manager {
2323
this.lock = new Auth0Lock(
2424
clientID,
2525
domain,
26+
// NOTE: The `this.lockOptions` variable is meant to be injected in a
27+
// testing environment.
2628
this.lockOptions || {
2729
allowSignUp: false,
2830
auth: {
@@ -47,20 +49,27 @@ class Auth0Manager {
4749
return this.lock
4850
}
4951

50-
getProfileFromToken (idToken) {
52+
getProfileFromToken (accessToken) {
5153
return new Promise((resolve, reject) => {
52-
this.getLock().getProfile(idToken, (err, profile) => {
54+
this.getLock().getUserInfo(accessToken, (err, profile) => {
5355
if (err) reject(err)
5456
else resolve(profile)
5557
})
5658
})
5759
}
5860

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 () {
6166
return window.localStorage.getItem('userToken')
6267
}
6368

69+
getIdToken () {
70+
return window.localStorage.getItem('idToken')
71+
}
72+
6473
hideLock () {
6574
this.getLock().hide()
6675
}
@@ -76,6 +85,9 @@ class Auth0Manager {
7685
* The string of the route to redirect to upon login success
7786
*/
7887
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
7991
const lock = this.getLock()
8092

8193
let hideFn = onHide
@@ -139,9 +151,10 @@ class Auth0Manager {
139151
}) {
140152
if (!authResult) return receiveTokenAndProfile()
141153

142-
this.setToken(authResult.idToken)
154+
this.setAccessToken(authResult.accessToken)
155+
this.setIdToken(authResult.idToken)
143156

144-
return this.getProfileFromToken(authResult.idToken)
157+
return this.getProfileFromToken(authResult.accessToken)
145158
.then((profile) => {
146159
const actions = [
147160
receiveTokenAndProfile({
@@ -159,7 +172,7 @@ class Auth0Manager {
159172
return actions
160173
})
161174
.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)
163176
this.removeToken()
164177
return receiveTokenAndProfile()
165178
})
@@ -177,7 +190,7 @@ class Auth0Manager {
177190
renewAuth () {
178191
return new Promise((resolve, reject) => {
179192
const nonce = uuidv4()
180-
auth0client.renewAuth({
193+
auth0Client.renewAuth({
181194
audience: '',
182195
nonce,
183196
postMessageDataType: 'auth0:silent-authentication',
@@ -188,11 +201,11 @@ class Auth0Manager {
188201
if (err) {
189202
console.log('Failed to renew log in.')
190203
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')
193206
console.log(authResult)
194207
reject(err)
195-
} else if (!nonceMathces(authResult.idToken, nonce)) {
208+
} else if (!nonceMathces(authResult.accessToken, nonce)) {
196209
const err = new Error('Nonce string does not match!')
197210
reject(err)
198211
} else {
@@ -220,7 +233,7 @@ class Auth0Manager {
220233
userIsLoggedIn
221234
}) {
222235
// Get the user token if we've saved it in localStorage before
223-
const userToken = this.getToken()
236+
const userToken = this.getIdToken()
224237

225238
if (userToken) {
226239
// user has logged in before
@@ -246,9 +259,8 @@ class Auth0Manager {
246259
})
247260
)
248261
} 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.
252264
const profileRefreshTime = getConfigProperty('application.profile_refresh_time')
253265
if (
254266
userIsLoggedIn &&
@@ -264,7 +276,9 @@ class Auth0Manager {
264276

265277
this.isTryingToGetProfileFromToken = true
266278

267-
return this.getProfileFromToken(userToken)
279+
const accessToken = this.getAccessToken()
280+
if (!accessToken) return logout(userIsLoggedIn)
281+
return this.getProfileFromToken(accessToken)
268282
.then((profile) => {
269283
this.isTryingToGetProfileFromToken = false
270284
return receiveTokenAndProfile({
@@ -291,9 +305,13 @@ class Auth0Manager {
291305
})
292306
}
293307

294-
setToken (token) {
308+
setAccessToken (token) {
295309
window.localStorage.setItem('userToken', token)
296310
}
311+
312+
setIdToken (token) {
313+
window.localStorage.setItem('idToken', token)
314+
}
297315
}
298316

299317
function nonceMathces (token, nonce) {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
"@turf/distance": "^5.1.5",
2929
"@turf/line-slice-along": "^4.6.0",
3030
"@turf/nearest-point-on-line": "^5.1.5",
31-
"auth0-js": "^8.8.0",
32-
"auth0-lock": "^10.18.0",
31+
"auth0-js": "^9.3.2",
32+
"auth0-lock": "^11.3.1",
3333
"babel-polyfill": "^6.22.0",
3434
"bootstrap": "^3.3.7",
3535
"camelcase-keys": "^4.2.0",

0 commit comments

Comments
 (0)