123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- let bcrypt = require('bcryptjs')
- let express = require('express')
- let router = express.Router()
- const Errors = require('../lib/errors.js')
- let User = require('../models').User
- router.post('/', async (req, res) => {
- let user, hash, validationErrors = [];
- try {
- //Validations
- if(req.body.username === undefined) {
- validationErrors.push(Errors.missingParameter('username'))
- } else {
- if(typeof req.body.username !== 'string') {
- validationErrors.push(Errors.invalidParameterType('username', 'string'))
- } if(req.body.username.length < 6) {
- validationErrors.push(Errors.parameterLengthTooSmall('username', 6))
- } if(req.body.username.length > 50) {
- validationErrors.push(Errors.parameterLengthTooLarge('username', 50))
- }
- }
- if(req.body.password === undefined) {
- validationErrors.push(Errors.missingParameter('password'))
- } else {
- if(typeof req.body.password !== 'string') {
- validationErrors.push(Errors.invalidParameterType('password', 'string'))
- } if(req.body.password.length < 6) {
- validationErrors.push(Errors.parameterLengthTooSmall('password', 6))
- } if(req.body.password.length > 100) {
- validationErrors.push(Errors.parameterLengthTooLarge('password', 100))
- }
- }
- if(validationErrors.length) throw Errors.VALIDATION_ERROR
- hash = await bcrypt.hash(req.body.password, 12)
- user = await User.create({
- username: req.body.username,
- hash: hash
- })
- res.json(user.toJSON())
- } catch (err) {
- if(err === Errors.VALIDATION_ERROR) {
- res.status(400)
- res.json({
- errors: validationErrors
- })
- } else if(err.name === 'SequelizeUniqueConstraintError') {
- res.status(400)
- res.json({
- errors: [Errors.accountAlreadyCreated]
- })
- } else {
- res.status(500)
- res.json({
- errors: [Errors.unknown]
- })
- }
- }
- })
- router.get('/:username', async (req, res) => {
- try {
- if(
- !req.session.loggedIn ||
- req.session.username !== req.params.username
- ) {
- throw Errors.requestNotAuthorized
- }
- let user = async User.findOne({
- attributes: { exclude: ['hash', 'id'] },
- where: { username: req.params.username }
- })
- res.json(user.toJSON())
- } catch (err) {
- if(err === Errors.requestNotAuthorized) {
- res.json({
- errors: [Errors.requestNotAuthorized]
- })
- } else {
- console.log(err)
- res.status(500)
- res.json({
- errors: [Errors.unknown]
- })
- }
- }
- })
- router.post('/:username/login', async (req, res) => {
- let user, bcryptRes, validationErrors = []
- try {
- //Validations
- if(req.body.password === undefined) {
- validationErrors.push(Errors.missingParameter('password'))
- } else if(typeof req.body.password !== 'string') {
- validationErrors.push(Errors.invalidParameterType('password', 'string'))
- }
- if(validationErrors.length) throw Errors.VALIDATION_ERROR
- user = await User.findOne({
- where: {
- username: req.params.username,
- }
- })
- if(user) {
- bcryptRes = await bcrypt.compare(req.body.password, user.hash)
- if(bcryptRes) {
- req.session.loggedIn = true
- req.session.username = user.username
- res.json({
- username: user.username,
- success: true
- })
- } else {
- res.status(401)
- res.json({
- errors: [Errors.invalidLoginCredentials]
- })
- }
- } else {
- res.status(401)
- res.json({
- errors: [Errors.invalidLoginCredentials]
- })
- }
- } catch (err) {
- if(err === Errors.VALIDATION_ERROR) {
- res.status(400)
- res.json({
- errors: validationErrors
- })
- } else {
- console.log(err)
- res.status(500)
- res.json({
- errors: [Errors.unknown]
- })
- }
- }
- })
- router.post('/:username/logout', async (req, res) => {
- req.session.loggedIn = false
- req.session.username = undefined
- res.json({
- success: true
- })
- })
- module.exports = router
|