Skip to content

Conversation

@SSUK-H
Copy link
Collaborator

@SSUK-H SSUK-H commented Jan 6, 2025

๐Ÿ“์ž‘์—… ๋‚ด์šฉ

  • ์นด์นด์˜ค Developers ์•ฑ ์ƒ์„ฑ ๋ฐ Client Secret ์ƒ์„ฑ
  • ๋„๋ฉ”์ธ๊ณผ Redirect URL ๋“ฑ๋ก
  • env ์นด์นด์˜ค ๊ด€๋ จ ํ‚ค ์ถ”๊ฐ€ (๋…ธ์…˜์— ์ž‘์„ฑํ•ด๋’€์Šต๋‹ˆ๋‹ค.)
  • ์นด์นด์˜ค ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์— signIn('kakao') ํ•ธ๋“ค๋Ÿฌ ์ถ”๊ฐ€
  • KakaoProvider ์ถ”๊ฐ€๋กœ ์นด์นด์˜ค ๋กœ๊ทธ์ธ ์ง€์›
    • clientId์™€ clientSecret ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ ์šฉ
  • ์นด์นด์˜ค ๋กœ๊ทธ์ธ ํ›„ ์ธ์ฆ ๋ฐ›๊ณ  ํ† ๊ทผ ๋ฐฑ์—”๋“œ์— ์ „๋‹ฌํ•˜๋ฉฐ ๋กœ๊ทธ์ธ ์š”์ฒญ
    • ์นด์นด์˜ค ๋กœ๊ทธ์ธ API ํ•จ์ˆ˜ ๋ฐ ํƒ€์ž… ์ •์˜
    • ์นด์นด์˜ค ๋กœ๊ทธ์ธ ์‹œ ์„œ๋ฒ„ ๊ฒ€์ฆ ๋กœ์ง ์ถ”๊ฐ€ (postKaKaoLogin)
    • next-auth ํƒ€์ž… ์ •์˜ ์ˆ˜์ • ๋ฐ ํ™•์žฅ
    • ์„ฑ๊ณต ์‹œ ์‚ฌ์šฉ์ž ์ •๋ณด ๋งคํ•‘

โœจPR Point

kakao Login ๋กœ์ง ์ถ”๊ฐ€ ์™ธ ๊ธฐ์กด ์ฝ”๋“œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ

์นด์นด์˜ค ๋กœ๊ทธ์ธ์—์„œ๋„ ๋™์ผํ•˜๊ฒŒ ํ•„์š”ํ•ด์„œ ๋ณ€๊ฒฝ๋œ ๋กœ๊ทธ์ธ API Response ํƒ€์ž… ๋ฐ˜์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

export type TTokenResponse = {
  id: number;
  username: string;
  email: string;
  accessToken: string;
  refreshToken: string;
  expiredAt: string;
};

ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ authorize ์—๋Ÿฌ ๋ฐœ์ƒํ•˜์—ฌ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

