Skip to content

Commit 2aed448

Browse files
author
Pooya Parsa
committed
feat: loginWith function
1 parent f26bb20 commit 2aed448

File tree

8 files changed

+31
-21
lines changed

8 files changed

+31
-21
lines changed

docs/api/methods.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,31 @@
22

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

77
- Returns: `Promise`
88

9-
Login using active strategy. Usage varies by current scheme.
9+
Set current strategy to `strategyName` and try to do login. Usage varies by current strategy.
10+
11+
```js
12+
this.$auth.loginWith('local', /* .... */)
13+
.then(() => this.$toast.success('Logged In!'))
14+
```
15+
16+
### `login(...args)`
17+
18+
- Returns: `Promise`
19+
20+
Login using active strategy. Usage varies by current strategy.
21+
22+
> Using `loginWith` is recommended instead of this function!
1023
1124
```js
1225
this.$auth.login(/* .... */)
1326
.then(() => this.$toast.success('Logged In!'))
1427
```
1528

16-
## `logout`
29+
## `logout()`
1730

1831
- Returns: `Promise`
1932

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

26-
## `fetchUser`
39+
## `fetchUser()`
2740

2841
- Returns: `Promise`
2942

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

36-
## `hasScope`
49+
## `hasScope(scopeName)`
3750
Check if user has a specific scope:
3851

3952
```js
4053
// Returns is a computed boolean
4154
this.$auth.hasScope('admin')
4255
```
4356

44-
### `setToken`
57+
### `setToken(token)`
4558

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

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

53-
### `onError`
66+
### `onError(handler)`
5467

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

docs/strategies/auth0.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ If this option is not provided, URL of login (or page which initiates login) wil
3838
To initiate login:
3939

4040
```js
41-
this.$auth.strategies.auth0.login()
41+
this.$auth.loginWith('auth0')
4242
```
4343

4444
User will be redirected to a page like this:

docs/strategies/local.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ This option can be used to disable all token handling. Useful for Cookie only fl
4242
To do a password based login by sending credentials in request body as a JSON object:
4343

4444
```js
45-
this.$auth.local.login({
45+
this.$auth.local.loginWith('local', {
4646
data: {
4747
username: 'your_username',
4848
password: 'your_password'

examples/demo/pages/login.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<div class="text-center">
1919
<b-btn-group>
2020
<b-btn @click="login" variant="outline-primary" size="lg">Login</b-btn>
21-
<b-btn @click="$auth.strategies.auth0.login()" variant="outline-primary" size="lg">Login with Auth0</b-btn>
21+
<b-btn @click="$auth.loginWith('auth0')" variant="outline-primary" size="lg">Login with Auth0</b-btn>
2222
</b-btn-group>
2323
</div>
2424
</form>
@@ -45,7 +45,7 @@ export default {
4545
},
4646
methods: {
4747
async login() {
48-
return this.$auth.strategies.local.login({
48+
return this.$auth.loginWith('local', {
4949
data: {
5050
username: this.username,
5151
password: this.password

lib/auth/auth.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export default class Auth {
196196
}
197197

198198
// ---------------------------------------------------------------
199-
// Strategy related functions
199+
// Strategy and Scheme
200200
// ---------------------------------------------------------------
201201

202202
get strategy () {
@@ -222,10 +222,6 @@ export default class Auth {
222222
return this.mounted()
223223
}
224224

225-
// ---------------------------------------------------------------
226-
// Scheme interface wrappers and default handlers
227-
// ---------------------------------------------------------------
228-
229225
mounted () {
230226
if (this.strategy.mounted) {
231227
return Promise.resolve(this.strategy.mounted(...arguments)).then(() => this.fetchUserOnce())
@@ -234,6 +230,11 @@ export default class Auth {
234230
return this.fetchUserOnce()
235231
}
236232

233+
loginWith (name, ...args) {
234+
return this.setStrategy(name)
235+
.then(() => this.login(...args))
236+
}
237+
237238
login () {
238239
if (this.strategy.login) {
239240
return Promise.resolve(this.strategy.login(...arguments))

lib/auth/schemes/auth0.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export default class Auth0Scheme {
2727
}
2828

2929
login (options) {
30-
this.auth.setStrategy(this.options._name)
31-
3230
this.auth0.authorize(Object.assign({
3331
redirectUri: window.location.href.split('#')[0]
3432
}, this.options, options))

lib/auth/schemes/local.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ export default class LocalScheme {
1515
return Promise.resolve()
1616
}
1717

18-
this.auth.setStrategy(this.options._name)
19-
2018
return this.auth.request(endpoint, this.options.endpoints.login)
2119
.then(data => {
2220
if (this.options.tokenRequired) {

test/module.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('auth', () => {
4040
await page.waitForFunction('!!window.$nuxt')
4141

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

0 commit comments

Comments
 (0)