Skip to content

Commit

Permalink
fix(refresh scheme): fix non-required refresh token flow (#715)
Browse files Browse the repository at this point in the history
Set refresh token to `true` instead of `false` if refresh token is not required. That way refresh token expiration is set. And add `isRefreshing` and `updateOnRefresh` params to be able to not update refresh token expiration if refresh endpoint doesn't refresh the refresh token.
Also simplify `check` as refresh token will always be `true` when not required.

Add custom scheme for Laravel JWT provider to not update refresh token expiration on refresh.
  • Loading branch information
JoaoPedroAS51 committed Jun 14, 2020
1 parent 7c11ee9 commit 0948291
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assignDefaults, assignAbsoluteEndpoints } from '../../utils/provider'
import path from 'path'
import { assignDefaults, assignAbsoluteEndpoints } from '../../../utils/provider'

export default function laravelJWT (_nuxt, strategy) {
const { url } = strategy
Expand All @@ -8,7 +9,7 @@ export default function laravelJWT (_nuxt, strategy) {
}

assignDefaults(strategy, {
scheme: 'refresh',
scheme: path.resolve(__dirname, 'scheme'),
name: 'laravelJWT',
endpoints: {
login: {
Expand Down
7 changes: 7 additions & 0 deletions src/providers/laravel/jwt/scheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import RefreshScheme from '../../../schemes/refresh'

export default class LaravelJWT extends RefreshScheme {
_updateTokens (response, { isRefreshing = false, updateOnRefresh = false } = {}) {
super._updateTokens(response, { isRefreshing, updateOnRefresh })
}
}
19 changes: 8 additions & 11 deletions src/schemes/refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ export default class RefreshScheme extends LocalScheme {
this.refreshController = new RefreshController(this)
}

_updateTokens (response) {
_updateTokens (response, { isRefreshing = false, updateOnRefresh = true } = {}) {
const token = getResponseProp(response, this.options.token.property)
const refreshToken = this.options.refreshToken.required ? getResponseProp(response, this.options.refreshToken.property) : false
const refreshToken = this.options.refreshToken.required ? getResponseProp(response, this.options.refreshToken.property) : true

this.token.set(token)

if (refreshToken) {
// Update refresh token if defined and if `isRefreshing` is `false`
// If `isRefreshing` is `true`, then only update if `updateOnRefresh` is also `true`
if (refreshToken && (!isRefreshing || (isRefreshing && updateOnRefresh))) {
this.refreshToken.set(refreshToken)
}
}
Expand All @@ -65,13 +67,8 @@ export default class RefreshScheme extends LocalScheme {
const token = this.token.sync()
const refreshToken = this.refreshToken.sync()

// Token is required but not available
if (!token) {
return response
}

// Refresh token is required but not available
if (this.options.refreshToken.required && !refreshToken) {
// Token and refresh token are required but not available
if (!token || !refreshToken) {
return response
}

Expand Down Expand Up @@ -161,7 +158,7 @@ export default class RefreshScheme extends LocalScheme {
this.options.endpoints.refresh
).then((response) => {
// Update tokens
this._updateTokens(response)
this._updateTokens(response, { isRefreshing: true })
return response
}).catch((error) => {
this.$auth.callOnError(error, { method: 'refreshToken' })
Expand Down

0 comments on commit 0948291

Please sign in to comment.