-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.controller.js
More file actions
65 lines (59 loc) · 2.12 KB
/
Copy pathauth.controller.js
File metadata and controls
65 lines (59 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const UserModel = require("./User")
const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken')
const expressJwt = require('express-jwt')
const express = require('express')
const validateJwt = expressJwt.expressjwt({ secret: process.env.SECRET, algorithms: ['HS256'] })
const findAndAssignUser = async (req, res, next) => {
try {
const user = await UserModel.findById(req.auth._id)
if (!user) {
return res.status(401).end()
}
req.user = user
next()
} catch (error) {
next(error)
}
}
const isAuthenticated = express.Router().use(validateJwt,findAndAssignUser)
const signToken = (_id) => jwt.sign({ _id }, process.env.SECRET)
const Auth = {
login: async (req, res) => {
const { body } = req
try {
const user = await UserModel.findOne({username: body.username})
if (!user) {
return res.status(401).send('Wrong username or password.')
} else {
const isMatch = await bcrypt.compare(body.password, user.password)
if (isMatch) {
const signed = signToken(user._id)
res.status(200).send(signed)
} else {
return res.status(401).send('Wrong username or password.')
}
}
} catch (error) {
res.send(error.message)
}
},
register: async (req, res) => {
const { body } = req
try {
const alreadyExist = await UserModel.findOne({username: body.username})
if (alreadyExist) {
return res.send("User already exist.")
} else {
const salt = await bcrypt.genSalt()
const hashed = await bcrypt.hash(body.password, salt)
const user = await UserModel.create({username: body.username, password: hashed, salt: salt})
const signed = signToken(user._id)
res.send(signed)
}
} catch (error) {
res.status(500).send(error.message)
}
},
}
module.exports = { Auth, isAuthenticated }