Skip to content

Commit 499c28a

Browse files
author
Pooya Parsa
committed
feat: improve auth store
BREAKING CHANGE: Some options changed and/or simplified
1 parent 869a4d1 commit 499c28a

File tree

4 files changed

+123
-134
lines changed

4 files changed

+123
-134
lines changed

README.md

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,82 +26,76 @@
2626
],
2727
// Default Values
2828
auth: {
29-
login: {
30-
endpoint: 'auth/login',
31-
propertyName: 'token'
32-
},
33-
logout: {
34-
endpoint: 'auth/logout',
35-
method: 'GET',
36-
paramTokenName: '',
37-
appendToken: false
38-
},
39-
user: {
40-
endpoint: 'auth/user',
41-
propertyName: 'user',
42-
paramTokenName: '',
43-
appendToken: false
44-
},
45-
storageTokenName: 'nuxt-auth-token',
46-
tokenType: 'Bearer',
47-
notLoggedInRedirectTo: '/login',
48-
loggedInRedirectTo: '/'
49-
}
29+
user: {
30+
endpoint: 'auth/user',
31+
propertyName: 'user',
32+
},
33+
login: {
34+
endpoint: 'auth/login',
35+
},
36+
logout: {
37+
endpoint: 'auth/logout',
38+
method: 'GET',
39+
},
40+
redirect: {
41+
notLoggedIn: '/login',
42+
loggedIn: '/'
43+
},
44+
token: {
45+
enabled: true,
46+
name: 'token',
47+
cookieName: 'token',
48+
type: 'Bearer'
49+
}
5050
}
5151
```
5252
5353
## Options
5454
55+
#### user
56+
Sets the global settings for store **fetch** action.
57+
* **endpoint** - Set the URL of the user data endpoint. It can be a relative or absolute path.
58+
* **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.
59+
5560
#### login
56-
Set the global settings for the login action.
61+
Set the global settings for store **login** action.
5762
* **endpoint** - Set the URL of the login endpoint. It can be a relative or absolute path.
58-
* **propertyName** - Set the name of the return object property that contains the access token.
5963
6064
#### logout
61-
Sets the global settings for the logout action.
65+
Sets the global settings for store **logout** action.
6266
* **endpoint** - Set the URL of the logout endpoint. It can be a relative or absolute path.
6367
* **method** - Set the request to POST or GET.
64-
* **paramTokenName** - Set the access token query string parameter name.
65-
* **appendToken** - Set true if you want the access token to be inserted in the URL.
66-
67-
#### user
68-
Sets the global settings for the fetch action.
69-
* **endpoint** - Set the URL of the user data endpoint. It can be a relative or absolute path.
70-
* **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.
71-
* **paramTokenName** - Set the access token query string parameter name.
72-
* **appendToken** - Set true if you want the access token to be inserted in the URL.
73-
74-
#### storageTokenName
75-
Set the token name in the local storage and in the cookie.
76-
77-
#### tokenType
78-
Sets the token type of the authorization header.
7968
80-
#### notLoggedInRedirectTo
81-
Sets the redirect URL default of the users not logged in. This is actived when 'auth' middeware is register.
69+
#### Token
70+
* **enabled** - Get and use tokens for authentication.
71+
* **name** - Set the token name in the local storage.
72+
* **cookieName** - Set the token name in Cookies. (Set to `null` to disable)
73+
* **type** - Sets the token type of the authorization header.
8274
83-
#### loggedInRedirectTo
84-
Sets the redirect URL default of the users logged in. This is actived when 'no-auth' middeware is register.
75+
#### redirect
76+
* **notLoggedInRedirectTo** - Sets the redirect URL default of the users not logged in. Only when `auth` middleware is added to a page.
77+
* **loggedInRedirectTo** - Sets the redirect URL default of the users logged in. Only when `no-auth` middleware is added to a page.
8578
8679
## Example usage
8780
8881
```js
8982
// ... code ...
83+
// Do a password based login
9084
store.dispatch('auth/login', {
9185
fields: {
9286
username: 'your_username',
9387
password: 'your_password'
9488
}
95-
}) // run login
89+
})
9690

