|
@@ -9,40 +9,6 @@ const client = require('../helpers/twilio');
|
|
const phoneToken = require('generate-sms-verification-code');
|
|
const phoneToken = require('generate-sms-verification-code');
|
|
const SECRET_KEY = process.env.JWT_SECRET;
|
|
const SECRET_KEY = process.env.JWT_SECRET;
|
|
|
|
|
|
-const saveAvatarForStatic = async (req, res, next) => {
|
|
|
|
- try {
|
|
|
|
- const userId = req.user.id;
|
|
|
|
- const DIR_IMAGES = process.env.DIR_IMAGES;
|
|
|
|
- const pathToFile = req.file.path;
|
|
|
|
- const newNameAvatar = req.file.originalname;
|
|
|
|
-
|
|
|
|
- const img = await Jimp.read(pathToFile);
|
|
|
|
- await img
|
|
|
|
- .autocrop()
|
|
|
|
- .cover(
|
|
|
|
- 250,
|
|
|
|
- 250,
|
|
|
|
- Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE
|
|
|
|
- )
|
|
|
|
- .writeAsync(pathToFile);
|
|
|
|
-
|
|
|
|
- await createFolderIsExist(path.join(DIR_IMAGES, userId));
|
|
|
|
- await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameAvatar));
|
|
|
|
- const newUrlAvatar = path.normalize(path.join(userId, newNameAvatar));
|
|
|
|
- const newUrl = `http://localhost:3000/${userId}/${newNameAvatar}`;
|
|
|
|
- await UserModel.updateAvatar(userId, newUrlAvatar);
|
|
|
|
- return res.status(200).json({
|
|
|
|
- status: 'success',
|
|
|
|
- code: 200,
|
|
|
|
- data: {
|
|
|
|
- avatarUrl: newUrl,
|
|
|
|
- },
|
|
|
|
- });
|
|
|
|
- } catch (e) {
|
|
|
|
- next(e);
|
|
|
|
- }
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
const createNewUser = async (req, res, next) => {
|
|
const createNewUser = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
const code = phoneToken(4, { type: 'number' });
|
|
const code = phoneToken(4, { type: 'number' });
|
|
@@ -83,8 +49,8 @@ const logIn = async (req, res, next) => {
|
|
const id = user._id;
|
|
const id = user._id;
|
|
const payload = { id };
|
|
const payload = { id };
|
|
const token = jwt.sign(payload, SECRET_KEY, { expiresIn: '24h' });
|
|
const token = jwt.sign(payload, SECRET_KEY, { expiresIn: '24h' });
|
|
- let registration = false;
|
|
|
|
- if (!user.name || !user.lastName || !user.avatarUrl) registration = true;
|
|
|
|
|
|
+ let registered = true;
|
|
|
|
+ if (!user.name || !user.lastName || !user.avatarUrl) registered = false;
|
|
await UserModel.updateToken(id, token);
|
|
await UserModel.updateToken(id, token);
|
|
await UserModel.updateCode(id, '');
|
|
await UserModel.updateCode(id, '');
|
|
return res.status(200).json({
|
|
return res.status(200).json({
|
|
@@ -92,7 +58,7 @@ const logIn = async (req, res, next) => {
|
|
code: 200,
|
|
code: 200,
|
|
data: {
|
|
data: {
|
|
token,
|
|
token,
|
|
- registration,
|
|
|
|
|
|
+ registered,
|
|
},
|
|
},
|
|
});
|
|
});
|
|
} catch (e) {
|
|
} catch (e) {
|
|
@@ -143,11 +109,43 @@ const getCurrent = async (req, res, next) => {
|
|
|
|
|
|
const updateCredentials = async (req, res, next) => {
|
|
const updateCredentials = async (req, res, next) => {
|
|
try {
|
|
try {
|
|
- const id = req.user.id;
|
|
|
|
- const user = await UserModel.updateCredentials(id, req.body);
|
|
|
|
|
|
+ const { id, token } = req.user;
|
|
|
|
+ await UserModel.updateCredentials(id, req.body);
|
|
return res.status(200).json({
|
|
return res.status(200).json({
|
|
data: {
|
|
data: {
|
|
- ...user,
|
|
|
|
|
|
+ token,
|
|
|
|
+ },
|
|
|
|
+ });
|
|
|
|
+ } catch (e) {
|
|
|
|
+ next(e);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const updateAvatar = async (req, res, next) => {
|
|
|
|
+ try {
|
|
|
|
+ const userId = req.user.id;
|
|
|
|
+ const token = req.user.token;
|
|
|
|
+ const DIR_IMAGES = process.env.DIR_IMAGES;
|
|
|
|
+ const pathToFile = req.file.path;
|
|
|
|
+ const newNameAvatar = req.file.originalname;
|
|
|
|
+ const img = await Jimp.read(pathToFile);
|
|
|
|
+ await img
|
|
|
|
+ .autocrop()
|
|
|
|
+ .cover(
|
|
|
|
+ 250,
|
|
|
|
+ 250,
|
|
|
|
+ Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE
|
|
|
|
+ )
|
|
|
|
+ .writeAsync(pathToFile);
|
|
|
|
+ await createFolderIsExist(path.join(DIR_IMAGES, userId));
|
|
|
|
+ await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameAvatar));
|
|
|
|
+ const avatarUrl = path.normalize(path.join(userId, newNameAvatar));
|
|
|
|
+ await UserModel.updateAvatar(userId, avatarUrl);
|
|
|
|
+ return res.status(200).json({
|
|
|
|
+ status: 'success',
|
|
|
|
+ code: 200,
|
|
|
|
+ data: {
|
|
|
|
+ token,
|
|
},
|
|
},
|
|
});
|
|
});
|
|
} catch (e) {
|
|
} catch (e) {
|
|
@@ -156,10 +154,10 @@ const updateCredentials = async (req, res, next) => {
|
|
};
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
module.exports = {
|
|
- saveAvatarForStatic,
|
|
|
|
createNewUser,
|
|
createNewUser,
|
|
logIn,
|
|
logIn,
|
|
logOut,
|
|
logOut,
|
|
getCurrent,
|
|
getCurrent,
|
|
updateCredentials,
|
|
updateCredentials,
|
|
|
|
+ updateAvatar,
|
|
};
|
|
};
|