Skip to content
This repository was archived by the owner on May 12, 2020. It is now read-only.

Commit b793af9

Browse files
committed
feat(storage): save token to localStorage
1 parent abcb6b0 commit b793af9

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/store/modules/login/actions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { userLogin } from 'API'
2+
import { setTokenToLocal } from 'UTILS/storage'
23
import types from './mutations/types'
34

45
export default {
@@ -10,6 +11,7 @@ export default {
1011
.then(res => {
1112
commit(types.SET_USERNAME, username)
1213
commit(types.SET_ACCESS_TOKEN, res.access_token)
14+
setTokenToLocal(res.access_token)
1315
vm.$router.replace('/')
1416
})
1517
.catch(e => {

src/utils/storage.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
const TOKEN_KEY = '__token__'
22

3+
function setValueToLocal (key, val) {
4+
const normalizeVal = typeof val === 'string'
5+
? val
6+
: JSON.stringify(val)
7+
localStorage.setItem(key, normalizeVal)
8+
}
9+
310
export function setTokenToLocal (token) {
4-
localStorage.setItem(TOKEN_KEY, token)
11+
setValueToLocal(TOKEN_KEY, token)
512
}
613

714
export function getTokenFromLocal () {
8-
localStorage.getItem(TOKEN_KEY)
15+
return localStorage.getItem(TOKEN_KEY)
916
}
1017

1118
export function removeTokenFromLocal () {

0 commit comments

Comments
 (0)