Skip to content

Commit

Permalink
feat: loginWith function
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Feb 19, 2018
1 parent f26bb20 commit 2aed448
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
27 changes: 20 additions & 7 deletions docs/api/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@

> You can all auth methods anywhere that `this` or `context.app` is available using `$auth`.
### `login`
### `loginWith(strategyName, ...args)`

- Returns: `Promise`

Login using active strategy. Usage varies by current scheme.
Set current strategy to `strategyName` and try to do login. Usage varies by current strategy.

```js
this.$auth.loginWith('local', /* .... */)
.then(() => this.$toast.success('Logged In!'))
```

### `login(...args)`

- Returns: `Promise`

Login using active strategy. Usage varies by current strategy.

> Using `loginWith` is recommended instead of this function!
```js
this.$auth.login(/* .... */)
.then(() => this.$toast.success('Logged In!'))
```

## `logout`
## `logout()`

- Returns: `Promise`

Expand All @@ -23,7 +36,7 @@ Logout active strategy. Usage varies by current scheme.
await this.$auth.logout()
```

## `fetchUser`
## `fetchUser()`

- Returns: `Promise`

Expand All @@ -33,15 +46,15 @@ Force re-fetch user using active strategy.
await this.$auth.fetchUser()
```

## `hasScope`
## `hasScope(scopeName)`
Check if user has a specific scope:

```js
// Returns is a computed boolean
this.$auth.hasScope('admin')
```

### `setToken`
### `setToken(token)`

Set token in all neccessary places including Vuex, local state, localStorage and Axios headers.

Expand All @@ -50,7 +63,7 @@ Set token in all neccessary places including Vuex, local state, localStorage and
this.$auth.setToken('123')
```

### `onError`
### `onError(handler)`

Listen for auth errors: (`plugins/auth.js`)

Expand Down
2 changes: 1 addition & 1 deletion docs/strategies/auth0.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ If this option is not provided, URL of login (or page which initiates login) wil
To initiate login:

```js
this.$auth.strategies.auth0.login()
this.$auth.loginWith('auth0')
```

User will be redirected to a page like this:
Expand Down
2 changes: 1 addition & 1 deletion docs/strategies/local.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ This option can be used to disable all token handling. Useful for Cookie only fl
To do a password based login by sending credentials in request body as a JSON object:

```js
this.$auth.local.login({
this.$auth.local.loginWith('local', {
data: {
username: 'your_username',
password: 'your_password'
Expand Down
4 changes: 2 additions & 2 deletions examples/demo/pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div class="text-center">
<b-btn-group>
<b-btn @click="login" variant="outline-primary" size="lg">Login</b-btn>
<b-btn @click="$auth.strategies.auth0.login()" variant="outline-primary" size="lg">Login with Auth0</b-btn>
<b-btn @click="$auth.loginWith('auth0')" variant="outline-primary" size="lg">Login with Auth0</b-btn>
</b-btn-group>
</div>
</form>
Expand All @@ -45,7 +45,7 @@ export default {
},
methods: {
async login() {
return this.$auth.strategies.local.login({
return this.$auth.loginWith('local', {
data: {
username: this.username,
password: this.password
Expand Down
11 changes: 6 additions & 5 deletions lib/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export default class Auth {
}

// ---------------------------------------------------------------
// Strategy related functions
// Strategy and Scheme
// ---------------------------------------------------------------

get strategy () {
Expand All @@ -222,10 +222,6 @@ export default class Auth {
return this.mounted()
}

// ---------------------------------------------------------------
// Scheme interface wrappers and default handlers
// ---------------------------------------------------------------

mounted () {
if (this.strategy.mounted) {
return Promise.resolve(this.strategy.mounted(...arguments)).then(() => this.fetchUserOnce())
Expand All @@ -234,6 +230,11 @@ export default class Auth {
return this.fetchUserOnce()
}

loginWith (name, ...args) {
return this.setStrategy(name)
.then(() => this.login(...args))
}

login () {
if (this.strategy.login) {
return Promise.resolve(this.strategy.login(...arguments))
Expand Down
2 changes: 0 additions & 2 deletions lib/auth/schemes/auth0.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export default class Auth0Scheme {
}

login (options) {
this.auth.setStrategy(this.options._name)

this.auth0.authorize(Object.assign({
redirectUri: window.location.href.split('#')[0]
}, this.options, options))
Expand Down
2 changes: 0 additions & 2 deletions lib/auth/schemes/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export default class LocalScheme {
return Promise.resolve()
}

this.auth.setStrategy(this.options._name)

return this.auth.request(endpoint, this.options.endpoints.login)
.then(data => {
if (this.options.tokenRequired) {
Expand Down
2 changes: 1 addition & 1 deletion test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('auth', () => {
await page.waitForFunction('!!window.$nuxt')

const { token, user } = await page.evaluate(async () => {
await window.$nuxt.$auth.login({
await window.$nuxt.$auth.loginWith('local', {
data: { username: 'test_username', password: '123' }
})

Expand Down

0 comments on commit 2aed448

Please sign in to comment.