Skip to content

Commit 379eaaa

Browse files
committed
add jwt login
1 parent e51e318 commit 379eaaa

7 files changed

Lines changed: 67 additions & 15 deletions

File tree

api.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { sign } from 'jsonwebtoken'
12
import { json } from 'body-parser'
2-
import { addUser } from './db'
3+
import { addUser, authUser } from './db'
4+
5+
const expiresIn = '90d'
6+
const sessionSecret = 'some truly random value'
37

48
export default [
59
{
@@ -13,13 +17,28 @@ export default [
1317
res.json = (obj) => res.write(JSON.stringify(obj))
1418
next()
1519
},
20+
{
21+
path: '/api/login',
22+
async handler(req, res, next) {
23+
if (req.method === 'POST' && await authUser(req.body)) {
24+
const payload = { email: req.body.email }
25+
const token = sign(payload, sessionSecret, { expiresIn })
26+
res.json({ token })
27+
res.end()
28+
return
29+
}
30+
res.writeHead(403, 'Forbidden')
31+
res.end()
32+
}
33+
},
1634
{
1735
path: '/api/users',
1836
async handler(req, res, next) {
1937
if (req.method === 'POST') {
2038
await addUser(req.body)
2139
res.json({ success: true })
2240
res.end()
41+
return
2342
}
2443
res.writeHead(403, 'Forbidden')
2544
res.end()

db.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,18 @@ export async function addUser(user) {
1818
user.password = await hashPassword(user.password)
1919
db.users[user.email] = user
2020
}
21+
22+
function checkPassword(password, user) {
23+
return new Promise((resolve) => {
24+
bcrypt.compare(password, user.password, (err, result) => {
25+
resolve(err ? false : result)
26+
})
27+
})
28+
}
29+
30+
export function authUser({ email, password }) {
31+
if (email in db.users && db.users[email]) {
32+
return checkPassword(password, db.users[email])
33+
}
34+
return false
35+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dependencies": {
2121
"@nuxt/http": "^0.1.1",
2222
"bcrypt": "^3.0.6",
23-
"body-parser": "^1.19.0"
23+
"body-parser": "^1.19.0",
24+
"jsonwebtoken": "^8.5.1"
2425
}
2526
}

pages/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div>
3-
Hello, {{ user.name }}!
3+
Hello, {{ user.email }}!
44
</div>
55
</template>
66

pages/login.vue

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<h2>Login</h2>
44
<input
55
placeholder="Email"
6-
:value="form.email">
6+
v-model="form.email">
77
<input
88
placeholder="Password"
9-
:value="form.password">
9+
v-model="form.password">
1010
<button @click="login">
1111
Login
1212
</button>
@@ -19,7 +19,17 @@ export default {
1919
form: {}
2020
}),
2121
methods: {
22-
login() {
22+
async login() {
23+
const response = await
24+
this.$http.$post('api/login', this.form)
25+
if (response.token) {
26+
this.$store.commit('authUser', {
27+
name: this.form.name,
28+
email: this.form.email,
29+
token: response.token
30+
})
31+
}
32+
this.$router.push('/')
2333
}
2434
}
2535
}

pages/register.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
<h2>Register</h2>
44
<input
55
placeholder="Name"
6-
:value="form.name">
6+
v-model="form.name">
77
<input
88
type="email"
99
placeholder="Email"
10-
:value="form.email">
10+
v-model="form.email">
1111
<input
1212
type="password"
1313
placeholder="Password"
14-
:value="form.password">
14+
v-model="form.password">
1515
<button @click="register">
1616
Register
1717
</button>
@@ -25,9 +25,9 @@ export default {
2525
}),
2626
methods: {
2727
async register() {
28-
const jsonResponse = await
29-
this.$http.$post('api/users', this.form)
30-
if (jsonResponse.success) {
28+
let jsonResponse = await this
29+
.$http.$post('api/users', this.form)
30+
if (jsonResponse && jsonResponse.success) {
3131
this.$router.push('/login')
3232
}
3333
}

store/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
export const state = () => ({
22
user: {
3-
authenticated: false,
4-
id: null,
53
email: null,
6-
name: null
4+
token: null,
5+
authenticated: false
76
}
87
})
8+
9+
export const mutations = {
10+
authUser(state, user) {
11+
state.user.email = user.email
12+
state.user.token = user.token
13+
state.user.authenticated = true
14+
}
15+
}

0 commit comments

Comments
 (0)