소스 검색

add something

unknown 2 년 전
부모
커밋
285820093e
6개의 변경된 파일21개의 추가작업 그리고 27개의 파일을 삭제
  1. 2 2
      app.js
  2. 9 12
      controllers/user.js
  3. BIN
      images/60d465e986100d3930569f4b/6-1@x2.png
  4. 3 3
      model/user.js
  5. 4 7
      routes/user.js
  6. 3 3
      validation/user.js

+ 2 - 2
app.js

@@ -6,7 +6,7 @@ const helmet = require('helmet');
 const apiLimiter = require('./helpers/apiLimiter');
 
 const contactsRouter = require('./routes/contacts');
-const authenticationRoute = require('./routes/user');
+const userRoute = require('./routes/user');
 const app = express();
 
 const FOLDER_IMAGES = process.env.DIR_IMAGES;
@@ -21,7 +21,7 @@ app.use(cors());
 app.use(express.json());
 
 app.use('/contacts', apiLimiter, contactsRouter);
-app.use('/auth', apiLimiter, authenticationRoute);
+app.use('/api', apiLimiter, userRoute);
 
 app.use((_req, res) => {
 	res.status(404).json({ message: 'Not found' });

+ 9 - 12
controllers/user.js

@@ -45,7 +45,7 @@ const saveAvatarForStatic = async (req, res, next) => {
 
 const createNewUser = async (req, res, next) => {
 	try {
-		const code = phoneToken(8, { type: 'number' });
+		const code = phoneToken(4, { type: 'number' });
 		const { number, country } = req.body;
 		const isExist = await UserModel.findByNumber(number);
 		if (isExist) {
@@ -73,13 +73,7 @@ const logIn = async (req, res, next) => {
 	try {
 		const { number, code } = req.body;
 		const user = await UserModel.findByNumber(number);
-		if (
-			!user ||
-			user.code !== code ||
-			!user.name ||
-			!user.lastName ||
-			!user.avatarUrl
-		)
+		if (!user || user.code !== code)
 			return res.status(401).json({
 				status: 'error',
 				code: 401,
@@ -90,6 +84,7 @@ const logIn = async (req, res, next) => {
 		const id = user._id;
 		const payload = { id };
 		const token = jwt.sign(payload, SECRET_KEY, { expiresIn: '24h' });
+		const registration = user.name? false: true
 		await UserModel.updateToken(id, token);
 		await UserModel.updateCode(id, '');
 		return res.status(200).json({
@@ -97,6 +92,7 @@ const logIn = async (req, res, next) => {
 			code: 200,
 			data: {
 				token,
+				registration
 			},
 		});
 	} catch (e) {
@@ -145,12 +141,13 @@ const getCurrent = async (req, res, next) => {
 	}
 };
 
-const updateName = async (req, res, next) => {
+const updateCredentials = async (req, res, next) => {
 	try {
 		const id = req.user.id;
-		const user = await UserModel.updateName(id, req.body.name);
+		const user = await UserModel.updateCredentials(id, req.body);
 		return res.status(200).json({
-			data: user,
+			data: {
+				user},
 		});
 	} catch (e) {
 		next(e);
@@ -163,5 +160,5 @@ module.exports = {
 	logIn,
 	logOut,
 	getCurrent,
-	updateName,
+	updateCredentials,
 };

BIN
images/60d465e986100d3930569f4b/6-1@x2.png


+ 3 - 3
model/user.js

@@ -16,8 +16,8 @@ const updateCode = async (id, code) => {
 const updateToken = async (id, token) => {
 	return await User.updateOne({ _id: id }, { token });
 };
-const updateName = async (id, name) => {
-	return await User.findByIdAndUpdate({ _id: id }, { name }, { new: true });
+const updateCredentials = async (id, body) => {
+	return await User.findByIdAndUpdate({ _id: id }, {...body}, { new: true });
 };
 const updateAvatar = async (id, avatarUrl) => {
 	return await User.updateOne({ _id: id }, { avatarUrl });
@@ -28,7 +28,7 @@ module.exports = {
 	createUser,
 	updateCode,
 	updateToken,
-	updateName,
+	updateCredentials,
 	updateAvatar,
 	findById,
 };

+ 4 - 7
routes/user.js

@@ -9,11 +9,8 @@ router
 	.post('/register', validation.registration, controllers.createNewUser)
 	.post('/login', validation.logIn, controllers.logIn)
 	.post('/logout', guard, controllers.logOut)
-	.patch('/users', [guard, validation.update], controllers.updateName)
-	.patch(
-		'/avatars',
-		[guard, upload.single('avatar'), validation.validateUploadAvatar],
-		controllers.saveAvatarForStatic
-	)
-	.get('/users/current', guard, controllers.getCurrent);
+	.patch('/users/current', [guard, validation.updateUser], controllers.updateCredentials)
+	.get('/users/current', guard, controllers.getCurrent)
+	.patch('/avatars',[guard, upload.single('avatar'),
+	 validation.validateUploadAvatar],controllers.saveAvatarForStatic)
 module.exports = router;

+ 3 - 3
validation/user.js

@@ -8,8 +8,8 @@ const schemaCreateNewUser = Joi.object({
 }).min(2);
 
 const schemaUpdateUser = Joi.object({
-	name: Joi.string().alphanum().min(3).max(30).optional().trim().optional(),
-	lastName: Joi.string().alphanum().min(3).max(30).optional().trim().optional(),
+	name: Joi.string().min(3).max(30).optional().trim().optional(),
+	lastName: Joi.string().min(3).max(30).optional().trim().optional(),
 	number: Joi.string().min(8).max(14).optional(),
 });
 
@@ -21,7 +21,7 @@ const schemaLogIn = Joi.object({
 module.exports.registration = (req, _res, next) => {
 	return validate(schemaCreateNewUser, req.body, next);
 };
-module.exports.update = (req, _res, next) => {
+module.exports.updateUser = (req, _res, next) => {
 	return validate(schemaUpdateUser, req.body, next);
 };
 module.exports.logIn = (req, _res, next) => {