|  | 
| 1 | 1 | import NextAuth, { AuthError } from 'next-auth'; | 
| 2 |  | -// import { OAuthAccountNotLinked } from '@auth/core/errors'; | 
| 3 | 2 | import Google from 'next-auth/providers/google'; | 
| 4 | 3 | import Github from 'next-auth/providers/github'; | 
| 5 | 4 | import Credentials from 'next-auth/providers/credentials'; | 
| 6 | 5 | import { DrizzleAdapter } from '@auth/drizzle-adapter'; | 
| 7 | 6 | import { db } from '@/db'; | 
| 8 |  | -import { | 
| 9 |  | -  getUserById, | 
| 10 |  | -  getUserByProviderAccountId, | 
| 11 |  | -  loginUser, | 
| 12 |  | -} from './db/query/User'; | 
|  | 7 | +import { getUserById, loginUser } from './db/query/User'; | 
| 13 | 8 | import bcrypt from 'bcryptjs'; | 
| 14 | 9 | import { encode, decode } from 'next-auth/jwt'; | 
| 15 |  | -import { cookies } from 'next/headers'; | 
| 16 | 10 | 
 | 
| 17 | 11 | class InvalidCredentialsError extends AuthError { | 
| 18 | 12 |   code = 'invalid-credentials'; | 
| @@ -71,35 +65,14 @@ export const { handlers, signIn, signOut, auth } = NextAuth({ | 
| 71 | 65 |     }), | 
| 72 | 66 |   ], | 
| 73 | 67 |   callbacks: { | 
| 74 |  | -    async signIn({ account }) { | 
| 75 |  | -      const cookieStore = cookies(); | 
| 76 |  | -      const session = cookieStore.has('authjs.session-token'); | 
| 77 |  | -      // If not logged in, let user login | 
| 78 |  | -      if (!session) { | 
| 79 |  | -        return true; | 
| 80 |  | -      } | 
| 81 |  | -      // If already logged in, and try to connect another account, throw error if already linked | 
| 82 |  | -      if (account?.provider === 'github' || account?.provider === 'google') { | 
| 83 |  | -        // check if user already exists with this account.providerAccountId | 
| 84 |  | -        const existingUser = await getUserByProviderAccountId( | 
| 85 |  | -          account?.providerAccountId as string, | 
| 86 |  | -        ); | 
| 87 |  | -        if (existingUser) { | 
| 88 |  | -          return '/error?error=OAuthAccountNotLinked'; | 
| 89 |  | -        } else { | 
| 90 |  | -          return true; | 
| 91 |  | -        } | 
| 92 |  | -      } | 
| 93 |  | -      return true; | 
| 94 |  | -    }, | 
| 95 | 68 |     authorized({ auth, request: { nextUrl } }) { | 
| 96 | 69 |       const isLoggedIn = !!auth?.user; | 
| 97 | 70 |       const paths = ['/profile', '/dashboard']; | 
| 98 | 71 |       const isProtected = paths.some((path) => | 
| 99 | 72 |         nextUrl.pathname.startsWith(path), | 
| 100 | 73 |       ); | 
| 101 | 74 | 
 | 
| 102 |  | -      const publicPath = ['/sign-in', '/sign-up']; | 
|  | 75 | +      const publicPath = ['/sign-up']; | 
| 103 | 76 |       const isPublic = publicPath.some((path) => | 
| 104 | 77 |         nextUrl.pathname.startsWith(path), | 
| 105 | 78 |       ); | 
|  | 
0 commit comments