Skip to content

Commit

Permalink
feat: improve password grant flow (#717)
Browse files Browse the repository at this point in the history
Pass all data from original requests to the new requests. Also add `scope` support.
  • Loading branch information
JoaoPedroAS51 committed Jun 14, 2020
1 parent 0948291 commit a6f773a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
6 changes: 6 additions & 0 deletions docs/schemes/local.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,9 @@ If your backend requires client id, it can be set here.
- Default: `false`

If your backend requires grant type, it can be set here.

### `scope`

- Default: `false`

If your backend requires scope, it can be set here.
6 changes: 6 additions & 0 deletions docs/schemes/refresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ If your backend requires client id, it can be set here.

If your backend requires grant type, it can be set here.

### `scope`

- Default: `false`

If your backend requires scope, it can be set here.

### `autoLogout`

- Default: `false`
Expand Down
8 changes: 7 additions & 1 deletion src/schemes/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const DEFAULTS: SchemeOptions = {
autoFetch: true
},
clientId: false,
grantType: false
grantType: false,
scope: false
}

export default class LocalScheme extends BaseScheme<typeof DEFAULTS> {
Expand Down Expand Up @@ -135,6 +136,11 @@ export default class LocalScheme extends BaseScheme<typeof DEFAULTS> {
endpoint.data.grant_type = this.options.grantType
}

// Add scope to payload if defined
if (this.options.scope) {
endpoint.data.scope = this.options.scope
}

// Make login request
const response = await this.$auth.request(
endpoint,
Expand Down
26 changes: 14 additions & 12 deletions src/utils/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,25 @@ export function initializePasswordGrantFlow (nuxt, strategy) {
}

formMiddleware(req, res, () => {
const {
username,
password,
grant_type: grantType = strategy.grantType,
refresh_token: refreshToken
} = req.body
const data = req.body

// If `grant_type` is not defined, set default value
if (!data.grant_type) {
data.grant_type = strategy.grantType
}

// If `client_id` is not defined, set default value
if (!data.client_id) {
data.grant_type = clientId
}

// Grant type is password, but username or password is not available
if (grantType === 'password' && (!username || !password)) {
if (data.grant_type === 'password' && (!data.username || !data.password)) {
return next(new Error('Invalid username or password'))
}

// Grant type is refresh token, but refresh token is not available
if (grantType === 'refresh_token' && !refreshToken) {
if (data.grant_type === 'refresh_token' && !data.refresh_token) {
return next(new Error('Refresh token not provided'))
}

Expand All @@ -137,10 +142,7 @@ export function initializePasswordGrantFlow (nuxt, strategy) {
data: {
client_id: clientId,
client_secret: clientSecret,
refresh_token: refreshToken,
grant_type: grantType,
username,
password
...data
},
headers: {
Accept: 'application/json'
Expand Down

0 comments on commit a6f773a

Please sign in to comment.