111 lines
3.3 KiB
JavaScript
111 lines
3.3 KiB
JavaScript
import { Router } from 'express';
|
|
import Jwt from 'jsonwebtoken';
|
|
import User from '../models/user.js';
|
|
import { errorCodes, makeError } from '../utils/errors.js';
|
|
|
|
const router = Router();
|
|
|
|
router.post('/signup', async (req, res) => {
|
|
const { email, password } = req.body;
|
|
if(email && password) {
|
|
try {
|
|
let collection = req.db.collection('users');
|
|
let users = await collection.find({ email }).toArray();
|
|
if(users.length == 0) {
|
|
let user = new User(email, password);
|
|
await collection.insertOne(user.toDB());
|
|
user.token = Jwt.sign({ email }, process.env.JWT_SECRET_AUTH, { expiresIn: process.env.JWT_EXPIRATION_TIME });
|
|
res.send({ success: true, data: user });
|
|
} else {
|
|
res.send(makeError('User already exists'));
|
|
}
|
|
} catch(ex) {
|
|
res.send(makeError('Error creating user'));
|
|
console.error(ex);
|
|
}
|
|
} else {
|
|
res.send(makeError('Invalid parameters'));
|
|
}
|
|
});
|
|
|
|
router.post('/login', async (req, res) => {
|
|
const { email, password } = req.body;
|
|
if(email && password) {
|
|
try {
|
|
let users = req.db.collection('users');
|
|
let me = await users.findOne({ email });
|
|
if(me) {
|
|
me = User.fromDB(me);
|
|
if(!me.checkPassword(password)) {
|
|
res.send(makeError('Incorrect email or password', errorCodes.invalidLoginOrPassword));
|
|
return;
|
|
}
|
|
|
|
me.token = Jwt.sign({ email }, process.env.JWT_SECRET_AUTH, { expiresIn: process.env.JWT_EXPIRATION_TIME });
|
|
res.send({ success: true, data: me });
|
|
} else {
|
|
res.send(makeError('Incorrect login or password', errorCodes.invalidLoginOrPassword));
|
|
}
|
|
} catch(ex) {
|
|
res.send(makeError('Error logging in'));
|
|
console.error(ex);
|
|
}
|
|
} else {
|
|
res.status(400).send(makeError('Invalid parameters'));
|
|
}
|
|
});
|
|
|
|
router.post('/signIn', async (req, res) => {
|
|
const { email, password } = req.body;
|
|
if(email && password) {
|
|
try {
|
|
let users = req.db.collection('users');
|
|
let me = await users.findOne({ email });
|
|
if(me) {
|
|
me = User.fromDB(me);
|
|
if(!me.checkPassword(password)) {
|
|
res.send(makeError('Incorrect email or password', errorCodes.invalidLoginOrPassword));
|
|
return;
|
|
}
|
|
|
|
me.token = Jwt.sign({ email }, process.env.JWT_SECRET_AUTH, { expiresIn: process.env.JWT_EXPIRATION_TIME });
|
|
res.send({ success: true, data: me });
|
|
} else {
|
|
let user = new User(email, password);
|
|
await users.insertOne(user.toDB());
|
|
user.token = Jwt.sign({ email }, process.env.JWT_SECRET_AUTH, { expiresIn: process.env.JWT_EXPIRATION_TIME });
|
|
res.send({ success: true, data: user });
|
|
}
|
|
} catch(ex) {
|
|
res.send(makeError('Error logging in'));
|
|
console.error(ex);
|
|
}
|
|
} else {
|
|
res.status(400).send(makeError('Invalid parameters'));
|
|
}
|
|
});
|
|
|
|
router.get('/', async (req, res) => {
|
|
const { email } = req.query;
|
|
let users = await req.db.collection('users').find({ email }).toArray();
|
|
if(users.length > 0) {
|
|
res.send({ success: true, data: User.fromDB(users[0]) });
|
|
} else {
|
|
res.status(204).send(makeError('There is no such user'));
|
|
}
|
|
});
|
|
|
|
router.get('/find', async (req, res) => {
|
|
const { email } = req.query;
|
|
let users = await req.db.collection('users').find({ login: { $regex: new RegExp(`.*${email}.*`, 'i') } }).toArray();
|
|
users = users.map(user => {
|
|
user.contacts = [];
|
|
return User.fromDB(user);
|
|
});
|
|
|
|
let code = users.length > 0 ? 200 : 204;
|
|
res.status(code).send({ success: true, data: { users } });
|
|
});
|
|
|
|
export default router;
|