Skip to content

Commit

Permalink
feat: implement base code for kakao oauth (#4)
Browse files Browse the repository at this point in the history
* chore: install next-auth

* feat: 기본 로그인 페이지 구현

* feat: oauth base code 작성

* fix: 불필요한 조건 분기문 제거
  • Loading branch information
jw-r authored Apr 23, 2024
1 parent cc993d1 commit 688025c
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"language": "en",
"ignorePaths": ["node_modules", ".next", "*.js", "*.json", "*.html", "*.css", "*.md", "*.svg"],
"files": ["src/**/*.tsx", "src/**/*.ts"],
"words": ["picktoss"]
"words": ["picktoss", "kakao", "nextauth"]
}
134 changes: 132 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"clsx": "^2.1.0",
"lucide-react": "^0.367.0",
"next": "14.1.4",
"next-auth": "^4.24.7",
"react": "^18",
"react-dom": "^18",
"tailwind-merge": "^2.2.2",
Expand Down
3 changes: 3 additions & 0 deletions public/icons/kakao.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Account, DefaultSession } from 'next-auth'
import NextAuth from 'next-auth/next'
import KakaoProvider from 'next-auth/providers/kakao'

declare module 'next-auth' {
interface Session {
user: {
id: string
accessToken: string
account: Account
} & DefaultSession['user']
}
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const handler = NextAuth({
providers: [
KakaoProvider({
clientId: process.env.AUTH_KAKAO_ID!,
clientSecret: process.env.AUTH_KAKAO_SECRET!,
}),
],
secret: process.env.NEXTAUTH_SECRET!,
callbacks: {
jwt: ({ token, account }) => {
/**
* TODO: Backend API 호출 with account.access_token
*/
token.accessToken = 'backend api access token'
token.account = account

return token
},

session: ({ session, token }) => {
session.user.id = token.sub || ''
session.user.accessToken = token.accessToken as string
session.user.account = token.account as Account

return session
},
},
})

export { handler as GET, handler as POST }
21 changes: 21 additions & 0 deletions src/app/signin/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use client'

import { signIn } from 'next-auth/react'
import { Button } from '@/components/ui/button'
import Image from 'next/image'

export default function SignIn() {
return (
<main className="flex justify-center">
<div className="mt-16">
<Button
className="w-full bg-[#FBE44D] text-[#3C1E1E] hover:bg-[#FBE44D]/80"
onClick={() => signIn('kakao')}
>
<Image src="/icons/kakao.svg" alt="" width={20} height={20} className="mr-[12px]" />
카카오 로그인
</Button>
</div>
</main>
)
}

0 comments on commit 688025c

Please sign in to comment.