multy-stub/server/routers/easy-project/auth.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-08-01 13:14:02 +03:00
const router = require('express').Router()
const jwt = require('jsonwebtoken')
const { JWT_SECRET } = require('./constants')
const { registrationUser, signInUser } = require('./db')
const { requiredFields, responseWrapper } = require('./utils')
router.get('/healthcheck', (req, res) => {
res.send(true)
})
router.post('/registration', requiredFields(['email', 'login', 'password']), async (req, res, next) => {
const { email, login, password, ...rest } = req.body
try {
await registrationUser({
email, login, password, ...rest,
})
res.send(responseWrapper(undefined, {
}))
} catch (e) {
next(e)
}
})
router.post('/sign-in', requiredFields(['email', 'password']), async (req, res, next) => {
const { email, password } = req.body
try {
const user = await signInUser({
email, password,
})
req.session.user = user
const token = jwt.sign({
id: user._id,
}, JWT_SECRET)
res.send(responseWrapper(undefined, {
token, user,
}))
} catch (e) {
next(e)
}
})
module.exports = router