Browse Source

add ability to seaarch in right list

unknown 3 years ago
parent
commit
1c35678cd6
2 changed files with 22 additions and 0 deletions
  1. 21 0
      controllers/user.js
  2. 1 0
      routes/user.js

+ 21 - 0
controllers/user.js

@@ -84,6 +84,24 @@ const logOut = async (req, res, next) => {
 	}
 };
 
+const online = async (req, res, next) => {
+	try {
+		const id = req.user.id;
+		const user = await UserModel.findById(id);
+		if (!user)
+			return res.status(401).json({
+				status: 'error',
+				code: 401,
+				data: 'UNAUTHORIZED',
+				message: 'Invalid credentials',
+			});
+		await UserModel.updateUser(id, { online: new Date() });
+		return res.status(204).json({});
+	} catch (e) {
+		next(e);
+	}
+};
+
 const getCurrent = async (req, res, next) => {
 	try {
 		const user = req.user;
@@ -94,6 +112,8 @@ const getCurrent = async (req, res, next) => {
 				data: 'UNAUTHORIZED',
 				message: 'Invalid credentials',
 			});
+		const id = req.user.id;
+		await UserModel.updateUser(id, { online: true });
 		return res.status(200).json({
 			status: 'success',
 			code: 200,
@@ -154,6 +174,7 @@ module.exports = {
 	createNewUser,
 	logIn,
 	logOut,
+	online,
 	getCurrent,
 	updateCredentials,
 	updateAvatar,

+ 1 - 0
routes/user.js

@@ -9,6 +9,7 @@ router
 	.post('/auth/register', validation.registration, controllers.createNewUser)
 	.post('/auth/login', validation.logIn, controllers.logIn)
 	.post('/auth/logout', guard, controllers.logOut)
+	.post('/auth/online', guard, controllers.online)
 	.patch(
 		'/users/current',
 		[guard, validation.updateUser],