deno.land / x / mongoose@6.7.5 / examples / redis-todo / routers / userRouter.js

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const Router = require('express').Router();const bcrypt = require('bcryptjs');const { User, Password } = require('../db/models/userModel');const Todo = require('../db/models/todoModel');const auth = require('../middleware/auth');
/* @public * @func: create new user * @input: username,name,email and password * @return: auth token */Router.post('/create', async function({ body }, res) { try { // storing password const password = new Password({ password: body.password }); const user = new User({ name: body.name, username: body.username, email: body.email, passwordId: password._id }); // body = user data
// gen auth token const token = await user.genAuthToken();
// hashing password await password.save(); await user.save(); res.status(201).json({ token }); } catch (err) { console.log(err); res.status(501).send('Server Error'); }});
/* @public * @func: login user * @input: user/email, password * @return: auth token */Router.post('/login', async function({ body }, res) { try { const user = await User.findOne( { $or: [{ email: body.email }, { username: body.username }] } ).populate('passwordId'); if (!user) return res.status(404).send({ msg: 'Invalid credential' });
const isPassword = await bcrypt.compare(body.password, user.passwordId.password); if (!isPassword) return res.status(404).send({ msg: 'Invalid credential' });
const token = user.genAuthToken(); res.status(201).json({ token }); } catch (err) { res.status(501).send('Server Error'); }});
/* @api private * @func: edit user * @input: username, name or password * @return: edited user */Router.post('/update', auth, async function({ userId, body }, res) { try { const updatedUser = await User.findByIdAndUpdate( { _id: userId }, { ...body }, { new: true });
// if password then hash it if (body.password) { const password = await Password.findById({ _id: updatedUser.passwordId }); password.password = body.password; password.save(); // hashing password }
res.status(200).json({ user: updatedUser }); } catch (err) { res.status(500).send('Server Error'); }});
/* @api private * @func: delete user */Router.delete('/delete', auth, async function({ userId }, res) { try { await User.findByIdAndRemove({ _id: userId }); await Todo.deleteMany({ userId }); res.status(200).send({ msg: 'User deleted' }); } catch (err) { res.status(501).send('Server Error'); }});
module.exports = Router;
mongoose

Version Info

Tagged at
a year ago