Explorar el Código

done something for auth

unknown hace 2 años
padre
commit
0e178c68e8
Se han modificado 3 ficheros con 18 adiciones y 10 borrados
  1. 10 9
      controllers/user.js
  2. 6 0
      model/schemas/user.js
  3. 2 1
      validation/user.js

+ 10 - 9
controllers/user.js

@@ -46,13 +46,13 @@ const saveAvatarForStatic = async (req, res, next) => {
 const createNewUser = async (req, res, next) => {
 	try {
 		const code = phoneToken(8, { type: 'number' });
-		const number = req.body.number;
+		const { number, country } = req.body;
 		const isExist = await UserModel.findByNumber(number);
 		if (isExist) {
 			const id = isExist._id;
 			await UserModel.updateCode(id, code);
 		} else {
-			await UserModel.createUser({ number, code });
+			await UserModel.createUser({ number, country, code });
 		}
 		client.messages.create({
 			body: `${code}`,
@@ -62,7 +62,7 @@ const createNewUser = async (req, res, next) => {
 		return res.status(201).json({
 			status: 'success',
 			code: 201,
-			data: code,
+			data: String(code),
 		});
 	} catch (e) {
 		next(e);
@@ -73,7 +73,13 @@ const logIn = async (req, res, next) => {
 	try {
 		const { number, code } = req.body;
 		const user = await UserModel.findByNumber(number);
-		if (!user || user.code !== code)
+		if (
+			!user ||
+			user.code !== code ||
+			!user.name ||
+			!user.lastName ||
+			!user.avatarUrl
+		)
 			return res.status(401).json({
 				status: 'error',
 				code: 401,
@@ -84,7 +90,6 @@ const logIn = async (req, res, next) => {
 		const id = user._id;
 		const payload = { id };
 		const token = jwt.sign(payload, SECRET_KEY, { expiresIn: '24h' });
-		const { name, lastName, avatarUrl } = user;
 		await UserModel.updateToken(id, token);
 		await UserModel.updateCode(id, '');
 		return res.status(200).json({
@@ -92,10 +97,6 @@ const logIn = async (req, res, next) => {
 			code: 200,
 			data: {
 				token,
-				number,
-				name,
-				lastName,
-				avatarUrl,
 			},
 		});
 	} catch (e) {

+ 6 - 0
model/schemas/user.js

@@ -20,6 +20,12 @@ const userSchema = new Schema(
 			min: 8,
 			max: 14,
 		},
+		country: {
+			type: String,
+			required: [true, 'Country required'],
+			min: 1,
+			max: 40,
+		},
 		avatarUrl: {
 			type: String,
 			default: function () {

+ 2 - 1
validation/user.js

@@ -4,7 +4,8 @@ const validate = require('./validate');
 
 const schemaCreateNewUser = Joi.object({
 	number: Joi.string().min(8).max(14).required(),
-}).min(1);
+	country: Joi.string().min(1).max(40).required(),
+}).min(2);
 
 const schemaUpdateUser = Joi.object({
 	name: Joi.string().alphanum().min(3).max(30).optional().trim().optional(),