| @@ -0,0 +1,25 @@ | |||
| const { Router } = require("express") | |||
| const User = require("../models/token") | |||
| const Token = require('../models/token') | |||
| const bcrypt = require('bcryptjs') | |||
| const loginUser = async (req, res) => { | |||
| try { | |||
| const findUser = await Token.findByCredentials(req.body.email, req.body.password) | |||
| if(!findUser) { | |||
| return res.status(400).send('Wrong credentials!') | |||
| } | |||
| const isValidPassword = await bcrypt.compare(req.body.password, findUser.password) | |||
| if(!isValidPassword) { | |||
| return res.status(400).send('Password is incorrect!') | |||
| } | |||
| const token = await Token.generateAuthToken(findUser) | |||
| return res.send(findUser) | |||
| } catch (e) { | |||
| return res.status(500).send(e) | |||
| } | |||
| } | |||
| module.exports = { loginUser } | |||
| @@ -1,3 +1,4 @@ | |||
| const bcrypt = require("bcryptjs/dist/bcrypt") | |||
| const { Router } = require("express") | |||
| const User = require("../models/user") | |||
| @@ -33,7 +34,11 @@ const create = async (req, res, userModel) => { | |||
| try { | |||
| if (Object.entries(userModel).length !== 0) { | |||
| //create user | |||
| return res.status(201).json(userModel) | |||
| const newUser = new User(req.body) | |||
| newUser.password = await bcrypt.hash(newUser.password, 8) | |||
| await newUser.save() | |||
| return res.status(201).json(newUser) | |||
| } else { | |||
| return res.status(400).send('bad request') | |||
| } | |||
| @@ -21,9 +21,6 @@ tokenSchema.statics.findByCredentials = async (email, password) => { | |||
| return | |||
| } | |||
| const checkMatch = await bcrypt.compare(password, user.password) | |||
| console.log(password) | |||
| console.log(user.password) | |||
| console.log(checkMatch) | |||
| if(checkMatch) { | |||
| return user | |||
| } | |||
| @@ -31,8 +28,10 @@ tokenSchema.statics.findByCredentials = async (email, password) => { | |||
| } | |||
| tokenSchema.statics.generateAuthToken = async function(userArg) { | |||
| console.log('aaa') | |||
| const user = userArg | |||
| const token = jwt.sign({ _id: user._id.toString() }, 'ovoJeSecret', { expiresIn: 60 * 20 }) | |||
| console.log(token) | |||
| user.tokens = user.tokens.concat({ token }) | |||
| await user.save() | |||
| @@ -24,14 +24,17 @@ const userSchema = new mongoose.Schema({ | |||
| }] | |||
| }) | |||
| userSchema.pre('save', async function(next) { | |||
| const user = this | |||
| // userSchema.pre('save', async function(next) { | |||
| // const user = this | |||
| // console.log('pre hash: ' + user.password) | |||
| user.password = await bcrypt.hash(user.password, 8) | |||
| // user.password = await bcrypt.hash(user.password, 8) | |||
| console.log('Middleware before password hash') | |||
| next() | |||
| }) | |||
| // console.log('posle hash: ' + user.password) | |||
| // console.log('Middleware before password hash') | |||
| // next() | |||
| // }) | |||
| const User = mongoose.model('User', userSchema) | |||
| @@ -5,22 +5,11 @@ const jwt = require('jsonwebtoken') | |||
| const bcrypt = require('bcryptjs') | |||
| const router = new express.Router() | |||
| const auth = require('../middleware/auth') | |||
| const endpoints = require('../endpoints/token') | |||
| router.post('/login', async (req, res) => { | |||
| const findUser = await Token.findByCredentials(req.body.email, req.body.password) | |||
| if(!findUser) { | |||
| return res.status(400).send('Wrong credentials!') | |||
| } | |||
| const isValidPassword = await bcrypt.compare(req.body.password, findUser.password) | |||
| if(!isValidPassword) { | |||
| return res.status(400).send('Password is incorrect!') | |||
| } | |||
| const token = await Token.generateAuthToken(findUser) | |||
| return res.send(findUser) | |||
| return await endpoints.loginUser(req, res) | |||
| }) | |||
| router.post('/logout/', async (req, res) => { | |||