๐ฉ๐ป ๋ฐฑ์๋(Back-End)/Node js
[E-Commerce App with REST API] (9) ๋น๋ฐ๋ฒํธ๋ฅผ ์๊ณ ์์ ๊ฒฝ์ฐ ์ฌ์์ (PUT) & ์ด๋ฉ์ผ ๋ณด๋ด๊ธฐ (nodemail)
์ง์ง์ํ์นด
2023. 3. 25. 01:49
728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ Developers Corner ์ ์ ํ๋ธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
=> Node.js E-Commerce App with REST API: Let's Build a Real-Life Example!
๐ท ๋น๋ฐ๋ฒํธ๋ฅผ ์๊ณ ์์ ๊ฒฝ์ฐ ๋น๋ฐ๋ฒํธ ์ฌ์์ (PUT)
UserSchema.pre("save", async function (next) {
if (!this.isModified("password")) {
next();
}
// Create a new User
// 1) ์ฐ์ ๋น๋ฐ๋ฒํธ ํด์ฌํ(์ํธํ)
this.password = await bcrypt.hash(this.password, 12);
this.passwordConfirm = undefined;
next();
});
// ๋น๋ฐ๋ฒํธ ์ฌ์์ ๋ ์ง
UserSchema.pre('save', function (next) {
if (!this.isModified('password') || this.isNew) return next();
this.passwordChangedAt = Date.now() - 1000;
next();
});
// check password matching
UserSchema.methods.isPasswordMatched = async function (enteredPassword) {
return await bcrypt.compare(enteredPassword, this.password);
};
// return JSON Web Token
UserSchema.methods.createPasswordResetToken = function () {
// Generate Token
const resetToken = crypto.randomBytes(32).toString("hex");
// Hash token
this.passwordResetToken = crypto
.createHash("sha256")
.update(resetToken)
.digest("hex");
// set expired time
this.passwordResetExpires = Date.now() + 30 * 60 * 1000 // 10๋ถ
return resetToken;
};
User Schema ๋ฐ์์ ์ ๊ฑฐ ๊ผญ ํด์ผํ๋๊ฑด๊ฐ?
๋๋ฌด ์ฝ๋๊ฐ ์์์๋น ใ ใ ใ ์ฉ ์ ์๋๊ฐ.ใ ฃ....
๐ท ์ด๋ฉ์ผ ๋ณด๋ด๊ธฐ (nodemail)
โ nodemail
: node ์๋ฒ์์ ๋ฉ์ผ์ ๋ณด๋ผ ์ ์๋ ๋ฉ์ผ ์ ์ก ๋ชจ๋
npm install nodemailer
โ App passwords
์ฑ ๋น๋ฐ๋ฒํธ ์์ฑํด์ .env ์ ์ ์ฅ
728x90
๋ฐ์ํ