WEB/Typescript

[Typescript] Property 'session' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.ts(2339) ์—๋Ÿฌ

kite707 2023. 5. 7.

๐Ÿ“Œ ๋ฌธ์ œ ์ƒํ™ฉ

require๋ฌธ์„ import๋กœ ๋ฐ”๊พธ๋Š” ์ž‘์—…์„ ํ•˜๋˜ ๋„์ค‘ const express = require(’express’)๋ฅผ import express from ‘express’๋กœ ๋ฐ”๊ฟจ๋”๋‹ˆ ์•„๋ž˜ ๋ถ€๋ถ„์—์„œ Property 'session' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.ts(2339) ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ•ด๊ฒฐ ๋ฐฉ์•ˆ

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € @types/express-session์„ ์„ค์น˜ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ํ•˜์ž.

npm install -D @types/express-session@1.17.0

์ดํ›„ ๊ทธ๋Ÿฌ๊ณ ๋‚˜๋ฉด req.logout๊ณผ req.session.destroy๊ฐ€ ๊ฐ๊ฐ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์š”๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— req.logout์ด ๋˜๋ฉด req.session.destroy๋ฅผ ํ•˜๋„๋ก ํ•จ์ˆ˜๋ฅผ ์งœ์ฃผ์—ˆ๋‹ค. ์ดํ›„ ์„ฑ๊ณตํ–ˆ๋‹ค๋Š” ๊ฐ’์„ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” success ํ•จ์ˆ˜(์ปค์Šคํ…€ ํ•จ์ˆ˜)๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ์—ˆ๋‹ค.

import { Request, Response } from 'express';
import { responseMessage, statusCode } from '../modules/constants';
import { success } from '../modules/util';
const logout = async (req: Request, res: Response) => {
  req.logout(() => {
    req.session.destroy(() => {
      success(res, statusCode.OK, responseMessage.SUCCESS, {
        session: req.session,
      });
    });
  });
};

export { logout };

์œ„์™€ ๊ฐ™์ด ํ•˜๋ฉด ์—๋Ÿฌ๋Š” ํ•ด๊ฒฐ๋œ๋‹ค.

 

โญ๏ธ req.logout vs req.session.destroy

req.logout๊ณผ req.session.destroy์˜ ์ฐจ์ด๊ฐ€ ๊ถ๊ธˆํ•ด์ ธ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ˜์†”์— ๊ฐ’์„ ์ฐ์–ด๋ดค๋‹ค.

import { Request, Response } from 'express';
import { responseMessage, statusCode } from '../modules/constants';
import { success } from '../modules/util';
const logout = async (req: Request, res: Response) => {
  console.log(`session before logout ${JSON.stringify(req.session, null, 4)}`);
  req.logout(() => {
    console.log(`session after logout ${JSON.stringify(req.session, null, 4)}`);
    req.session.destroy(() => {
      console.log(
        `session after destroy ${JSON.stringify(req.session, null, 4)}`,
      );
      success(res, statusCode.OK, responseMessage.SUCCESS, {
        session: req.session,
      });
    });
  });
};

export { logout };

๊ทธ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

๋กœ๊ทธ์•„์›ƒ์„ ํ•˜๋ฉด passport property๊ฐ€ ์‚ฌ๋ผ์ง€๊ณ  destroy ์ดํ›„์—๋Š” session์ด undefined๋กœ ๋ฐ”๋€๋‹ค.

 

๊ทธ๋Ÿฐ๋ฐ ์‚ฌ์‹ค ๋กœ๊ทธ์•„์›ƒ์„ ์•ˆํ•˜๊ณ  ๋ฐ”๋กœ req.session.destroy๋ฅผ ํ•ด๋„ session์€ undefined๊ฐ€ ์ฐํžŒ๋‹ค. logout์„ ์ƒ๋žตํ•œ ์ฝ”๋“œ์™€ ๊ทธ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๊ฐ๊ฐ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

import { Request, Response } from 'express';
import { responseMessage, statusCode } from '../modules/constants';
import { success } from '../modules/util';
const logout = async (req: Request, res: Response) => {
  console.log(`session before logout ${JSON.stringify(req.session, null, 4)}`);
  req.session.destroy(() => {
    console.log(
      `session after destroy ${JSON.stringify(req.session, null, 4)}`,
    );
    success(res, statusCode.OK, responseMessage.SUCCESS, {
      session: req.session,
    });
  });
};

export { logout };

๊ทธ๋ž˜์„œ ์ฑ—GPT์—๊ฒŒ ๋ฌผ์–ด๋ณธ ๊ฒฐ๊ณผ ๊ฒฐ๋ก ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.


session.logout์€ ๋‹จ์ˆœํžˆ Passport์— ์˜ํ•ด ์ถ”๊ฐ€๋œ req.user property๋ฅผ ์ง€์šฐ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ servier-side session์— ์œ ์ €์™€ ๊ด€๋ จ๋œ ์ •๋ณด๊ฐ€ ๋‚จ์•„์žˆ์„ ๊ฒƒ์„ ๋Œ€๋น„ํ•˜์—ฌ session.destroy๊นŒ์ง€ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์—ฐ์ด์–ด ์˜ค๋Š” ๋‹ค๋ฅธ ์š”์ฒญ(Request)์ด ์œ ์ €๊ฐ€ ๋กœ๊ทธ์•„์›ƒ์„ ํ•œ ์ƒํƒœ์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์œ ์ € ๊ด€๋ จ ์ •๋ณด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋Œ“๊ธ€