Skip to content

Commit

Permalink
feat(oauth2): logout support (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevintechie authored Apr 16, 2020
1 parent c6f3539 commit 43eedc7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
10 changes: 7 additions & 3 deletions docs/schemes/oauth2.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ If the refresh token has expired, the token cannot be refreshed. You can find th
The user is logged out and navigated to the **home** page.

#### Client side (Client initiated axios request)
The user is logged out and navigated to the **logout** page, for explicitly explaining what happened.
The user is logged out and navigated to the **logout** page, for explicitly explaining what happened.

## Options

Expand All @@ -33,7 +33,8 @@ auth: {
endpoints: {
authorization: 'https://accounts.google.com/o/oauth2/auth',
token: undefined,
userInfo: 'https://www.googleapis.com/oauth2/v3/userinfo'
userInfo: 'https://www.googleapis.com/oauth2/v3/userinfo',
logout: 'https://example.com/logout'
},
token: {
property: 'access_token',
Expand Down Expand Up @@ -71,9 +72,12 @@ While not a part of oauth2 spec, almost all oauth2 providers expose this endpoin

If using Google code authorization flow (`responseType: 'code'`) provide a URI for a service that accepts a POST request with JSON payload containing a `code` property, and returns tokens [exchanged by provider](https://developers.google.com/identity/protocols/OpenIDConnect#exchangecode) for `code`. See [source code](https://github.com/nuxt-community/auth-module/blob/dev/lib/schemes/oauth2.js)


If a `false` value is set, we only do login without fetching user profile.

#### `logout`

Endpoint to logout user from Oauth2 provider's system. Ensures that a user is signed out of the current authorization session.

### token

#### `property`
Expand Down
30 changes: 30 additions & 0 deletions lib/schemes/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ export default class Oauth2Scheme {
}
}

get _logoutRedirectURI () {
const url = this.options.logoutRedirectUri

if (url) {
return url
}

if (process.server && this.req) {
const protocol = 'http' + (isHttps(this.req) ? 's' : '') + '://'

return protocol + this.req.headers.host + this.$auth.options.redirect.logout
}

if (process.client) {
return window.location.origin + this.$auth.options.redirect.logout
}
}

async mounted () {
// Sync tokens
this.$auth.token.sync()
Expand Down Expand Up @@ -116,6 +134,18 @@ export default class Oauth2Scheme {
window.location = url
}

logout () {
if (this.options.endpoints.logout) {
const opts = {
client_id: this.options.clientId,
logout_uri: this._logoutRedirectURI
}
const url = this.options.endpoints.logout + '?' + encodeQuery(opts)
window.location = url
}
return this.$auth.reset()
}

async fetchUser () {
if (!this.$auth.token.get()) {
return
Expand Down

0 comments on commit 43eedc7

Please sign in to comment.