mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 15:47:48 +01:00
68 lines
1.6 KiB
JavaScript
68 lines
1.6 KiB
JavaScript
const express = require('express');
|
|
const mongoose = require('mongoose');
|
|
const passport = require('passport');
|
|
|
|
const User = mongoose.model('User');
|
|
|
|
const router = express.Router();
|
|
|
|
const asyncHelper = require('../asyncHelper');
|
|
const auth = require('./auth');
|
|
|
|
const { NotFoundError } = require('../errors');
|
|
|
|
router.post(
|
|
'/',
|
|
asyncHelper(async (req, res) => {
|
|
const { username, password } = req.body;
|
|
const user = new User({ username });
|
|
await user.setPassword(password);
|
|
await user.save();
|
|
res.json({ success: true, data: user.toAuthJson() });
|
|
}),
|
|
);
|
|
|
|
router.patch(
|
|
'/user',
|
|
auth.required,
|
|
asyncHelper(async (req, res) => {
|
|
const { username, password } = req.body;
|
|
const patch = {};
|
|
if (username !== undefined) {
|
|
patch.username = username;
|
|
}
|
|
const user = await User.findOneAndUpdate(
|
|
{ _id: req.user.id },
|
|
{ $set: patch },
|
|
{ runValidators: true, context: 'query', new: true },
|
|
).exec();
|
|
if (!user) {
|
|
throw new NotFoundError(`can't find user with username ${req.user.username}`);
|
|
}
|
|
if (password !== undefined) {
|
|
await user.setPassword(password);
|
|
await user.save();
|
|
}
|
|
res.json({ success: true, data: user.toAuthJson() });
|
|
}),
|
|
);
|
|
|
|
router.delete(
|
|
'/user',
|
|
auth.required,
|
|
asyncHelper(async (req, res) => {
|
|
await User.findByIdAndRemove(req.user.id).exec();
|
|
res.json({ success: true });
|
|
}),
|
|
);
|
|
|
|
router.post(
|
|
'/login',
|
|
passport.authenticate('local', { session: false }),
|
|
asyncHelper(async (req, res) => {
|
|
res.json({ success: true, data: req.user.toAuthJson() });
|
|
}),
|
|
);
|
|
|
|
module.exports = router;
|