Repository navigation
feat: 카카오 로그인 기능 구현 #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
de6d9d3
94d7a31
04d0afc
1c67829
98c9d9b
bf17eb8
424bf36
d776042
396f434
d2a9063
3220799
58e961a
3e294fc
0a8143e
1cf38c6
29b612f
e7e482e
1f30c28
8c70a4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| @use '@/styles/abstracts/variables' as v; | ||
| @use '@/styles/abstracts/functions' as fn; | ||
|
|
||
| .page { | ||
| display: flex; | ||
| flex-direction: column; | ||
| min-height: 100svh; | ||
| padding: fn.r(20) fn.r(24) fn.r(40); | ||
| } | ||
|
|
||
| .content { | ||
| flex: 1; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| .logo { | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| gap: fn.r(20); | ||
| } | ||
|
|
||
| .character { | ||
| width: 40vw; | ||
| max-width: fn.r(200); | ||
| height: auto; | ||
| image-rendering: pixelated; | ||
| } | ||
|
|
||
| .logoImage { | ||
| width: 45vw; | ||
| height: auto; | ||
| max-width: fn.r(200); | ||
| } | ||
|
|
||
| .footer { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: fn.r(12); | ||
| } | ||
|
|
||
| .kakaoButton { | ||
| background-color: #fee500 !important; | ||
| color: #000000 !important; | ||
| gap: fn.r(8); | ||
|
|
||
| &:hover:not(:disabled) { | ||
| background-color: #f5dc00 !important; | ||
| } | ||
| } | ||
|
|
||
| .disclaimer { | ||
| font-size: v.$font-size-caption; | ||
| font-weight: v.$font-weight-regular; | ||
| color: v.$color-black-400; | ||
| text-align: center; | ||
| line-height: v.$line-height-caption; | ||
| } | ||
|
|
||
| .disclaimerLink { | ||
| text-decoration: underline; | ||
| cursor: pointer; | ||
| color: inherit; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| 'use client'; | ||
|
|
||
| import Image from 'next/image'; | ||
| import Link from 'next/link'; | ||
| import { signIn } from 'next-auth/react'; | ||
| import Button from '@/components/ui/Button'; | ||
| import styles from './login.module.scss'; | ||
|
|
||
| export default function LoginPage() { | ||
| return ( | ||
| <div className={styles.page}> | ||
| <div className={styles.content}> | ||
| <div className={styles.logo}> | ||
| {/* eslint-disable-next-line @next/next/no-img-element */} | ||
| <img | ||
| src="/images/common/login_character.png" | ||
| alt="말해부엉 캐릭터" | ||
| className={styles.character} | ||
| /> | ||
| <Image | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이걸 이미지 처리했네요....?!?! 충분히 코드로 처리 가능할 것 같은뎁...
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 폰트를 쓰는곳이 로고밖에 없기때문에 이미지가 맞을거같아서 제가 미리 만들어둔거에요!
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아 비용이 들어가는 폰트라면 그게 나을 것 같네요. |
||
| src="/images/common/logo.png" | ||
| alt="말해부엉" | ||
| width={264} | ||
| height={76} | ||
| sizes="(max-width: 667px) 45vw, 200px" | ||
| className={styles.logoImage} | ||
| /> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div className={styles.footer}> | ||
| <Button | ||
| className={styles.kakaoButton} | ||
| onClick={() => signIn('kakao', { callbackUrl: '/home' })} | ||
| > | ||
| <KakaoIcon /> | ||
| 카카오로 로그인 하기 | ||
| </Button> | ||
| <p className={styles.disclaimer}> | ||
| 로그인 시{' '} | ||
| <Link href="/terms" className={styles.disclaimerLink}> | ||
| 이용약관 | ||
| </Link>{' '} | ||
| 및{' '} | ||
| <Link href="/privacy" className={styles.disclaimerLink}> | ||
| 개인정보 처리방침 | ||
| </Link> | ||
| 에 동의한 것으로 간주합니다. | ||
| </p> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function KakaoIcon() { | ||
| return ( | ||
| <svg | ||
| width="18" | ||
| height="18" | ||
| viewBox="0 0 18 18" | ||
| fill="none" | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| aria-hidden="true" | ||
| > | ||
| <path | ||
| fillRule="evenodd" | ||
| clipRule="evenodd" | ||
| d="M9 1.5C4.858 1.5 1.5 4.134 1.5 7.38c0 2.1 1.395 3.942 3.498 4.998L4.1 15.228a.188.188 0 0 0 .288.204l3.726-2.472A8.8 8.8 0 0 0 9 13.26c4.142 0 7.5-2.634 7.5-5.88S13.142 1.5 9 1.5Z" | ||
| fill="#000000" | ||
| /> | ||
| </svg> | ||
|
Comment on lines
+57
to
+71
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아이콘이 깨져보이는데 확인부탁드려요 |
||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,14 +1,63 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { NextAuthOptions } from 'next-auth' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import KakaoProvider from 'next-auth/providers/kakao' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { PrismaAdapter } from '@auth/prisma-adapter' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { prisma } from '@/lib/db' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { generateNickname } from './nickname' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const MAX_NICKNAME_RETRIES = 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async function saveFirstLoginFields(userId: string, kakaoId: string): Promise<void> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (let attempt = 0; attempt < MAX_NICKNAME_RETRIES; attempt++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isLastAttempt = attempt === MAX_NICKNAME_RETRIES - 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const nickname = isLastAttempt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? `부엉이${Date.now()}` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : generateNickname() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await prisma.user.update({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { id: userId }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data: { kakaoId, nickname, termsAgreedAt: new Date() }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const err = e as { code?: string; meta?: { target?: string[] } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (err.code === 'P2002' && err.meta?.target?.includes('nickname')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+9
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 모든 재시도 실패 시 오류가 발생하지 않습니다. 루프가 10회 모두 🐛 재시도 실패 시 오류를 던지도록 수정 throw e
}
}
+ throw new Error('고유 닉네임 생성 실패: 재시도 한도 초과')
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const authOptions: NextAuthOptions = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| adapter: PrismaAdapter(prisma) as NextAuthOptions['adapter'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| session: { strategy: 'jwt' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| providers: [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| KakaoProvider({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clientId: process.env.KAKAO_CLIENT_ID!, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clientSecret: process.env.KAKAO_CLIENT_SECRET!, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pages: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| signIn: '/login', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error: '/login', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callbacks: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async signIn({ user, account }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (account?.provider === 'kakao' && user.id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const existing = await prisma.user.findUnique({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where: { id: user.id }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| select: { kakaoId: true }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // kakaoId가 없으면 최초 로그인 → 커스텀 필드 설정 (닉네임 충돌 시 재시도) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!existing?.kakaoId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await saveFirstLoginFields(user.id, account.providerAccountId) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| jwt({ token, user }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (user) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token.sub = user.id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| const VERBS = [ | ||
| '날아다니는', '잠자는', '노래하는', '춤추는', '달리는', | ||
| '웃는', '생각하는', '먹는', '빛나는', '졸고있는', | ||
| '반짝이는', '행복한', '신나는', '귀여운', '멋진', | ||
| '느긋한', '용감한', '씩씩한', '지혜로운', '엉뚱한', | ||
| ] | ||
|
|
||
| export function generateNickname(): string { | ||
| const verb = VERBS[Math.floor(Math.random() * VERBS.length)] | ||
| const digits = String(Math.floor(Math.random() * 9000) + 1000) | ||
| return `${verb}부엉이${digits}` | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export { default } from 'next-auth/middleware' | ||
|
|
||
| export const config = { | ||
| matcher: [ | ||
| '/((?!login(?:/|$)|api/auth(?:/|$)|_next/static|_next/image|favicon\\.ico$|images(?:/|$)).*)', | ||
| ], | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mixin 사용 부탁드립니다