Skip to content

Commit b068bac

Browse files
committed
Naive login implementation
1 parent d0753f8 commit b068bac

File tree

8 files changed

+8197
-25
lines changed

8 files changed

+8197
-25
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"lint": "vue-cli-service lint"
99
},
1010
"dependencies": {
11+
"axios": "^0.18.0",
12+
"body-parser": "^1.18.3",
13+
"cors": "^2.8.5",
1114
"express": "^4.16.4",
1215
"jsonwebtoken": "^8.4.0",
1316
"vue": "^2.5.22",

server.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
var express = require("express");
22
var jwt = require("jsonwebtoken");
3+
var cors = require("cors");
4+
var bodyParser = require("body-parser");
35

46
const app = express();
57

8+
app.use(cors());
9+
app.use(bodyParser.json());
10+
611
app.get("/", (req, res) => {
712
res.json({
813
message: "Welcome to the API."
@@ -11,26 +16,36 @@ app.get("/", (req, res) => {
1116

1217
app.get("/protected", verifyToken, (req, res) => {
1318
//Do we want to do this async or not?
14-
jwt.verify(req.token, "the_secret_key"),
15-
(err, authData) => {
16-
if (err) {
17-
res.sendStatus(403);
18-
} else {
19-
res.json({
20-
message: "You've successly accessed a protected route!",
21-
authData
22-
});
23-
}
24-
};
19+
jwt.verify(req.token, "the_secret_key", err => {
20+
if (err) {
21+
res.sendStatus(403);
22+
} else {
23+
res.json({
24+
message: "You've successly accessed a protected route!"
25+
});
26+
}
27+
});
2528
});
2629

2730
app.post("/login", (req, res) => {
2831
// Are we fine with just faking out a user?
29-
const user = { name: "Nancy Usery", email: "nancy@gmail.com", id: 4321 };
30-
const token = jwt.sign({ user }, "the_secret_key");
31-
res.json({
32-
token
33-
});
32+
const user = {
33+
email: "nancy@gmail.com",
34+
password: "pass123"
35+
};
36+
if (
37+
req.body &&
38+
req.body.email === user.email &&
39+
req.body.password === user.password
40+
) {
41+
const token = jwt.sign({ user }, "the_secret_key");
42+
res.json({
43+
token,
44+
email: user.email
45+
});
46+
} else {
47+
res.sendStatus(401);
48+
}
3449
});
3550

3651
function verifyToken(req, res, next) {
@@ -40,6 +55,7 @@ function verifyToken(req, res, next) {
4055
if (typeof bearerHeader !== "undefined") {
4156
const bearer = bearerHeader.split(" ");
4257
const bearerToken = bearer[1];
58+
4359
req.token = bearerToken;
4460
next();
4561
} else {

src/App.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<div id="app">
33
<div id="nav">
44
<router-link to="/">Home</router-link> |
5-
<router-link to="/about">About</router-link>
5+
<router-link to="/login">Login</router-link> |
6+
<router-link to="/dashboard">Dashboard</router-link>
67
</div>
78
<router-view />
89
</div>

src/router.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Vue from "vue";
22
import Router from "vue-router";
33
import Home from "./views/Home.vue";
4+
import Dashboard from "./views/Dashboard.vue";
45

56
Vue.use(Router);
67

@@ -14,13 +15,18 @@ export default new Router({
1415
component: Home
1516
},
1617
{
17-
path: "/about",
18-
name: "about",
18+
path: "/dashboard",
19+
name: "dashboard",
20+
component: Dashboard
21+
},
22+
{
23+
path: "/login",
24+
name: "login",
1925
// route level code-splitting
2026
// this generates a separate chunk (about.[hash].js) for this route
2127
// which is lazy-loaded when the route is visited.
2228
component: () =>
23-
import(/* webpackChunkName: "about" */ "./views/About.vue")
29+
import(/* webpackChunkName: "about" */ "./views/Login.vue")
2430
}
2531
]
2632
});

src/views/About.vue

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/views/Dashboard.vue

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<template>
2+
<div class="dashboard">
3+
<h1>Dashboard</h1>
4+
{{ message }}
5+
</div>
6+
</template>
7+
8+
<script>
9+
import axios from "axios";
10+
11+
export default {
12+
data() {
13+
return {
14+
message: "NO MESSAGE"
15+
};
16+
},
17+
created() {
18+
const userString = localStorage.getItem("user");
19+
if (userString) {
20+
const token = JSON.parse(userString).token;
21+
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
22+
23+
axios.get("//localhost:3000/protected").then(({ data }) => {
24+
this.message = data.message;
25+
});
26+
}
27+
}
28+
};
29+
</script>

src/views/Login.vue

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<template>
2+
<div class="about">
3+
<form @submit.prevent="login">
4+
<input v-model="email" type="email" name="email" value="" />
5+
<input v-model="password" type="password" name="" value="" />
6+
<button type="submit" name="button">Login</button>
7+
</form>
8+
</div>
9+
</template>
10+
11+
<script>
12+
import axios from "axios";
13+
14+
export default {
15+
data() {
16+
return {
17+
email: "",
18+
password: ""
19+
};
20+
},
21+
methods: {
22+
login() {
23+
axios
24+
.post("//localhost:3000/login", {
25+
email: this.email,
26+
password: this.password
27+
})
28+
.then(({ data }) => {
29+
localStorage.setItem("user", JSON.stringify(data));
30+
axios.defaults.headers.common["Authorization"] = `Bearer ${
31+
data.token
32+
}`;
33+
this.$router.push("/dashboard");
34+
});
35+
}
36+
}
37+
};
38+
</script>

0 commit comments

Comments
 (0)