Skip to content
Permalink
Browse files
feat: loginWith function
  • Loading branch information
Pooya Parsa committed Feb 19, 2018
1 parent f26bb20 commit 2aed44854ab353bc166cf862997ba4fd3c91c67e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
@@ -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`

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

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

- Returns: `Promise`

@@ -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.

@@ -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`)

@@ -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:
@@ -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'
@@ -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>
@@ -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
@@ -196,7 +196,7 @@ export default class Auth {
}

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

get strategy () {
@@ -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())
@@ -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))
@@ -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))
@@ -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) {
@@ -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' }
})

0 comments on commit 2aed448

Please sign in to comment.