Skip to content

Commit c38a1e4

Browse files
author
Pooya Parsa
committed
feat: add auth0-js scheme
1 parent ec4ee25 commit c38a1e4

File tree

12 files changed

+473
-79
lines changed

12 files changed

+473
-79
lines changed

examples/api/auth.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ app.post('/login', (req, res, next) => {
3434
const accessToken = jsonwebtoken.sign(
3535
{
3636
username,
37-
rand: Math.random() * 1000,
37+
picture: 'https://github.com/nuxt.png',
38+
name: 'User ' + username,
3839
scope: ['test', 'user']
3940
},
4041
'dummy'

examples/demo/layouts/default.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
</b-navbar-nav>
1313
<b-navbar-nav class="ml-auto">
1414
<template v-if="$auth.state.loggedIn">
15-
<b-nav-item-dropdown :text="$auth.state.user.username" right>
15+
<b-nav-item-dropdown :text="$auth.state.user.name" right>
1616
<b-dropdown-item @click="$auth.logout()">Logout</b-dropdown-item>
1717
</b-nav-item-dropdown>
18+
<b-img :src="$auth.state.user.picture" class="mt-1" rounded="circle" width="30px" height="30px" />
1819
</template>
1920
<template v-else>
2021
<b-dropdown-item to="/login">Login</b-dropdown-item>

examples/demo/nuxt.config.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,28 @@ module.exports = {
1111
extractCSS: true
1212
},
1313
serverMiddleware: ['../api/auth'],
14+
modules: [
15+
'bootstrap-vue/nuxt',
16+
'@nuxtjs/axios',
17+
'@@'
18+
],
19+
axios: {
20+
proxy: true
21+
},
22+
proxy: {
23+
'/api': 'http://localhost:3000'
24+
},
1425
auth: {
1526
strategies: {
1627
local: {
1728
endpoints: {
1829
login: { propertyName: 'token.accessToken' }
1930
}
31+
},
32+
auth0: {
33+
clientID: 'q8lDHfBLJ-Fsziu7bf351OcYQAIe3UJv',
34+
domain: 'nuxt-auth.auth0.com'
2035
}
2136
}
22-
},
23-
modules: ['bootstrap-vue/nuxt', '@nuxtjs/axios', '@@'],
24-
axios: {
25-
proxy: true
26-
},
27-
proxy: {
28-
'/api': 'http://localhost:3000'
2937
}
3038
}

examples/demo/pages/login.vue

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
</b-form-group>
1717

1818
<div class="text-center">
19-
<b-btn @click="login" variant="outline-primary" size="lg">Login</b-btn>
19+
<b-btn-group>
20+
<b-btn @click="login" variant="outline-primary" size="lg">Login</b-btn>
21+
<b-btn @click="$auth.strategies.auth0.login()" variant="outline-primary" size="lg">Login with Auth0</b-btn>
22+
</b-btn-group>
2023
</div>
2124
</form>
2225
</div>
@@ -42,7 +45,7 @@ export default {
4245
},
4346
methods: {
4447
async login() {
45-
return this.$auth.login({
48+
return this.$auth.strategies.local.login({
4649
data: {
4750
username: this.username,
4851
password: this.password

lib/auth/auth.js

+74-52
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Cookies from 'js-cookie'
22
import { parse as parseCookie } from 'cookie'
33
import getProp from 'dotprop'
44
import Vue from 'vue'
5-
import { routeOption, isRelativeURL, isUnset, isSameURL } from './utilities'
5+
import { routeOption, isRelativeURL, isUnset, isSet, isSameURL } from './utilities'
66

77
export default class Auth {
88
constructor (ctx, options) {
@@ -24,21 +24,16 @@ export default class Auth {
2424
// Register vuex store
2525
this._registerVuexStore()
2626

27-
// Reset on error
28-
if (this.options.resetOnError) {
29-
this._resetOnError()
30-
}
31-
3227
// Watch for loggedIn changes only in client side
3328
if (process.browser) {
3429
this._autoRedirect()
3530
}
3631

37-
// Sync token
38-
this.syncToken()
32+
// Restore strategy
33+
this.syncUniversal('strategy', this.options.defaultStrategy)
3934

40-
// Set defaultStrategy
41-
return this.setStrategy()
35+
// Call mounted for active strategy on initial load
36+
return this.mounted()
4237
}
4338

4439
// ---------------------------------------------------------------
@@ -65,12 +60,6 @@ export default class Auth {
6560
})
6661
}
6762

68-
_resetOnError () {
69-
this.onError(() => {
70-
this.reset()
71-
})
72-
}
73-
7463
_autoRedirect () {
7564
this.watchState('loggedIn', loggedIn => {
7665
if (!routeOption(this.ctx.route, 'auth', false)) {
@@ -85,38 +74,42 @@ export default class Auth {
8574

8675
// ...Universal
8776

88-
setUniversal (key, value) {
77+
setUniversal (key, value, isJson) {
8978
// Local state
9079
this.setState(key, value)
9180

9281
// Cookies
9382
this.setCookie(key, value)
9483

9584
// Local Storage
96-
this.setLocalStorage(key, value)
85+
this.setLocalStorage(key, value, isJson)
9786
}
9887

99-
getUniversal (key) {
88+
getUniversal (key, isJson) {
10089
// Local state
10190
let value = this.getState(key)
10291

10392
// Cookies
10493
if (isUnset(value)) {
105-
value = this.getCookie(key)
94+
value = this.getCookie(key, isJson)
10695
}
10796

10897
// Local Storage
10998
if (isUnset(value)) {
110-
value = this.getLocalStorage(key)
99+
value = this.getLocalStorage(key, isJson)
111100
}
112101

113102
return value
114103
}
115104

116-
syncUniversal (key) {
117-
const value = this.getUniversal(key)
105+
syncUniversal (key, defaultValue, isJson) {
106+
let value = this.getUniversal(key, isJson)
118107

119-
if (!isUnset(value)) {
108+
if (isUnset(value) && isSet(defaultValue)) {
109+
value = defaultValue
110+
}
111+
112+
if (isSet(value)) {
120113
this.setUniversal(key, value)
121114
}
122115

@@ -154,19 +147,20 @@ export default class Auth {
154147

155148
// ...Local Storage
156149

157-
setLocalStorage (key, value) {
150+
setLocalStorage (key, value, isJson) {
158151
if (typeof localStorage !== 'undefined') {
159152
if (isUnset(value)) {
160153
localStorage.removeItem(key)
161154
} else {
162-
localStorage.setItem(key, value)
155+
localStorage.setItem(key, isJson ? JSON.stringify(value) : value)
163156
}
164157
}
165158
}
166159

167-
getLocalStorage (key) {
160+
getLocalStorage (key, isJson) {
168161
if (typeof localStorage !== 'undefined') {
169-
return localStorage.getItem(key)
162+
const value = localStorage.getItem(key)
163+
return isJson ? JSON.parse(value) : value
170164
}
171165
}
172166

@@ -186,7 +180,7 @@ export default class Auth {
186180
}
187181
}
188182

189-
getCookie (key) {
183+
getCookie (key, isJson) {
190184
if (!this.options.cookie) {
191185
return
192186
}
@@ -196,8 +190,9 @@ export default class Auth {
196190
: this.ctx.req.headers.cookie
197191

198192
const cookies = parseCookie(cookieStr || '') || {}
193+
const value = cookies[key]
199194

200-
return cookies[key]
195+
return isJson ? JSON.parse(value) : value
201196
}
202197

203198
// ---------------------------------------------------------------
@@ -213,41 +208,74 @@ export default class Auth {
213208
}
214209

215210
setStrategy (name) {
216-
if (name === undefined) {
217-
name = this.options.defaultStrategy
218-
}
219-
220-
const oldName = this.getState('strategy')
221-
222-
if (oldName === name) {
211+
if (name === this.getUniversal('strategy')) {
223212
return Promise.resolve()
224213
}
225214

226-
// Update state
227-
this.setState('strategy', name)
215+
// Call to reset
216+
this.reset()
217+
218+
// Set strategy
219+
this.setUniversal('strategy', name)
228220

229221
// Call mounted hook on active strategy
230-
return this._mounted()
222+
return this.mounted()
231223
}
232224

233225
// ---------------------------------------------------------------
234-
// Scheme interface wrappers
226+
// Scheme interface wrappers and default handlers
235227
// ---------------------------------------------------------------
236228

237-
_mounted () {
238-
return this.strategy.mounted(...arguments)
229+
mounted () {
230+
if (this.strategy.mounted) {
231+
return Promise.resolve(this.strategy.mounted(...arguments)).then(() => this.fetchUserOnce())
232+
}
233+
234+
return this.fetchUserOnce()
239235
}
240236

241237
login () {
242-
return this.strategy.login(...arguments)
238+
if (this.strategy.login) {
239+
return Promise.resolve(this.strategy.login(...arguments))
240+
}
241+
242+
return Promise.resolve()
243243
}
244244

245245
fetchUser () {
246-
return this.strategy.fetchUser(...arguments)
246+
if (this.strategy.fetchUser) {
247+
return Promise.resolve(this.strategy.fetchUser(...arguments))
248+
}
249+
250+
return Promise.resolve()
247251
}
248252

249253
logout () {
250-
return this.strategy.logout(...arguments)
254+
if (this.strategy.logout) {
255+
return Promise.resolve(this.strategy.logout(...arguments)).then(() => this.reset())
256+
}
257+
258+
this.reset()
259+
260+
return Promise.resolve()
261+
}
262+
263+
fetchUserOnce () {
264+
if (!this.state.user) {
265+
return this.fetchUser(...arguments)
266+
}
267+
268+
return Promise.resolve()
269+
}
270+
271+
reset () {
272+
if (this.strategy.reset) {
273+
this.strategy.reset(...arguments)
274+
}
275+
276+
this.setState('loggedIn', false)
277+
this.setState('user', null)
278+
this.setToken(null)
251279
}
252280

253281
// ---------------------------------------------------------------
@@ -329,12 +357,6 @@ export default class Auth {
329357
}
330358
}
331359

332-
reset () {
333-
this.setState('loggedIn', false)
334-
this.setState('user', null)
335-
this.setToken(null)
336-
}
337-
338360
redirect (name) {
339361
if (!this.options.redirect) {
340362
return

lib/auth/plugin.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import Auth from './auth'
22
import './middleware'
33

44
// Active chemes
5-
<% for (let scheme of options.schemes) { %>import <%= 'scheme_' + hash(scheme) %> from '<%= scheme %>'<% } %>
5+
<% for (let scheme of options.schemes) { %> import <%= 'scheme_' + hash(scheme) %> from '<%= scheme %>'
6+
<% } %>
67

78
export default function (ctx, inject) {
89
// Options
@@ -15,8 +16,8 @@ export default function (ctx, inject) {
1516
inject('auth', $auth)
1617

1718
// Register strategies
18-
<% for (let strategyName in options.strategies) { const strategy = options.strategies[strategyName] %>
19-
$auth.registerStrategy('<%= strategyName %>', new <%='scheme_' + hash(strategy._scheme)%>($auth, <%= JSON.stringify(strategy) %>))
19+
<% for (let strategy of options.strategies) { %>
20+
$auth.registerStrategy('<%= strategy._name %>', new <%='scheme_' + hash(strategy._scheme) %> ($auth, <%= JSON.stringify(strategy) %>))
2021
<% } %>
2122

2223
// Initialize auth

0 commit comments

Comments
 (0)