Skip to content

Commit

Permalink
Merge remote-tracking branch 'auth-module/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Feb 10, 2019
2 parents 2e38c54 + 8e6cd92 commit cd52236
Show file tree
Hide file tree
Showing 8 changed files with 4,570 additions and 3,879 deletions.
13 changes: 13 additions & 0 deletions docs/api/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ this.$auth.login(/* .... */)
.then(() => this.$toast.success('Logged In!'))
```

### `setUserToken(token)`

- Returns: `Promise`

Set the auth token and fetch the user using the new token and current strategy.

> **TIP:** This function can properly set the user after registration
```js
this.$auth.setUserToken(token)
.then(() => this.$toast.success('User set!'))
```

## `logout()`

- Returns: `Promise`
Expand Down
36 changes: 26 additions & 10 deletions lib/core/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,20 @@ export default class Auth {
}
}

// Call mounted for active strategy on initial load
await this.mounted()

// Watch for loggedIn changes only in client side
if (process.browser && this.options.watchLoggedIn) {
this.$storage.watchState('loggedIn', loggedIn => {
if (!routeOption(this.ctx.route, 'auth', false)) {
this.redirect(loggedIn ? 'home' : 'logout')
}
})
try {
// Call mounted for active strategy on initial load
await this.mounted()
} catch (error) {
this.callOnError(error)
} finally {
// Watch for loggedIn changes only in client side
if (process.client && this.options.watchLoggedIn) {
this.$storage.watchState('loggedIn', loggedIn => {
if (!routeOption(this.ctx.route, 'auth', false)) {
this.redirect(loggedIn ? 'home' : 'logout')
}
})
}
}
}

Expand Down Expand Up @@ -152,6 +156,18 @@ export default class Auth {
})
}

setUserToken (token) {
if (!this.strategy.setUserToken) {
this.setToken(this.strategy.name, token)
return Promise.resolve()
}

return Promise.resolve(this.strategy.setUserToken(token)).catch(error => {
this.callOnError(error, { method: 'setUserToken' })
return Promise.reject(error)
})
}

reset () {
if (!this.strategy.reset) {
this.setUser(false)
Expand Down
2 changes: 1 addition & 1 deletion lib/module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function processStrategies (options) {
const schemeSrc = resolveScheme.call(this, strategy._scheme)

if (!schemeSrc) {
// Scheme is mondatory but is invalid
// Scheme is mandatory but is invalid
continue
}

Expand Down
6 changes: 3 additions & 3 deletions lib/providers/auth0.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ module.exports = function auth0 (strategy) {
_scheme: 'oauth2',
authorization_endpoint: `https://${strategy.domain}/authorize`,
userinfo_endpoint: `https://${strategy.domain}/userinfo`,
scope: ['openid', 'profile', 'email'],
});
};
scope: ['openid', 'profile', 'email']
})
}
16 changes: 16 additions & 0 deletions lib/schemes/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ export default class LocalScheme {
return this.fetchUser()
}

async setUserToken (tokenValue) {
// Ditch any leftover local tokens before attempting to log in
await this._logoutLocally()

if (this.options.tokenRequired) {
const token = this.options.tokenType
? this.options.tokenType + ' ' + tokenValue
: tokenValue

this.$auth.setToken(this.name, token)
this._setToken(token)
}

return this.fetchUser()
}

async fetchUser (endpoint) {
// User endpoint is disabled.
if (!this.options.endpoints.user) {
Expand Down
2 changes: 1 addition & 1 deletion lib/schemes/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default class Oauth2Scheme {
};

if (this.options.audience) {
opts.audience = this.options.audience;
opts.audience = this.options.audience
}

this.$auth.$storage.setLocalStorage(this.name + '.state', opts.state)
Expand Down
42 changes: 21 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"heroku-postbuild": "nuxt build examples/demo",
"start": "nuxt start examples/demo",
"dev": "nuxt examples/demo",
"lint": "eslint lib src test",
"lint-fix": "eslint --fix lib src test",
"lint": "eslint lib test",
"lint-fix": "eslint --fix lib test",
"test": "npm run lint && jest",
"release": "standard-version && git push --follow-tags && npm publish"
},
Expand All @@ -41,37 +41,37 @@
"collectCoverage": true
},
"dependencies": {
"@nuxtjs/axios": "^5.3.1",
"boom": "^7.2.0",
"consola": "^1.4.3",
"@nuxtjs/axios": "^5.3.6",
"boom": "^7.3.0",
"consola": "^2.4.0",
"cookie": "^0.3.1",
"dotprop": "^1.0.2",
"js-cookie": "^2.2.0",
"lodash": "^4.17.10"
"lodash": "^4.17.11"
},
"devDependencies": {
"@nuxtjs/toast": "^3.0.0",
"@nuxtjs/toast": "^3.0.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.5",
"body-parser": "^1.18.3",
"bootstrap-vue": "^2.0.0-rc.11",
"codecov": "^3.0.4",
"codecov": "^3.1.0",
"cookie-parser": "^1.4.3",
"eslint": "^5.4.0",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jest": "^21.21.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-promise": "^4.0.0",
"eslint-plugin-standard": "^3.1.0",
"eslint-plugin-vue": "^4.7.1",
"express": "^4.16.3",
"eslint": "^5.13.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jest": "^22.2.2",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-vue": "^5.1.0",
"express": "^4.16.4",
"express-jwt": "^5.3.1",
"jest": "^23.5.0",
"jsdom": "^12.0.0",
"jest": "^24.1.0",
"jsdom": "^13.2.0",
"nuxt-class-component": "^1.2.0",
"nuxt-edge": "^2.0.0-25582697.225",
"puppeteer": "^1.7.0",
"nuxt-edge": "^2.5.0-25829307.e083c38f",
"puppeteer": "^1.12.2",
"standard-version": "^4.4.0"
}
}
Loading

0 comments on commit cd52236

Please sign in to comment.