로그인을 하기 위해선 우선 회원정보가 있어야 합니다.
Mongoose를 이용하기 위해 아래 두 과정이 필요합니다.
//Defining my schema
const userSchema = new mongoose.Schema({
avatarUrl: String,
email: { type: String, required: true, unique: true },
username: { type: String, required: true, unique: true },
password: { type: String },
name: { type: String },
});
//2. Creating a model
const User = mongoose.model("User", userSchema);
mongoose.connect("mongodb://127.0.0.1:27017/ES6");
const { name, username, email, password, password2} = req.body;
const exists = await User.exists({ $or: [{ username }, { email }] });
if (exists) {
return res.status(400).render("join", {
errorMessage: "This username/email is already taken.",
});
}
https://www.mongodb.com/docs/manual/reference/operator/query/or/
https://ko.wikipedia.org/wiki/HTTP_%EC%83%81%ED%83%9C_%EC%BD%94%EB%93%9C
if (password !== password2) {
return res.status(400).render("join", {
errorMessage: "Password confirmation does not match.",
});
}
const user = await User.findOne({username});
if (!user) {
return res.status(400).render("login", {
errorMessage: "An account with this username does not exists.",
});
}
const ok = await bcrypt.compare(password, user.password);
if (!ok) {
return res.status(400).render("login", {
errorMessage: "Wrong password",
});
}
app.use(
session({
secret: "정준",
resave: true,
saveUninitialized: true,
store: MongoStore.create({ mongoUrl: mongoUrl }),
})
);
req.session.loggedIn = true;
req.session.user = user;
https://github.com/JUNGJUNSEO/ES6/tree/78c65fd3717df5af507190c676131e505283388e/src