ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ๋กœ๊ทธ์ธ API๊ตฌํ˜„
    ์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ 2023. 11. 9. 19:36

    <aside> ๐Ÿ’ก ๋กœ๊ทธ์ธ์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ •๋ฆฌํ•˜๊ธฐ!

    1. email, password๋ฅผ ์ „๋‹ฌ ๋ฐ›์Œ
    2. email์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ DB์— ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์ฆ
    3. ์‚ฌ์šฉ์ž๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ์‚ฌ์šฉ์ž์™€ ์ž…๋ ฅ๋ฐ›์€ password๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ๊ฒ€์ฆ
    4. JWT ์ƒ์„ฑ ํ›„ Cookie ๋ฐ Body๋กœ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌ
    5. ๋กœ๊ทธ์ธ ์„ฑ๊ณต! </aside>

    ๋กœ๊ทธ์ธ์— ์‚ฌ์šฉํ•˜๋Š” JWTํ† ํฐ์˜ ์˜ˆ์‹œ

     

    const token = jwt.sign({ userId: user.userId }, "customized-secret-key");

     

     

    ๋กœ๊ทธ์ธ ๊ตฌํ˜„ ํ•˜๋Š” ๋กœ์ง EX)

     

    // routes/auth.js

    const jwt = require("jsonwebtoken");
    const express = require("express");
    const router = express.Router();

    const User = require("../schemas/user");

    // ๋กœ๊ทธ์ธ API
    router.post("/auth", async (req, res) => {
      const { email, password } = req.body;

      const user = await User.findOne({ email });

      // NOTE: ์ธ์ฆ ๋ฉ”์„ธ์ง€๋Š” ์ž์„ธํžˆ ์„ค๋ช…ํ•˜์ง€ ์•Š๋Š”๊ฒƒ์„ ์›์น™์œผ๋กœ ํ•œ๋‹ค.
      if (!user || password !== user.password) {
        res.status(400).json({
          errorMessage: "์ด๋ฉ”์ผ ๋˜๋Š” ํŒจ์Šค์›Œ๋“œ๊ฐ€ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.",
        });
        return;
      }

      const token = jwt.sign(
        { userId: user.userId },
        "customized-secret-key",
      );

    res.cookie("Authorization", `Bearer ${token}`); // JWT๋ฅผ Cookie๋กœ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค!
      res.status(200).json({ token }); // JWT๋ฅผ Body๋กœ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค!
    });

    module.exports = router;

     

     

      1. โœ… ์™œ ๊ฒฝ๋กœ๊ฐ€ /auth ์ธ๊ฐ€์š”?
      Authenticate๋ฅผ ์ค„์ธ ๋‹จ์–ด์ธ๋ฐ, ๋กœ๊ทธ์ธ ํ•œ๋‹ค๋Š” ํ–‰์œ„ ์ž์ฒด๋ฅผ "์‚ฌ์šฉ์ž๊ฐ€ ์ž์‹ ์˜ ์ •๋ณด๋ฅผ ์ธ์ฆํ•œ๋‹ค" ๋ผ๊ณ  ๋ณด๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์œผ๋กœ ๋กœ๊ทธ์ธ์— ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ๋กœ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค!

     

     

    ๋‚ด ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๋Š” API

     

    // routes/users.js

    const authMiddleware = require("./middlewares/auth-middleware");

    ...

    // ๋‚ด ์ •๋ณด ์กฐํšŒ API
    router.get("/users/me", authMiddleware, async (req, res) => {
      const { email, nickname } = res.locals.user;
      
      res.status(200).json({
        user: { email, nickname }
      });
    });


     

    ์ƒํ’ˆ ๋ชฉ๋ก ์กฐํšŒ API ์ˆ˜์ • ์ฝ”๋“œ ์˜ˆ์‹œ)

     

    // routes/goods.js


    // ์ƒํ’ˆ ๋ชฉ๋ก ์กฐํšŒ API
    router.get("/goods", async (req, res) => {
      const { category } = req.query;

      const goods = await Goods.find(category ? { category } : {})
        .sort("-date")
        .exec();

      const results = goods.map((item) => {
        return {
          goodsId: item.goodsId,
          name: item.name,
          price: item.price,
          thumbnailUrl: item.thumbnailUrl,
          category: item.category,
        };
      });

      res.json({ goods: results });
    });

     

    ์ƒํ’ˆ ์ƒ์„ธ ์กฐํšŒ API ์ˆ˜์ • ์ฝ”๋“œ ์˜ˆ์‹œ

    // routes/goods.js


    // ์ƒํ’ˆ ๋ชฉ๋ก ์กฐํšŒ API
    router.get("/goods", async (req, res) => {
      const { category } = req.query;

      const goods = await Goods.find(category ? { category } : {})
        .sort("-date")
        .exec();

      const results = goods.map((item) => {
        return {
          goodsId: item.goodsId,
          name: item.name,
          price: item.price,
          thumbnailUrl: item.thumbnailUrl,
          category: item.category,
        };
      });

      res.json({ goods: results });
    });

     

     

    ์ฝ”๋“œ ์Šค๋‹ˆํŽซ ์˜ˆ์‹œ๋ฅผ ์˜ฌ๋ฆฐ์ด์œ ๋Š” ๋‚ด๊ฐ€ ์ข€ ๋” ์ด ๋กœ์ง์„ ๋ฐ˜๋ณตํ•ด์„œ ๋ณด๋ฉด์„œ ๋ˆˆ์— ์ต์ˆ™ํ•ด์ง€๊ณ  ์†์— ์ต์ˆ™ํ•ด ์กŒ์œผ๋ฉด ํ•˜๋Š” ๋งˆ์Œ์— ์ ์–ด ์˜ฌ๋ ธ๋‹ค.

Designed by Tistory.