9791
// ... code ...
9892
store.dispatch('auth/logout') // run logout
9993

10094
// ... code ...
101-
store.state['auth']['token'] // get access token
95+
store.state.auth.token // get access token
10296

10397
// ... code ...
104-
store.state['auth']['user'] // get user data
98+
store.state.auth.user // get user data
10599

106100
// ... code ...
107101
store.getters['auth/loggedIn'] // get login status (true or false)
@@ -113,8 +107,8 @@ store.getters['auth/loggedIn'] // get login status (true or false)
113107
// ... in nuxt.config.js ...
114108
router: {
115109
middleware: [
116-
'auth', // If user not logged in, redirect to '/login' or to URL defined in notLoggedInRedirectTo property
117-
'no-auth' // If user is already logged in, redirect to '/' or to URL defined in loggedInRedirectTo property
110+
'auth', // If user not logged in, redirect to '/login' or to URL defined in redirect property
111+
'no-auth' // If user is already logged in, redirect to '/' or to URL defined in redirect property
118112
]
119113
}
120114
```

lib/module.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,49 @@ const merge = require('lodash/merge')
44
module.exports = function (moduleOptions) {
55
// Apply defaults
66
const defaults = {
7+
user: {
8+
endpoint: 'auth/user',
9+
propertyName: 'user',
10+
},
711
login: {
812
endpoint: 'auth/login',
9-
propertyName: 'token',
10-
session: false
1113
},
1214
logout: {
1315
endpoint: 'auth/logout',
1416
method: 'GET',
15-
paramTokenName: '',
16-
appendToken: false
1717
},
18-
user: {
19-
endpoint: 'auth/user',
20-
propertyName: 'user',
21-
paramTokenName: '',
22-
appendToken: false
18+
redirect: {
19+
notLoggedIn: '/login',
20+
loggedIn: '/'
2321
},
24-
storageTokenName: 'nuxt-auth-token',
25-
tokenType: 'Bearer',
26-
notLoggedInRedirectTo: '/login',
27-
loggedInRedirectTo: '/'
22+
token: {
23+
enabled: true,
24+
name: 'token',
25+
cookieName: 'token',
26+
type: 'Bearer'
27+
}
2828
}
2929

3030
const options = merge(defaults, moduleOptions, this.options.auth)
3131

3232
// Plugin
33-
this.addPlugin({ src: resolve(__dirname, './templates/auth.plugin.js'), fileName: 'auth.plugin.js' })
33+
this.addPlugin({
34+
src: resolve(__dirname, './templates/auth.plugin.js'),
35+
fileName: 'auth.plugin.js',
36+
options
37+
})
3438

3539
// Middleware
36-
this.addTemplate({ src: resolve(__dirname, './templates/auth.middleware.js'), fileName: 'auth.middleware.js', options })
40+
this.addTemplate({
41+
src: resolve(__dirname, './templates/auth.middleware.js'),
42+
fileName: 'auth.middleware.js',
43+
options
44+
})
3745

3846
// Store
39-
this.addTemplate({ src: resolve(__dirname, './templates/auth.store.js'), fileName: 'auth.store.js', options })
47+
this.addTemplate({
48+
src: resolve(__dirname, './templates/auth.store.js'),
49+
fileName: 'auth.store.js',
50+
options
51+
})
4052
}

lib/templates/auth.middleware.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import middleware from './middleware'
22

3-
const options = <%= serialize(options) %>
4-
53
middleware.auth = function authMiddleware ({ store, redirect }) {
64
// If user not logged in, redirect to /login
75
if (!store.getters['auth/loggedIn']) {
8-
return redirect(options.notLoggedInRedirectTo)
6+
return redirect('<%= options.redirect.loggedIn %>')
97
}
108
}
119

1210
middleware['no-auth'] = function noAuthMiddleware ({ store, redirect }) {
1311
// If user is already logged in, redirect to /
1412
if (store.getters['auth/loggedIn']) {
15-
return redirect(options.loggedInRedirectTo)
13+
return redirect('<%= options.redirect.notLoggedIn %>')
1614
}
1715
}

0 commit comments

Comments
 (0)