๋ชฉ๋ก๐ฉ๐ป ๋ฐฑ์๋(Back-End)/Node js (108)
๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
=> Node.js E-Commerce App with REST API: Let's Build a Real-Life Example! ๐ท login ์ cookie ์์ฑํ๊ธฐ Cookie ํค๋๋ฅผ ํ์ฑํ๊ณ , ์ฟ ํค ์ด๋ฆ์ ์ํด ํค๊ฐ ์ง์ ๋ ๊ฐ์ฒด๋ก req.cookies๋ฅผ ์ฑ์ secret ๋ฌธ์์ด์ ์ ๋ฌํ์ฌ ์ ํ์ ์ผ๋ก ์๋ช ๋(signed) ์ฟ ํค ์ง์์ ํ์ฑํ secret ๋ฌธ์์ด์ ๋ค๋ฅธ ๋ฏธ๋ค์จ์ด์์ ์ฌ์ฉํ ์ ์๋๋ก req.secret์ ํ ๋นํจ npm i cookie-parser ๐ท JWT(JSON Web Token) ์ ์ ๋ฅผ ์ธ์ฆํ๊ณ ์๋ณํ๊ธฐ ์ํ ํ ํฐ ํ ํฐ์ ์ธ์ ๊ณผ๋ ๋ฌ๋ฆฌ ์๋ฒ๊ฐ ์๋ ํด๋ผ์ด์ธํธ์ ์ ์ฅ โ Refresh token : ์๋ก์ด access token์ ์ฌ๋ฐ๊ธ ๋ฐ์ ์ ์๋ ์ ํจ ๊ธฐ๊ฐ์ด ๊ธด token ..
=> Node.js E-Commerce App with REST API: Let's Build a Real-Life Example! ๐ท ๋ก๊ทธ์ธ ์ ๋์ค๋ token์ผ๋ก user ์ ๋ณด ํ์ธํ๊ธฐ ๐ท ๊ด๋ฆฌ์์ธ์ง ํ์ธํ๊ธฐ role์ด admin ๊ณผ user๋ก ๋๋๋ค ๐ท ๊ด๋ฆฌ์์์ block ์ธ์ง unblock ์ธ์ง ๐ท ์ฝ๋ โ controllers/useCtrl.js const User = require("../models/User"); const bcrypt = require("bcrypt"); const asyncHandler = require("express-async-handler"); const { generateToken } = require("../config/jwtToken"); const { va..
=> Node.js E-Commerce App with REST API: Let's Build a Real-Life Example! ๐ท login ์ ์ ๋ณด๊ฐ ๋ง๋์ง ํ์ธํ๊ธฐ ๊ธฐ์กด ๋น๋ฐ๋ฒํธ์ ์ ๋ ฅํ ๋น๋ฐ๋ฒํธ๊ฐ ๋์ผํ์ง ํ์ธํ๊ธฐ ๐ท jwt (json webtoken) Header : ์๊ณ ๋ฆฌ์ฆ๊ณผ ํ์ Payload : ๋ฐ์ดํฐ SIGNATURE : ํ ํฐ์ ์ธ์ฝ๋ฉํ๊ฑฐ๋ ์ ํจ์ฑ ๊ฒ์ฆ์ ํ ๋, ์ฌ์ฉํ๋ ๊ณ ์ ํ ์ฝ๋ => Header ์ Payload๋ฅผ ํฉ์น ๋ฌธ์์ด์ BASE64๋ก ์ธ์ฝ๋ฉ => ์ธ์ฝ๋ฉํ ๊ฐ์ Secret Key๋ฅผ ์ด์ฉํด ํค๋์์ ์ ์ํ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ํด์ฑ => ์ด ๊ฐ์ ๋ค์ BASE64๋ก ์ธ์ฝ๋ฉํ์ฌ ์์ฑ ์ด ์ธ ๋ถ๋ถ์ ํฉ์ณ์ ์ํธํ๋ฅผ ํ ๊ฒ์ด JWT npm i jsonwebtoken ๐ท role์ผ๋ก ์ฌ..
=> Node.js E-Commerce App with REST API: Let's Build a Real-Life Example! ๐ท Async Request Handler async๋ฅผ ์ฝ๊ฒ ์ฌ์ฉ Request Handler๋ฅผ ์ฒ๋ฆฌํ๋๋ฐ ๊ณตํต์ ์ผ๋ก ์ค๋ฅ์ฒ๋ฆฌ๋ฅผ ํ ์ ์๊ฑฐ๋ ๊ฐ๋จํ๊ฒ ๊ตฌํ npm i express-async-handler โ request handler์์ ์ค๋ฅ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ - promise().catch(next) - async function, try ~ catch, next ๐ท bcrypt๋ก ๋น๋ฐ๋ฒํธ hash ์ ์ฉํ๊ธฐ ๐ท ์ฝ๋ โ index.js const express = require("express"); const bodyParser = require("body-parser");..
=> Node.js E-Commerce App with REST API: Let's Build a Real-Life Example! ๐ท User์ ํ์๊ฐ์ ๋ด์ญ์ MongoDB ์ฐ๋ ๐ท PostMan ํ์ธ ๐ท ์ฝ๋ โ models/User.js const mongoose = require("mongoose"); const UserSchema = new mongoose.Schema({ firstname: { type: String, required: true }, lastname: { type: String, required: true }, email: { type: String, required: true, unique: true }, mobile: { type: String, required: true..
=> Node.js E-Commerce App with REST API: Let's Build a Real-Life Example! ๐ท ํ๊ฒฝ ์ค์ ๐ package.json ๋ง๋ค์ด์ค๋ค npm init ๐ ํ์ํ ํจํค์ง ๋ค์ด๋ก๋ ํ๋ค npm i express mongoose bcrypt body-parser dotenv npm install nodemon --save ๐ ํด๋๋ฅผ ์์ฑํ๋ค ๐ ์๋ฒ์ MongoDB๋ฅผ ์ฐ๊ฒฐํ๋ค ๐ท ์ฝ๋ โ config/dbConnect.js const mongoose = require("mongoose"); require("dotenv").config(); // DB config const db = process.env.MONGODB_URI; // connect to Mongo..
=> Node.js With Passport Authentication | Full Project => authentication app with login, register and access control using Node.js, Express, Passport, Mongoose ๐ฅ passport๋ฅผ ์ฌ์ฉํ์ฌ ๋ก๊ทธ์ธ, ํ์๊ฐ์ ๊ธฐ๋ฅ์ ๊ตฌํํ๊ณ auth ์ธ์ฆ๊น์ง ์๋ฃํ๊ธฐ ์ฐธ๊ณ : https://github.com/gani0325/2023/tree/main/Web/PassportAuthentication ๐ง middlewares/auth.js module.exports = { ensureAuthenticated: (req, res, next) => { if (req.isAuthenticated()) {..
=> Node.js With Passport Authentication | Full Project => authentication app with login, register and access control using Node.js, Express, Passport, Mongoose ๐ฅ ๋ก๊ทธ์ธ์ ๋์์ฃผ๋ passpoart ์ด๋ฆ์ฒ๋ผ ์์ ์ ์น์ฌ์ดํธ์ ๋ฐฉ๋ฌธํ ๋ ์ฌ๊ถ๊ฐ์ ์ญํ ๋ก๊ทธ์ธ์ ์ฝ๊ฒ ํ ์ ์๊ฒ ๋์์ค // login์ด ์ต์ด๋ก ์ฑ๊ณตํ์ ๋๋ง ํธ์ถ๋๋ ํจ์ // done(null, user.id)๋ก ์ธ์ ์ ์ด๊ธฐํ passport.serializeUser(function (user, cb) { process.nextTick(function () { return cb(null, user); }); });..