'(credentials: Record<"name" | "email" | "password", string> | undefined) => Promise<{ id: string | undefined; name: string | undefined; email: string | undefined; accessToken: string | undefined; refreshToken: string | undefined; expiredAt: string | undefined; } | null>' ํ˜•์‹์€ '(credentials: Record<"name" | "email" | "password", string> | undefined, req: Pick<RequestInternal, "body" | "query" | "headers" | "method">) => Awaitable<...>' ํ˜•์‹์— ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 
'Promise<{ id: string | undefined; name: string | undefined; email: string | undefined; accessToken: string | undefined; refreshToken: string | undefined; expiredAt: string | undefined; } | null>' ํ˜•์‹์€ 'Awaitable<User | null>' ํ˜•์‹์— ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 
'Promise<{ id: string | undefined; name: string | undefined; email: string | undefined; accessToken: string | undefined; refreshToken: string | undefined; expiredAt: string | undefined; } | null>' ํ˜•์‹์€ 'PromiseLike<User | null>' ํ˜•์‹์— ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 
'then' ์†์„ฑ์˜ ํ˜•์‹์ด ํ˜ธํ™˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 
'<TResult1 = { id: string | undefined; name: string | undefined; email: string | undefined; accessToken: string | undefined; refreshToken: string | undefined; expiredAt: string | undefined; } | null, TResult2 = never>(onfulfilled?: ((value: { ...; } | null) => TResult1 | PromiseLike<...>) | ... 1 more ... | undefined...' ํ˜•์‹์€ '<TResult1 = User | null, TResult2 = never>(onfulfilled?: ((value: User | null) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => PromiseLike<...>' ํ˜•์‹์— ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 
'onfulfilled' ๋ฐ 'onfulfilled' ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ํ˜•์‹์ด ํ˜ธํ™˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 
'value' ๋ฐ 'value' ๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ํ˜•์‹์ด ํ˜ธํ™˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 
'{ id: string | undefined; name: string | undefined; email: string | undefined; accessToken: string | undefined; refreshToken: string | undefined; expiredAt: string | undefined; } | null' ํ˜•์‹์€ 'User | null' ํ˜•์‹์— ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 
'{ id: string | undefined; name: string | undefined; email: string | undefined; accessToken: string | undefined; refreshToken: string | undefined; expiredAt: string | undefined; }' ํ˜•์‹์€ 'User' ํ˜•์‹์— ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 'id' ์†์„ฑ์˜ ํ˜•์‹์ด ํ˜ธํ™˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
 'string | undefined' ํ˜•์‹์€ 'string' ํ˜•์‹์— ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 
 'undefined' ํ˜•์‹์€ 'string' ํ˜•์‹์— ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.ts(2322) credentials.d.ts(13, 5): ํ•„์š”ํ•œ ํ˜•์‹์€ ์—ฌ๊ธฐ์—์„œ 'UserCredentialsConfig<{ name: { label: string; type: string; placeholder: string; }; email: { label: string; type: string; placeholder: string; }; password: { label: string; type: string; placeholder: string; }; }>' ํ˜•์‹์— ์„ ์–ธ๋œ 'authorize' ์†์„ฑ์—์„œ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. 
 (property) authorize: (credentials: Record<"name" | "email" | "password", string> | undefined, req: Pick<RequestInternal, "body" | "query" | "headers" | "method">) => Awaitable<User | null>

credentials ์—†๊ฑฐ๋‚˜ API ํ˜ธ์ถœ ์‹คํŒจ ์‹œ null ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

api ํ˜ธ์ถœ ์—๋Ÿฌ๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์–ด๋ ค์›Œ try-catch๋กœ ๊ฐ์‹ธ ์—๋Ÿฌ ํ•ธ๋“ค๋ง์„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค. authorizeํ•จ์ˆ˜์—์„œ ๊ธฐ์กด ๋กœ๊ทธ์ธ ํผ์—์„œ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ๋กœ id, name, email์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ์–ด, ๋กœ๊ทธ์ธ/ํšŒ์›๊ฐ€์ž… API ํ˜ธ์ถœ ์„ฑ๊ณต ์‹œ ์‘๋‹ต๊ฐ’์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, API ํ˜ธ์ถœ์˜ success ๊ฐ’์ด false์ผ ๋•Œ ์—๋Ÿฌ ๋กœ๊ทธ ์ถœ๋ ฅ ์ฝ”๋“œ๋ฅผ ํ•œ ์ค„๋กœ ๊ฐ„์†Œํ™”ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

User ํƒ€์ž…์„ ํ™•์žฅ ํ–ˆ์œผ๋ฉฐ, interface ์ฝ”๋“œ์ปจ๋ฒค์…˜์ด ์•ž์— I๋ฅผ ๋ช…์‹œํ•ด์ค˜์•ผํ•˜๋Š”๋ฐ Next auth์—์„œ User ํƒ€์ž…์„ ์ฐพ์ง€ ๋ชปํ•ด ์ œ๊ฑฐํ•˜์˜€๊ณ , lint ๋ฌด์‹œ ์ฒ˜๋ฆฌ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

  // eslint-disable-next-line @typescript-eslint/naming-convention
  interface User extends DefaultUser {
    accessToken?: string;
    refreshToken?: string;
    expiredAt?: string;
  }

๊ธฐ์กด Session ํƒ€์ž…์„ ํ™•์žฅํ–ˆ๋Š”๋ฐ DefaultSession['user']์— ์ด๋ฏธ ํฌํ•จ๋œ name, email ์ค‘๋ณต๋˜์–ด ์ œ๊ฑฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๊ถ๊ธˆํ•˜์‹  ๋ถ€๋ถ„์€ ๋ง์”€ํ•ด ์ฃผ์„ธ์š”! ์ž์„ธํžˆ ์„ค๋ช… ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

