250x250
๋ฐ˜์‘ํ˜•
arkhyeon
arkhyeon
arkhyeon
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (88)
    • Spring (5)
    • Java (4)
    • React (25)
      • TypeScript (6)
      • JavaScript (1)
      • Jest (9)
    • NEXT (8)
    • SQL (1)
    • React native (1)
    • CSS (3)
    • Web (1)
    • Git (3)
    • ETC (6)
    • ๋น…๋ฐ์ดํ„ฐDB (8)
    • Docker (4)
    • Tool (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • node WebSocket
  • jest
  • usetransition
  • javasciprt websocket
  • javascript wss
  • websocket
  • react19
  • Spring WebSocket
  • react spring websocket
  • docker tomcat
  • react loading
  • HIVE
  • react websocket
  • websocket server
  • react jest
  • react usetransition
  • WSS
  • kudu
  • react
  • react typescript

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
arkhyeon

arkhyeon

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

Next_Auth Session JWT Type Extend

2024. 8. 2. 10:18
728x90
๋ฐ˜์‘ํ˜•
๐Ÿ’ก Next Auth JWT, Session ์‚ฌ์šฉ ์‹œ ์‚ฌ์šฉ์ž ์ •์˜ ์†์„ฑ(์˜ˆ: role)์„ auth(session)์—์„œ ์ถ”๊ฐ€ํ•˜๊ณ  User | AdapterUser ํƒ€์ž… ์„ค์ •์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํƒ€์ž… ์—๋Ÿฌ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์›ํ•˜๋Š” ์†์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ์„ค์ •ํ•œ๋‹ค.

type.d.ts ์„ค์ •

ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ํด๋”์— type.d.ts ํŒŒ์ผ ์ƒ์„ฑ ํ›„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅ ํ›„ tsconfig.json์— ๋“ฑ๋ก

//type.d.ts
import { JWT } from 'next-auth/jwt';
import NextAuth, { type DefaultSession } from 'next-auth';
declare module 'next-auth/jwt' {
  interface JWT {
	  //JWT์—์„œ ์‚ฌ์šฉํ•  ์‚ฌ์šฉ์ž ์ •์˜ ์†์„ฑ ๋“ฑ๋ก
    role: string;
  }
}
declare module 'next-auth' {
  export interface User extends DefaultUser {
	  //User | AdapterUser์—์„œ ์‚ฌ์šฉํ•  ์‚ฌ์šฉ์ž ์ •์˜ ์†์„ฑ ๋“ฑ๋ก
    role: string;
  }

  interface Session {
    user: {
  	  //Session์—์„œ ์‚ฌ์šฉํ•  ์‚ฌ์šฉ์ž ์ •์˜ ์†์„ฑ ๋“ฑ๋ก
      role: string;
    } & DefaultSession['user'];
  }
}
//tsconfig.json
{
  //compilerOptions...
  //paths...
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    ".next/types/**/*.ts",
    "app/lib/placeholder-data.js",
    "scripts/seed.js",
    "type.d.ts"  // <--- ๋“ฑ๋ก
  ],
  //exclude...
}

์‚ฌ์šฉ์ž ์ •์˜ ํƒ€์ž… ์„ ์–ธ ์‹œ next-env.d.ts๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ์€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋ฏ€๋กœ ๋ณ€๊ฒฝ๋œ ๋‚ด์šฉ์€ ๋ฎ์–ด์“ฐ์—ฌ์ง€๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  tsconfig.json์—์„œ ์ฐธ์กฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ

export async function getUserOne(): Promise<User> {
  const session = await auth();
	console.log(session.user.id);
	console.log(session.user.role);
}
export const authConfig = {
  pages: {
    signIn: '/login',
  },
  callbacks: {
		authorized({ auth, request: { nextUrl } }) {
	    const isAdmin = auth.user.role
	    const isAdminRoute = adminRoute.includes(nextUrl.pathname);
			//...
		},
	},
} satisfies NextAuthConfig;

 

 

Auth.js | Typescript

Authentication for the Web

authjs.dev

 

 

Configuring: TypeScript | Next.js

Next.js provides a TypeScript-first development experience for building your React application.

nextjs.org

 

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

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”