[next-auth]카카오로그인

코드왕·2023년 10월 5일
0

1.설명동영상

https://www.youtube.com/watch?v=EqpJTFP1pHw&t=143s

  1. 설명 블로그

https://velog.io/@seondal/Next-auth%EB%A1%9C-%EC%B9%B4%EC%B9%B4%EC%98%A4-%EC%86%8C%EC%85%9C-%EB%A1%9C%EA%B7%B8%EC%9D%B8-5%EB%B6%84%EB%A7%8C%EC%97%90-%EA%B5%AC%ED%98%84%ED%95%98%EA%B8%B0

  1. getServerSession시 null 뜨는이유

NEXTAUTH_SECRET을 넣어줘야한다.

//pages/api/auth/[...nextauth].js
import NextAuth from "next-auth"
import GithubProvider from "next-auth/providers/github"
import KakaoProvider from "next-auth/providers/kakao" // KakaoProvider 추가
import {createClient} from "@supabase/supabase-js"
import { getServerSession } from "next-auth/next" // Ensure this import

const supabase = createClient(process.env.NEXT_PUBLIC_SUPABASE_URL, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY)

export const authOptions = {
  // Configure one or more authentication providers
  secret: process.env.NEXTAUTH_SECRET,
  providers: [
    KakaoProvider({ // KakaoProvider 설정
      clientId: process.env.KAKAO_ID,
      clientSecret: process.env.KAKAO_SECRET,
    }),
    // ...add more providers here
  ],
  callbacks: {
    async jwt({ token, account, profile }) {
      // Persist the OAuth access_token to the token right after signin
      if (account) {
        token.accessToken = account.access_token
        console.log('profile:', profile)
        // Insert email into Supabase profiles table
        if (profile?.properties?.nickname) {
          await supabase
            .from('profiles')
            .upsert({ nickname: profile.properties.nickname })

        }
      }
      return token
    },
    async session({ session, token, user }) {
      // Send properties to the client, like an access_token from a provider.
      session.accessToken = token.accessToken
      return session
    }
  },
  // Ensure session strategy is set
  session: {
    strategy: 'jwt',
  },
}

export default NextAuth(authOptions)

// Example usage of getServerSession
export async function someServerSideFunction(context) {
  const session = await getServerSession(context.req, context.res, authOptions)
  if (!session) {
    // Handle the case where the session is null
    console.log('Session is null')
  }
  return session
}
  1. NEXTAUTH_SECRET은 git bash에 openssl rand -base64 32
    를 쳐주면 나타난다.
NEXT_PUBLIC_SUPABASE_URL=https://eqjneupuqfvpgjqfnvcy.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImVxam5ldXB1cWZ2cGdqcWZudmN5Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MTMwMjE5NTAsImV4cCI6MjAyODU5Nzk1MH0.4eP-VJrjDkmCOGVTEYw7ImmAyxx_QE93W04kSAhuBGI
KAKAO_ID=d0370ad827308dcb0ed5bb45eef8a8ac
KAKAO_SECRET=AVfJOromi9hb9JkVdzeAeY9aWKhfteIc
NEXTAUTH_SECRET=sbTtF/xEDVtRKpYG+OZxDQtLnxdzqHNG7J4YojTC6JA=
 
profile
CODE DIVE!

0개의 댓글