Skip to content

Commit c9b8876

Browse files
committed
Move login to the store
1 parent 67f957a commit c9b8876

File tree

6 files changed

+45
-12
lines changed

6 files changed

+45
-12
lines changed

server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ app.get("/protected", verifyToken, (req, res) => {
1818
//Do we want to do this async or not?
1919
jwt.verify(req.token, "the_secret_key", err => {
2020
if (err) {
21-
res.sendStatus(403);
21+
res.sendStatus(401);
2222
} else {
2323
res.json({
2424
message: "You've successly accessed a protected route!"
@@ -61,7 +61,7 @@ function verifyToken(req, res, next) {
6161
req.token = bearerToken;
6262
next();
6363
} else {
64-
res.sendStatus(403);
64+
res.sendStatus(401);
6565
}
6666
}
6767

src/main.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Vue from "vue";
22
import App from "./App.vue";
33
import router from "./router";
44
import store from "./store";
5+
import axios from "axios";
56

67
Vue.config.productionTip = false;
78

@@ -16,5 +17,18 @@ new Vue({
1617

1718
this.$store.commit("setUserData", userData);
1819
}
20+
21+
axios.interceptors.response.use(
22+
response => response,
23+
error => {
24+
console.log(error.response);
25+
if (error.response.status === 401) {
26+
// Do something with response error
27+
this.$router.push("/login");
28+
this.$store.commit("logout");
29+
}
30+
return Promise.reject(error);
31+
}
32+
);
1933
}
2034
}).$mount("#app");

src/router.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Dashboard from "./views/Dashboard.vue";
55

66
Vue.use(Router);
77

8-
export default new Router({
8+
const router = new Router({
99
mode: "history",
1010
base: process.env.BASE_URL,
1111
routes: [
@@ -30,3 +30,18 @@ export default new Router({
3030
}
3131
]
3232
});
33+
34+
router.beforeEach((to, from, next) => {
35+
// redirect to login page if not logged in and trying to access a restricted page
36+
const publicPages = ["/login", "/"];
37+
const authRequired = !publicPages.includes(to.path);
38+
const loggedIn = localStorage.getItem("user");
39+
40+
if (authRequired && !loggedIn) {
41+
return next("/login");
42+
}
43+
44+
next();
45+
});
46+
47+
export default router;

src/store.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ export default new Vuex.Store({
1919
logout() {
2020
localStorage.removeItem("user");
2121
location.reload();
22-
// or
23-
// axios.defaults.headers.common["Authorization"] = "";
24-
// state.user = null;
2522
}
2623
},
27-
actions: {}
24+
actions: {
25+
login({ commit }, credentials) {
26+
return axios
27+
.post("//localhost:3000/login", credentials)
28+
.then(({ data }) => {
29+
commit("setUserData", data);
30+
});
31+
}
32+
}
2833
});

src/views/Dashboard.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import axios from "axios";
1111
export default {
1212
data() {
1313
return {
14-
message: "NO MESSAGE"
14+
message: "Loading..."
1515
};
1616
},
1717
created() {

src/views/Login.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ export default {
2020
},
2121
methods: {
2222
login() {
23-
axios
24-
.post("//localhost:3000/login", {
23+
this.$store
24+
.dispatch("login", {
2525
email: this.email,
2626
password: this.password
2727
})
28-
.then(({ data }) => {
29-
this.$store.commit("setUserData", data);
28+
.then(() => {
3029
this.$router.push("/dashboard");
3130
});
3231
}

0 commit comments

Comments
 (0)