-
Notifications
You must be signed in to change notification settings - Fork 927
/
auth.store.js
163 lines (141 loc) · 4.23 KB
/
auth.store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<% if (options.token.enabled && options.token.cookie) { %>
import Cookie from 'cookie'
import Cookies from 'js-cookie'
<% } %>
export default {
namespaced: true,
state: () => ({
<% if (options.token.enabled) { %>token: null,<% } %>
user: null
}),
getters: {
loggedIn (state) {
return Boolean(state.user<% if (options.token.enabled) { %> && state.token<% } %>)
}
},
mutations: {
// SET_USER
SET_USER (state, user) {
state.user = user
},
<% if (options.token.enabled) { %>
// SET_TOKEN
SET_TOKEN (state, token) {
state.token = token
}
<% } %>
},
actions: {
<% if (options.token.enabled) { %>
// Update token
updateToken ({ commit }, token) {
// Update token in store's state
commit('SET_TOKEN', token)
// Set Authorization token for all axios requests
this.$axios.setToken(token, '<%= options.token.type %>');
<% if (options.token.localStorage) { %>
// Update localStorage
if (process.browser && localStorage) {
if (token) {
localStorage.setItem('<%= options.token.name %>', token)
} else {
localStorage.removeItem('<%= options.token.name %>')
}
}
<% } %>
<% if (options.token.cookie) { %>
// Update cookies
if (process.browser) {
// ...Browser
if (token) {
Cookies.set('<%= options.token.cookieName %>', token)
} else {
Cookies.remove('<%= options.token.cookieName %>')
}
} else {
// ...Server
let params = {
domain: '/'
}
if (!token) {
let expires
let date = new Date()
expires = date.setDate(date.getDate() - 1)
params.expires = new Date(expires)
}
this.$ctx.res.setHeader('Set-Cookie', Cookie.serialize('<%= options.token.cookieName %>', token, params))
}
<% } %>
},
<% } %>
<% if (options.token.enabled) { %>
// Fetch Token
fetchToken ({ dispatch }) {
let token
<% if (options.token.localStorage) { %>
// Try to extract token from localStorage
if (process.browser && localStorage) {
token = localStorage.getItem('<%= options.token.name %>')
}
<% } %>
<% if (options.token.cookie) { %>
// Try to extract token from cookies
if (!token) {
const cookieStr = process.browser ? document.cookie : this.$ctx.req.headers.cookie
const cookies = Cookie.parse(cookieStr || '') || {}
token = cookies['<%= options.token.cookieName %>']
}
<% } %>
if (token) {
dispatch('updateToken', token)
}
},
<% } %>
// Reset
reset ({ dispatch, commit }) {
commit('SET_USER', null)
<% if (options.token.enabled) { %>dispatch('updateToken', null)<% } %>
},
// Fetch
async fetch ({ getters, state, commit, dispatch }, { endpoint = '<%= options.user.endpoint %>' } = {}) {
<% if (options.token.enabled) { %>
// Fetch and update latest token
dispatch('fetchToken')
<% } %>
// Skip if not loggedIn
if (!getters.loggedIn) {
return
}
// Try to get user profile
try {
const data = await this.$axios.$get(endpoint)
commit('SET_USER', data<%= options.user.propertyName ? ('[\'' + options.user.propertyName + '\']') : '' %>)
} catch (e) {
// Reset store
dispatch('reset')
}
},
// Login
async login ({ dispatch }, { fields, endpoint = '<%= options.login.endpoint %>' } = {}) {
// Send credentials to API
let data = await this.$axios.$post(endpoint, fields)
<% if (options.token.enabled) { %>
dispatch('updateToken', data['<%= options.token.name %>'])
<% } %>
// Fetch authenticated user
await dispatch('fetch')
},
// Logout
async logout ({ dispatch, state }, { endpoint = '<%= options.logout.endpoint %>' } = {}) {
// Server side logout
try {
await this.$axios.$<%= options.logout.method.toLowerCase() %>(endpoint)
} catch (e) {
// eslint-disable-next-line no-console
console.error('Error while logging out', e)
}
// Reset store
dispatch('reset')
}
}
}