Skip to content

Commit fc54042

Browse files
committed
add api auth
1 parent 6316365 commit fc54042

File tree

6 files changed

+52
-5
lines changed

6 files changed

+52
-5
lines changed

api.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { sign, verify } from 'jsonwebtoken'
22
import { json } from 'body-parser'
33
import { parse } from 'cookie'
4-
import { addUser, authUser } from './db'
4+
import { addUser, authUser, getUser } from './db'
55

66
const expiresIn = '90d'
77
const sessionSecret = 'some truly random value'
@@ -45,6 +45,35 @@ export default [
4545
res.end()
4646
}
4747
},
48+
(req, res, next) => {
49+
if (!req.url.startsWith('/api')) {
50+
return next()
51+
}
52+
if (!req.headers.authorization) {
53+
res.statusCode = 401
54+
res.end()
55+
return
56+
}
57+
const tokenMatch = req.headers.authorization.match(/Bearer (.+)/)
58+
if (tokenMatch) {
59+
const jwtData = verify(tokenMatch[1], sessionSecret)
60+
if (jwtData) {
61+
req.email = jwtData.email
62+
req.token = tokenMatch[1]
63+
return next()
64+
}
65+
}
66+
res.statusCode = 401
67+
res.end()
68+
},
69+
{
70+
path: '/api/user',
71+
async handler(req, res, next) {
72+
const user = await getUser(req.email)
73+
res.json({ user })
74+
res.end()
75+
}
76+
},
4877
(req, res, next) => {
4978
const cookies = req.headers.cookie || ''
5079
const parsedCookies = parse(cookies) || {}

db.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ export function authUser({ email, password }) {
3333
}
3434
return false
3535
}
36+
37+
export function getUser(email) {
38+
return db.users[email]
39+
}

middleware/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ export default function ({ store, route, redirect, req }) {
66
})
77
}
88
if (!store.state.user.authenticated) {
9-
redirect('/register')
9+
return redirect('/register')
1010
}
1111
}

nuxt.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export default {
88
},
99
http: {
1010
baseURL: 'http://localhost:3030'
11-
}
11+
},
12+
plugins: ['~/plugins/http']
1213
}

pages/index.vue

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

@@ -9,6 +9,11 @@ import { mapState } from 'vuex'
99
1010
export default {
1111
middleware: 'auth',
12-
computed: mapState(['user'])
12+
data: () => ({
13+
user: {}
14+
}),
15+
async asyncData({ $http, store }) {
16+
return $http.$get('api/user')
17+
}
1318
}
1419
</script>

plugins/http.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function ({ $http, store }) {
2+
$http.onRequest((config) => {
3+
if (store.state.user.authenticated) {
4+
config.headers.set('Authorization', `Bearer ${store.state.user.token}`)
5+
}
6+
return config
7+
})
8+
}

0 commit comments

Comments
 (0)