๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[E-Commerce App with REST API] (9) ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•Œ๊ณ  ์žˆ์„ ๊ฒฝ์šฐ ์žฌ์ˆ˜์ • (PUT) & ์ด๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ (nodemail) ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป ๋ฐฑ์—”๋“œ(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

https://nodemailer.com/about/

 

 

โœ… App passwords 

์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑํ•ด์„œ .env ์— ์ €์žฅ

 

728x90
๋ฐ˜์‘ํ˜•
Comments