| @@ -18,7 +18,7 @@ const tokenSchema = new mongoose.Schema({ | |||
| tokenSchema.statics.findByCredentials = async (email, password) => { | |||
| const user = await User.findOne({email}) | |||
| if(!user) { | |||
| throw new Error('Login unsuccessfull!') | |||
| return | |||
| } | |||
| const checkMatch = await bcrypt.compare(password, user.password) | |||
| @@ -26,7 +26,7 @@ tokenSchema.statics.findByCredentials = async (email, password) => { | |||
| console.log(user.password) | |||
| console.log(checkMatch) | |||
| if(!checkMatch) { | |||
| return | |||
| return user | |||
| } | |||
| return user | |||
| } | |||
| @@ -8,12 +8,12 @@ const router = new express.Router() | |||
| router.get('/login', async (req, res) => { | |||
| const findUser = await Token.findByCredentials(req.body.email, req.body.password) | |||
| if(!findUser) { | |||
| return res.status(400).send('Invalid credentials!') | |||
| return res.status(400).send('User does not exist, wrong email') | |||
| } | |||
| const isValidPassword = await bcrypt.compare(req.body.password, findUser.password) | |||
| if(!isValidPassword) { | |||
| return res.status(400).send("Email or password is incorrect!") | |||
| return res.status(400).send('Password is incorrect!') | |||
| } | |||
| return res.send(findUser) | |||
| @@ -108,19 +108,4 @@ router.delete('/users/:id', (req, res) => { | |||
| } | |||
| }) | |||
| router.post('/users/login', async (req, res) => { | |||
| try { | |||
| const user = await User.findByCredentials(req.body.email, req.body.password) | |||
| if(!user) { | |||
| return res.status(400).send('Invalid email or password!') | |||
| } | |||
| console.log(user) | |||
| const token = await user.generateAuthToken() | |||
| console.log(token) | |||
| return res.send(user) | |||
| } catch(e) { | |||
| } | |||
| }) | |||
| module.exports = router | |||