-
Notifications
You must be signed in to change notification settings - Fork 7k
Expand file tree
/
Copy pathauthController.js
More file actions
172 lines (141 loc) · 3.69 KB
/
authController.js
File metadata and controls
172 lines (141 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const bcrypt = require("bcryptjs");
const User = require("../models/user");
const jwt = require("jsonwebtoken");
const Joi = require("joi");
const gravatar = require("gravatar");
const fs = require("fs/promises");
const path = require("path");
const Jimp = require("jimp");
const avatarsDir = path.join(__dirname, "../public/avatars");
const registerSchema = Joi.object({
email: Joi.string().email().required(),
password: Joi.string().min(6).required(),
});
const loginSchema = Joi.object({
email: Joi.string().email().required(),
password: Joi.string().min(6).required(),
});
// REJESTRACJA
const register = async (req, res, next) => {
try {
const { error } = registerSchema.validate(req.body);
if (error) {
return res.status(400).json({ message: error.details[0].message });
}
const { email, password } = req.body;
const userExists = await User.findOne({ email });
if (userExists) {
return res.status(409).json({
status: "error",
code: 409,
message: "Email in use",
data: "Conflict",
});
}
const hashedPassword = await bcrypt.hash(password, 10);
const avatarURL = gravatar.url(email, { s: "250", d: "identicon" }, true);
const newUser = await User.create({
email,
password: hashedPassword,
avatarURL,
});
res.status(201).json({
user: {
email: newUser.email,
subscription: newUser.subscription,
avatarURL,
},
});
} catch (error) {
next(error);
}
};
// LOGOWANIE
const login = async (req, res, next) => {
try {
const { email, password } = req.body;
const { error } = loginSchema.validate(req.body);
if (error) {
return res.status(400).json({ message: error.details[0].message });
}
const user = await User.findOne({ email });
if (!user) {
return res.status(401).json({
status: "error",
code: 401,
message: `User ${email} doesn't exist`,
data: "Bad request",
});
}
const isPasswordValid = await bcrypt.compare(password, user.password);
if (!isPasswordValid) {
return res.status(401).json({
status: "error",
code: 401,
message: "Email or password is wrong",
data: "Bad request",
});
}
const payload = { id: user._id };
const token = jwt.sign(payload, process.env.SECRET_KEY, {
expiresIn: "1h",
});
user.token = token;
await user.save();
res.status(200).json({
status: "success",
code: 200,
data: {
token,
},
user: {
email: user.email,
subscription: user.subscription,
},
});
} catch (error) {
next(error);
}
};
// WYLOGOWANIE
const logout = async (req, res, next) => {
try {
const user = req.user;
user.token = null;
await user.save();
res.status(204).send();
} catch (error) {
next(error);
}
};
const getCurrentUser = async (req, res) => {
const { email, subscription } = req.user;
res.status(200).json({
email,
subscription,
});
};
const updateAvatar = async (req, res, next) => {
try {
if (!req.file) {
return res.status(400).json({ message: "No file uploaded" });
}
const { path: tmpPath, filename } = req.file;
const newPath = path.join(avatarsDir, filename);
const image = await Jimp.read(tmpPath);
await image.resize(250, 250).writeAsync(tmpPath);
await fs.rename(tmpPath, newPath);
const avatarURL = `/avatars/${filename}`;
await User.findByIdAndUpdate(req.user._id, { avatarURL });
res.json({ avatarURL });
} catch (error) {
next(error);
}
};
module.exports = {
register,
login,
logout,
getCurrentUser,
updateAvatar,
};