Skip to content

Commit e51e318

Browse files
committed
get users registered
1 parent 66e4ee7 commit e51e318

10 files changed

Lines changed: 145 additions & 37 deletions

File tree

api.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { json } from 'body-parser'
2+
import { addUser } from './db'
23

34
export default [
45
{
@@ -11,5 +12,17 @@ export default [
1112
(req, res, next) => {
1213
res.json = (obj) => res.write(JSON.stringify(obj))
1314
next()
15+
},
16+
{
17+
path: '/api/users',
18+
async handler(req, res, next) {
19+
if (req.method === 'POST') {
20+
await addUser(req.body)
21+
res.json({ success: true })
22+
res.end()
23+
}
24+
res.writeHead(403, 'Forbidden')
25+
res.end()
26+
}
1427
}
1528
]

db.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
import bcrypt from 'bcrypt'
3+
4+
function hashPassword(password) {
5+
const salt = bcrypt.genSaltSync()
6+
return new Promise((resolve) => {
7+
bcrypt.hash(password, salt, (err, hash) => {
8+
resolve(err ? null : hash)
9+
})
10+
})
11+
}
12+
13+
const db = {
14+
users: {}
15+
}
16+
17+
export async function addUser(user) {
18+
user.password = await hashPassword(user.password)
19+
db.users[user.email] = user
20+
}

layouts/default.vue

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<template>
2+
<main>
3+
<nuxt />
4+
</main>
5+
</template>
6+
7+
<style>
8+
html, body, #__nuxt, #__layout {
9+
font-size: 16px;
10+
width: 100%;
11+
height: 100%;
12+
margin: 0px;
13+
}
14+
main {
15+
width: 100%;
16+
height: 100%;
17+
margin: 0px;
18+
display: flex;
19+
flex-direction: column;
20+
align-items: center;
21+
justify-content: center;
22+
}
23+
main div {
24+
display: flex;
25+
flex-direction: column;
26+
align-items: flex-start;
27+
}
28+
main input, main button {
29+
outline: none;
30+
display: block;
31+
font-size: 16px;
32+
margin-bottom: 5px;
33+
}
34+
</style>

middleware/auth.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
export default function ({ store, route, redirect }) {
2+
if (!store.state.user.authenticated) {
3+
redirect('/register')
4+
}
25
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"dependencies": {
2121
"@nuxt/http": "^0.1.1",
22+
"bcrypt": "^3.0.6",
2223
"body-parser": "^1.19.0"
2324
}
2425
}

pages/index.vue

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
11
<template>
2-
<div class="login">
3-
<input
4-
placeholder="Email"
5-
:value="form.email">
6-
<input
7-
placeholder="Password"
8-
:value="form.password">
9-
<button @click="login">
10-
Login
11-
</button>
2+
<div>
3+
Hello, {{ user.name }}!
124
</div>
135
</template>
146

157
<script>
8+
import { mapState } from 'vuex'
9+
1610
export default {
17-
data: () => ({
18-
form: {}
19-
}),
20-
async asyncData({ app }) {
21-
const response = await app.$http.get('api/ping')
22-
return {
23-
pongMessage: await response.text()
24-
}
25-
},
26-
mounted() {
27-
alert(this.pongMessage)
28-
}
11+
middleware: 'auth',
12+
computed: mapState(['user'])
2913
}
3014
</script>

pages/login.vue

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<template>
2+
<div>
3+
<h2>Login</h2>
4+
<input
5+
placeholder="Email"
6+
:value="form.email">
7+
<input
8+
placeholder="Password"
9+
:value="form.password">
10+
<button @click="login">
11+
Login
12+
</button>
13+
</div>
14+
</template>
15+
16+
<script>
17+
export default {
18+
data: () => ({
19+
form: {}
20+
}),
21+
methods: {
22+
login() {
23+
}
24+
}
25+
}
26+
</script>

pages/protected.vue

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

pages/register.vue

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<template>
2+
<div>
3+
<h2>Register</h2>
4+
<input
5+
placeholder="Name"
6+
:value="form.name">
7+
<input
8+
type="email"
9+
placeholder="Email"
10+
:value="form.email">
11+
<input
12+
type="password"
13+
placeholder="Password"
14+
:value="form.password">
15+
<button @click="register">
16+
Register
17+
</button>
18+
</div>
19+
</template>
20+
21+
<script>
22+
export default {
23+
data: () => ({
24+
form: {}
25+
}),
26+
methods: {
27+
async register() {
28+
const jsonResponse = await
29+
this.$http.$post('api/users', this.form)
30+
if (jsonResponse.success) {
31+
this.$router.push('/login')
32+
}
33+
}
34+
}
35+
}
36+
</script>

store/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
export const state = () => ({
2-
2+
user: {
3+
authenticated: false,
4+
id: null,
5+
email: null,
6+
name: null
7+
}
38
})

0 commit comments

Comments
 (0)