Permalink
Browse files

feat: improve auth store

BREAKING CHANGE: Some options changed and/or simplified
  • Loading branch information...
pi0 committed Nov 10, 2017
1 parent 869a4d1 commit 499c28ac6300037e759bf84583564b1fc058d8e6
Showing with 123 additions and 134 deletions.
  1. +42 −48 README.md
  2. +28 −16 lib/module.js
  3. +2 −4 lib/templates/auth.middleware.js
  4. +51 −66 lib/templates/auth.store.js
@@ -26,82 +26,76 @@
],
// Default Values
auth: {
login: {
endpoint: 'auth/login',
propertyName: 'token'
},
logout: {
endpoint: 'auth/logout',
method: 'GET',
paramTokenName: '',
appendToken: false
},
user: {
endpoint: 'auth/user',
propertyName: 'user',
paramTokenName: '',
appendToken: false
},
storageTokenName: 'nuxt-auth-token',
tokenType: 'Bearer',
notLoggedInRedirectTo: '/login',
loggedInRedirectTo: '/'
}
user: {
endpoint: 'auth/user',
propertyName: 'user',
},
login: {
endpoint: 'auth/login',
},
logout: {
endpoint: 'auth/logout',
method: 'GET',
},
redirect: {
notLoggedIn: '/login',
loggedIn: '/'
},
token: {
enabled: true,
name: 'token',
cookieName: 'token',
type: 'Bearer'
}
}
```
## Options
#### user
Sets the global settings for store **fetch** action.
* **endpoint** - Set the URL of the user data endpoint. It can be a relative or absolute path.
* **propertyName** - Set the name of the return object property that contains the user data. If you want the entire object returned, set an empty string.
#### login
Set the global settings for the login action.
Set the global settings for store **login** action.
* **endpoint** - Set the URL of the login endpoint. It can be a relative or absolute path.
* **propertyName** - Set the name of the return object property that contains the access token.
#### logout
Sets the global settings for the logout action.
Sets the global settings for store **logout** action.
* **endpoint** - Set the URL of the logout endpoint. It can be a relative or absolute path.
* **method** - Set the request to POST or GET.
* **paramTokenName** - Set the access token query string parameter name.
* **appendToken** - Set true if you want the access token to be inserted in the URL.
#### user
Sets the global settings for the fetch action.
* **endpoint** - Set the URL of the user data endpoint. It can be a relative or absolute path.
* **propertyName** - Set the name of the return object property that contains the user data. If you want the entire object returned, set an empty string.
* **paramTokenName** - Set the access token query string parameter name.
* **appendToken** - Set true if you want the access token to be inserted in the URL.
#### storageTokenName
Set the token name in the local storage and in the cookie.
#### tokenType
Sets the token type of the authorization header.
#### notLoggedInRedirectTo
Sets the redirect URL default of the users not logged in. This is actived when 'auth' middeware is register.
#### Token
* **enabled** - Get and use tokens for authentication.
* **name** - Set the token name in the local storage.
* **cookieName** - Set the token name in Cookies. (Set to `null` to disable)
* **type** - Sets the token type of the authorization header.
#### loggedInRedirectTo
Sets the redirect URL default of the users logged in. This is actived when 'no-auth' middeware is register.
#### redirect
* **notLoggedInRedirectTo** - Sets the redirect URL default of the users not logged in. Only when `auth` middleware is added to a page.
* **loggedInRedirectTo** - Sets the redirect URL default of the users logged in. Only when `no-auth` middleware is added to a page.
## Example usage
```js
// ... code ...
// Do a password based login
store.dispatch('auth/login', {
fields: {
username: 'your_username',
password: 'your_password'
}
}) // run login
})
// ... code ...
store.dispatch('auth/logout') // run logout
// ... code ...
store.state['auth']['token'] // get access token
store.state.auth.token // get access token
// ... code ...
store.state['auth']['user'] // get user data
store.state.auth.user // get user data
// ... code ...
store.getters['auth/loggedIn'] // get login status (true or false)
@@ -113,8 +107,8 @@ store.getters['auth/loggedIn'] // get login status (true or false)
// ... in nuxt.config.js ...
router: {
middleware: [
'auth', // If user not logged in, redirect to '/login' or to URL defined in notLoggedInRedirectTo property
'no-auth' // If user is already logged in, redirect to '/' or to URL defined in loggedInRedirectTo property
'auth', // If user not logged in, redirect to '/login' or to URL defined in redirect property
'no-auth' // If user is already logged in, redirect to '/' or to URL defined in redirect property
]
}
```
@@ -4,37 +4,49 @@ const merge = require('lodash/merge')
module.exports = function (moduleOptions) {
// Apply defaults
const defaults = {
user: {
endpoint: 'auth/user',
propertyName: 'user',
},
login: {
endpoint: 'auth/login',
propertyName: 'token',
session: false
},
logout: {
endpoint: 'auth/logout',
method: 'GET',
paramTokenName: '',
appendToken: false
},
user: {
endpoint: 'auth/user',
propertyName: 'user',
paramTokenName: '',
appendToken: false
redirect: {
notLoggedIn: '/login',
loggedIn: '/'
},
storageTokenName: 'nuxt-auth-token',
tokenType: 'Bearer',
notLoggedInRedirectTo: '/login',
loggedInRedirectTo: '/'
token: {
enabled: true,
name: 'token',
cookieName: 'token',
type: 'Bearer'
}
}
const options = merge(defaults, moduleOptions, this.options.auth)
// Plugin
this.addPlugin({ src: resolve(__dirname, './templates/auth.plugin.js'), fileName: 'auth.plugin.js' })
this.addPlugin({
src: resolve(__dirname, './templates/auth.plugin.js'),
fileName: 'auth.plugin.js',
options
})
// Middleware
this.addTemplate({ src: resolve(__dirname, './templates/auth.middleware.js'), fileName: 'auth.middleware.js', options })
this.addTemplate({
src: resolve(__dirname, './templates/auth.middleware.js'),
fileName: 'auth.middleware.js',
options
})
// Store
this.addTemplate({ src: resolve(__dirname, './templates/auth.store.js'), fileName: 'auth.store.js', options })
this.addTemplate({
src: resolve(__dirname, './templates/auth.store.js'),

This comment has been minimized.

@anteriovieira

anteriovieira Dec 5, 2017

Member

@pi0 , how does nuxt know that this file is a module in store?

This comment has been minimized.

@pi0

pi0 Dec 5, 2017

Member
fileName: 'auth.store.js',
options
})
}
@@ -1,17 +1,15 @@
import middleware from './middleware'
const options = <%= serialize(options) %>
middleware.auth = function authMiddleware ({ store, redirect }) {
// If user not logged in, redirect to /login
if (!store.getters['auth/loggedIn']) {
return redirect(options.notLoggedInRedirectTo)
return redirect('<%= options.redirect.loggedIn %>')
}
}
middleware['no-auth'] = function noAuthMiddleware ({ store, redirect }) {
// If user is already logged in, redirect to /
if (store.getters['auth/loggedIn']) {
return redirect(options.loggedInRedirectTo)
return redirect('<%= options.redirect.notLoggedIn %>')
}
}
Oops, something went wrong.

0 comments on commit 499c28a

Please sign in to comment.