SSUK-H added 2 commits January 6, 2025 10:55
- API ์‘๋‹ต๊ฐ’ ๋ณ€๊ฒฝ ์‚ฌํ•ญ ๋ฐ˜์˜
  - ์ค‘๋ณต๋œ `id`, `name`, `email` ์ œ๊ฑฐ
  - API ์‘๋‹ต๊ฐ’(`id`, `username`, `email`) ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋ฆฌํ„ด
- ํฌ๋ฆฌ๋ด์…œ ๋ฏธ์ œ๊ณต ์‹œ `null` ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ˆ˜์ •
- `try-catch` ๋ธ”๋ก ์ถ”๊ฐ€๋กœ ์—๋Ÿฌ ๋กœ๊ทธ ์ถœ๋ ฅ ๋ฐ ํ•œ ์ค„๋กœ ์ฒ˜๋ฆฌ ๊ฐ„์†Œํ™”
- ์นด์นด์˜ค ๋กœ๊ทธ์ธ API ํ•จ์ˆ˜ ๋ฐ ํƒ€์ž… ์ •์˜
- ์นด์นด์˜ค ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์— signIn('kakao') ํ•ธ๋“ค๋Ÿฌ ์ถ”๊ฐ€
- KakaoProvider ์ถ”๊ฐ€๋กœ ์นด์นด์˜ค ๋กœ๊ทธ์ธ ์ง€์›
  - `clientId`์™€ `clientSecret` ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ ์šฉ
  - ์นด์นด์˜ค ๋กœ๊ทธ์ธ ์‹œ ์„œ๋ฒ„ ๊ฒ€์ฆ ๋กœ์ง ์ถ”๊ฐ€ (`postKaKaoLogin`)
  - ์„ฑ๊ณต ์‹œ ์‚ฌ์šฉ์ž ์ •๋ณด ๋งคํ•‘
- next-auth ํƒ€์ž… ์ •์˜ ์ˆ˜์ • ๋ฐ ํ™•์žฅ
  - `Session` ํƒ€์ž…์—์„œ `DefaultSession['user']`์— ์ด๋ฏธ ํฌํ•จ๋œ `name`, `email` ์ œ๊ฑฐ
  - `User` ํƒ€์ž…์„ `DefaultUser`๋กœ ํ™•์žฅํ•˜์—ฌ ๊ธฐ์กด ์ค‘๋ณต๋œ ํ•„๋“œ(`id`, `name`, `email`) ์ œ๊ฑฐ
  - `User` ํƒ€์ž…์— `expiredAt` ํ•„๋“œ ์ถ”๊ฐ€
@SSUK-H SSUK-H added ๐Ÿ› ๏ธrefactor๐Ÿ› ๏ธ ๊ธฐ๋Šฅ ๋ณ€๊ฒฝ ์—†์ด ์ฝ”๋“œ ๊ตฌ์กฐ๋ฅผ ๊ฐœ์„ ํ•ฉ๋‹ˆ๋‹ค. โœจfeatโœจ ๊ธฐ๋Šฅ์„ ์ •์˜ํ•˜๊ณ  ๊ฐœ๋ฐœํ•ฉ๋‹ˆ๋‹ค labels Jan 6, 2025
@SSUK-H SSUK-H added this to the 2์ฐจ ๊ฐœ๋ฐœ milestone Jan 6, 2025
@SSUK-H SSUK-H self-assigned this Jan 6, 2025
@github-actions
Copy link

github-actions bot commented Jan 6, 2025

โœ… GitHub Actions ์‹คํ–‰ ๊ฒฐ๊ณผ

  • Build and Test: ์„ฑ๊ณต ๐ŸŽ‰
  • Lint: ์„ฑ๊ณต ๐ŸŽ‰

@HaydenDevK HaydenDevK self-requested a review January 8, 2025 08:08
@SSUK-H SSUK-H merged commit 116c3dd into dev Jan 14, 2025
5 checks passed
@SSUK-H SSUK-H deleted the 129/new branch January 14, 2025 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

โœจfeatโœจ ๊ธฐ๋Šฅ์„ ์ •์˜ํ•˜๊ณ  ๊ฐœ๋ฐœํ•ฉ๋‹ˆ๋‹ค ๐Ÿ› ๏ธrefactor๐Ÿ› ๏ธ ๊ธฐ๋Šฅ ๋ณ€๊ฒฝ ์—†์ด ์ฝ”๋“œ ๊ตฌ์กฐ๋ฅผ ๊ฐœ์„ ํ•ฉ๋‹ˆ๋‹ค.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants