Skip to content

Commit 34d1613

Browse files
author
Walker Leite
committed
feat(loopback): add token in local storage
1 parent aa9e972 commit 34d1613

File tree

4 files changed

+82
-29
lines changed

4 files changed

+82
-29
lines changed

template/client/router.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,7 @@ const router = new VueRouter({
3434
],
3535
});
3636

37-
router.beforeEach((to, from, next) => {
38-
if (to.matched.some(record => record.meta.requiresAuth)) {
39-
// this route requires auth, check if logged in
40-
// if not, redirect to login page (except when it's profile route and
41-
// there is an access_token).
42-
if (to.name === 'profile' && to.query.access_token) {
43-
next();
44-
} else if (!store.state.auth.access_token) {
45-
next({
46-
name: 'login',
47-
});
48-
} else {
49-
next();
50-
}
51-
} else {
52-
next(); // make sure to always call next()!
53-
}
54-
});
37+
// Sync routes with auth module
38+
store.dispatch('auth/syncRouter', router);
5539

5640
export default router;

template/client/services/loopback.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
1-
import {host, restApiRoot, port} from '~/server/config.json';
1+
import { host, restApiRoot, port } from '~/server/config.json';
22
import axios from 'axios';
33

4+
const Storage = window.localStorage;
5+
6+
/**
7+
* Add a token in the local storage
8+
* */
9+
function exportTokenToLocalStorage(token) {
10+
if (Storage) {
11+
Storage.setItem('loopback-token', JSON.stringify(token));
12+
}
13+
}
14+
15+
/**
16+
* Remove token from local storage
17+
*/
18+
function removeTokenFromLocalStorage() {
19+
if (Storage) {
20+
Storage.removeItem('loopback-token');
21+
}
22+
}
23+
24+
function addTokenFromLocalStorage(http) {
25+
const token = Storage && Storage.getItem('loopback-token');
26+
if (token) http.setToken(JSON.parse(token), false);
27+
}
28+
429
const http = axios.create({
530
baseURL: 'http://' + host + ':' + port + restApiRoot,
631
});
732

8-
http.setToken = token => {
9-
http.defaults.headers.common['Authorization'] = token;
33+
http.setToken = (token, save = true) => {
34+
http.token = token;
35+
http.defaults.headers.common['Authorization'] = token.id;
36+
if (save) exportTokenToLocalStorage(token);
1037
};
1138

1239
http.removeToken = () => {
1340
delete http.defaults.headers.common.Authorization;
41+
removeTokenFromLocalStorage();
1442
};
1543

1644
http.find = (endpoint, filter) => http.get(endpoint, {params: {filter}});
@@ -32,6 +60,9 @@ const interceptResponse = res => {
3260
};
3361
http.interceptors.response.use(interceptResponse, interceptErrors);
3462

63+
// Set storage Token in http if exists
64+
addTokenFromLocalStorage(http);
65+
3566
export default http;
3667

3768
// Documentation: https://github.com/axios/axios

template/client/store/modules/auth/actions.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
import loopback from '@/services/loopback';
22
import router from '@/router.js';
33

4+
/**
5+
* Sync loopback token with current state
6+
*/
7+
export function syncToken({ commit, dispatch }) {
8+
if (loopback.token) {
9+
commit('setAccessToken', loopback.token);
10+
dispatch('loadAccount', loopback.token.userId);
11+
}
12+
}
13+
14+
/**
15+
* Sync router for auth
16+
*/
17+
export function syncRouter({ state, dispatch }, router) {
18+
dispatch('syncToken');
19+
20+
router.beforeEach((to, from, next) => {
21+
if (to.matched.some(record => record.meta.requiresAuth)) {
22+
// this route requires auth, check if logged in
23+
// if not, redirect to login page (except when it's profile route and
24+
// there is an access_token).
25+
if (to.name === 'profile' && to.query.access_token) {
26+
next();
27+
} else if (!state.access_token) {
28+
next({
29+
name: 'login',
30+
});
31+
} else {
32+
next();
33+
}
34+
} else {
35+
next(); // make sure to always call next()!
36+
}
37+
});
38+
}
39+
440
/**
541
* Sign-in account with email and password (stores in state.account)
642
* @param {Function} commit commit mutations function
@@ -16,6 +52,14 @@ export function signIn({commit, dispatch, state}, {email, password}) {
1652
})
1753
.then(token => {
1854
commit('setAccessToken', token);
55+
56+
// Update Loopback Token
57+
if (state.access_token !== null) {
58+
loopback.setToken(state.access_token);
59+
} else {
60+
loopback.removeToken();
61+
}
62+
1963
router.push({name: 'dashboard'});
2064
return dispatch('loadAccount', state.access_token.userId);
2165
});
@@ -32,6 +76,7 @@ export function signOut({commit}) {
3276
.post('/Accounts/logout')
3377
.then(() => {
3478
commit('setAccessToken', null);
79+
loopback.removeToken();
3580
router.push({name: 'login'});
3681
});
3782
}

template/client/store/modules/auth/mutations.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
import loopback from '@/services/loopback';
2-
31
export function setAccessToken(state, token) {
42
// eslint-disable-next-line camelcase
5-
state.access_token = token;
6-
if (state.access_token !== null) {
7-
loopback.setToken(state.access_token.id);
8-
} else {
9-
loopback.removeToken();
10-
}
3+
state.access_token = token
114
}
125

136
export function setAccount(state, account) {

0 commit comments

Comments
 (0)