From 2aed44854ab353bc166cf862997ba4fd3c91c67e Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 19 Feb 2018 20:29:17 +0330 Subject: [PATCH] feat: loginWith function --- docs/api/methods.md | 27 ++++++++++++++++++++------- docs/strategies/auth0.md | 2 +- docs/strategies/local.md | 2 +- examples/demo/pages/login.vue | 4 ++-- lib/auth/auth.js | 11 ++++++----- lib/auth/schemes/auth0.js | 2 -- lib/auth/schemes/local.js | 2 -- test/module.test.js | 2 +- 8 files changed, 31 insertions(+), 21 deletions(-) diff --git a/docs/api/methods.md b/docs/api/methods.md index b1663ee6b..7c4a3eb77 100644 --- a/docs/api/methods.md +++ b/docs/api/methods.md @@ -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,7 +46,7 @@ Force re-fetch user using active strategy. await this.$auth.fetchUser() ``` -## `hasScope` +## `hasScope(scopeName)` Check if user has a specific scope: ```js @@ -41,7 +54,7 @@ Check if user has a specific scope: 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`) diff --git a/docs/strategies/auth0.md b/docs/strategies/auth0.md index 5d435abdb..6b2425d0f 100644 --- a/docs/strategies/auth0.md +++ b/docs/strategies/auth0.md @@ -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: diff --git a/docs/strategies/local.md b/docs/strategies/local.md index d63166529..8823b2c70 100644 --- a/docs/strategies/local.md +++ b/docs/strategies/local.md @@ -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' diff --git a/examples/demo/pages/login.vue b/examples/demo/pages/login.vue index 56da763e3..d22b2fb79 100644 --- a/examples/demo/pages/login.vue +++ b/examples/demo/pages/login.vue @@ -18,7 +18,7 @@
Login - Login with Auth0 + Login with Auth0
@@ -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 diff --git a/lib/auth/auth.js b/lib/auth/auth.js index 5f17df62c..aa4fc45ea 100644 --- a/lib/auth/auth.js +++ b/lib/auth/auth.js @@ -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)) diff --git a/lib/auth/schemes/auth0.js b/lib/auth/schemes/auth0.js index 4bed8e7cf..d61c685dd 100644 --- a/lib/auth/schemes/auth0.js +++ b/lib/auth/schemes/auth0.js @@ -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)) diff --git a/lib/auth/schemes/local.js b/lib/auth/schemes/local.js index 14034a11b..33e87e0a8 100644 --- a/lib/auth/schemes/local.js +++ b/lib/auth/schemes/local.js @@ -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) { diff --git a/test/module.test.js b/test/module.test.js index 33cabd5b2..00948aeaf 100644 --- a/test/module.test.js +++ b/test/module.test.js @@ -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' } })