1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-08 07:00:04 +00:00

test(auth): added for authorize + token

This commit is contained in:
Saad Jutt
2021-11-02 18:42:06 +05:00
parent f7e0849148
commit b48e674468
8 changed files with 895 additions and 50 deletions

View File

@@ -1,35 +1,15 @@
import express from 'express'
import bcrypt from 'bcryptjs'
import User from '../../model/User'
import { createUser } from '../../controllers/createUser'
import { registerValidation } from '../../utils'
const userRouter = express.Router()
userRouter.post('/', async (req, res) => {
const { error, value } = registerValidation(req.body)
const { error, value: data } = registerValidation(req.body)
if (error) return res.status(400).send(error.details[0].message)
const { displayname, username, password, isadmin, isactive } = value
// Checking if user is already in the database
const usernameExist = await User.findOne({ username })
if (usernameExist) return res.status(400).send('Username already exists.')
// Hash passwords
const salt = await bcrypt.genSalt(10)
const hashPassword = await bcrypt.hash(password, salt)
// Create a new user
const user = new User({
displayname,
username,
password: hashPassword,
isadmin,
isactive
})
try {
const savedUser = await user.save()
const savedUser = await createUser(data)
res.send({
displayname: savedUser.displayname,
username: savedUser.username,