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)

λΈ”λ‘œκ·Έ 메뉴

  • ν™ˆ
  • νƒœκ·Έ
  • λ°©λͺ…둝

곡지사항

인기 κΈ€

νƒœκ·Έ

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

졜근 λŒ“κΈ€

졜근 κΈ€

ν‹°μŠ€ν† λ¦¬

hELLO Β· Designed By μ •μƒμš°.
arkhyeon

arkhyeon

NEXT

NEXT 14 NEXT-AUTH 5 middleware.ts not working

2024. 7. 11. 16:28
728x90
λ°˜μ‘ν˜•
πŸ’‘ Learn Nextμ—μ„œλŠ” src 폴더λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 경우, middleware.ts νŒŒμΌμ„ ν”„λ‘œμ νŠΈ λ£¨νŠΈμ— μœ„μΉ˜μ‹œν‚΅λ‹ˆλ‹€.
κ·ΈλŸ¬λ‚˜ src 폴더λ₯Ό μ‚¬μš©ν•˜λŠ” κ²½μš°μ—λŠ” νŒŒμΌμ„ src 폴더 내뢀에 μœ„μΉ˜μ‹œμΌœμ•Ό ν•©λ‹ˆλ‹€.

문제점

auth.ts - NextAuth - signIn ν•¨μˆ˜λŠ” μ œλŒ€λ‘œ λ™μž‘μ„ ν–ˆμœΌλ‚˜ middleware.ts에 μ—°κ²°λœ auth.config.ts 파일이 λ™μž‘μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

//auth.ts

async function getUser(email: string): Promise<User | null> {
  try {
    return await prisma.user.findUnique({where: { uid: 1 }})
  } catch (error) {
    console.error('Failed to fetch user:', error)
    throw new Error('Failed to fetch user.')
  }
}

export const { auth, signIn, signOut } = NextAuth({
  ...authConfig,
  providers: [
    Credentials({
      async authorize(credentials) {
        const parsedCredentials = z
          .object({ email: z.string().email(), password: z.string().min(6) })
          .safeParse(credentials)

        if (parsedCredentials.success) {
          const { email, password } = parsedCredentials.data
          const user = await getUser(email)

          if (!user) return null
          const passwordsMatch = password === user.password

          if (passwordsMatch) return user
        }

        console.log('Invalid credentials')
        return null
      },
    }),
  ],
})
//auth.config.ts

export const authConfig = {
  pages: {
    signIn: '/login',
  },
  callbacks: {
    authorized({ auth, request: { nextUrl } }) {
      const isLoggedIn = !!auth?.user
      // const isOnDashboard = nextUrl.pathname.startsWith('/product')
      const isPublicRoute = publicRoute.includes(nextUrl.pathname)
      const isAuthRoute = authRoute.includes(nextUrl.pathname)

      if (isPublicRoute && isLoggedIn) {
        return Response.redirect(new URL('/', nextUrl))
      }

      if (isAuthRoute) {
        return isLoggedIn
      }

      // if (isOnDashboard) {
      //   return isLoggedIn
      //   // Redirect unauthenticated users to login page
      // } else if (isLoggedIn) {
      //   return Response.redirect(new URL('/product', nextUrl))
      // }
      return true
    },
  },
  providers: [], // Add providers with an empty array for now
} satisfies NextAuthConfig
// src/middleware.ts
import NextAuth from 'next-auth'
import { authConfig } from '@/auth.config'

export default NextAuth(authConfig).auth

export const config = {
  // <https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher>
  matcher: ['/((?!api|_next/static|_next/image|.*\\\\.png$).*)'],
}

ν•΄κ²°

Learn Nextμ—μ„œλŠ” src 폴더λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šμ•„ middleware.ts 파일의 μœ„μΉ˜λ₯Ό ν”„λ‘œμ νŠΈ λ£¨νŠΈμ— μœ„μΉ˜ν•˜μ˜€μ§€λ§Œ src 폴더λ₯Ό μ‚¬μš©ν•˜λ©΄ src 폴더 내뢀에 파일이 μœ„μΉ˜ν•΄μ•Όν•œλ‹€.

728x90
λ°˜μ‘ν˜•

'NEXT' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

NEXT JS + Google Analytics Connect Setting  (0) 2024.08.07
Parallel Routes Modal Difficult Error  (0) 2024.07.25
NextJs Next-auth getSession() 둜그인 정보 확인  (0) 2024.07.23
NextJS Prisma PostgreSQL μ—°κ²° 및 Migrate  (0) 2024.07.09
next-themes Hydration failed Error  (0) 2024.05.31
    'NEXT' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
    • Parallel Routes Modal Difficult Error
    • NextJs Next-auth getSession() 둜그인 정보 확인
    • NextJS Prisma PostgreSQL μ—°κ²° 및 Migrate
    • next-themes Hydration failed Error
    arkhyeon
    arkhyeon

    ν‹°μŠ€ν† λ¦¬νˆ΄λ°”