Skip to content

Commit 8c72731

Browse files
committed
bagfix:google_redirect
1 parent c5ea3d3 commit 8c72731

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

controllers/auth/login.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const { User } = require('../../models/user');
77
const { SECRET_KEY } = process.env;
88

99
const login = async (req, res) => {
10-
const { email, password, } = req.body;
10+
let { email, password, } = req.body;
11+
email = email.toLowerCase();
1112
const user = await User.findOne({ email });
1213
if (!user) {
1314
throw RequestError(401, "Email not found");

models/user.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const userShema = new Schema({
1313
},
1414
email: {
1515
type: String,
16+
minlength: 6,
17+
maxLength: 63,
1618
required: [true, 'Email is required'],
1719
match: emailRegexp,
1820
unique: true,
@@ -34,14 +36,13 @@ userShema.post("save", handleSchemaValidationErrors);
3436
// ** Joi schemas ***************************************
3537
const singupSchema = Joi.object({
3638
name: Joi.string().required(),
37-
// email: Joi.string().required(),
38-
email: Joi.string().pattern(emailRegexp).required(),
39-
password: Joi.string().min(6).required(),
39+
email: Joi.string().pattern(emailRegexp).min(6).required(),
40+
password: Joi.string().min(6).max(6).required(),
4041
repeat_password: Joi.string().required().valid(Joi.ref('password')),
4142
})
4243

4344
const loginSchema = Joi.object({
44-
email: Joi.string().pattern(emailRegexp).required(),
45+
email: Joi.string().min(6).max(6).pattern(emailRegexp).required(),
4546
password: Joi.string().min(6).required(),
4647
})
4748

routes/google/auth_controller.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,29 @@ const googleRedirect = async (req, res) => {
5858
const name = userData.data.name;
5959
let user = await User.findOne({ email });
6060
if (!user) {
61-
// console.log(`user not found!`)
61+
console.log(`user not found!`)
6262
// const password = "111111";
6363
const password = v4();
6464
const hashPassword = await bcrypt.hash(password, 10);
65-
await User.create({ name, email, password: hashPassword });
65+
user = await User.create({ name, email, password: hashPassword });
6666
console.log(`User was created`)
6767
}
6868
// ***************************************************
6969

7070
// LOGIN *********************************************
71-
user = await User.findOne({ email });
71+
// user = await User.findOne({ email });
7272
const peyload = { id: user._id };
7373
const token = jwt.sign(peyload, SECRET_KEY, { expiresIn: "24h" });
7474
await User.findByIdAndUpdate(user._id, { token });
7575
res.json({
7676
token,
77+
name: user.name,
7778
});
7879
console.log('Login is successful');
7980
// ***************************************************
8081
// return res.redirect(
81-
// `${process.env.FRONTEND_URL}?email=${userData.data.email}`
82+
// `http://localhost:3001?email=${userData.data.email}`
83+
// // `http://localhost:3001/api/users/current/?token=${token}`
8284
// // `${process.env.FRONTEND_URL}?token=${token}`
8385
// )
8486
}

0 commit comments

Comments
 (0)