11import db from "@repo/db/client" ;
22import bcrypt from "bcrypt" ;
3- import { JWT } from "next-auth/jwt" ;
43import CredentialsProvider from "next-auth/providers/credentials" ;
54import GithubProvider from "next-auth/providers/github" ;
65import GoogleProvider from "next-auth/providers/google" ;
7- import { signIn , signOut } from "next-auth/react " ;
6+ import { Session } from "next-auth" ;
87import z from "zod" ;
98
9+ declare module "next-auth" {
10+ interface Session {
11+ user : {
12+ id : string ;
13+ name ?: string | null ;
14+ email ?: string | null ;
15+ image ?: string | null ;
16+ } ;
17+ }
18+ }
19+
1020interface credentialsTypes {
1121 username : string ;
1222 password : string ;
@@ -97,7 +107,7 @@ export const authOptions = {
97107 console . log ( "This is userEmail" , existingUser . email ) ;
98108 console . log ( "This is username" , existingUser . username ) ;
99109 return {
100- id : existingUser ? .id . toString ( ) ,
110+ id : existingUser . id . toString ( ) ,
101111 usernname : existingUser . username ,
102112 email : existingUser . email ,
103113 name : existingUser . name ,
@@ -127,6 +137,7 @@ export const authOptions = {
127137 id : user . id . toString ( ) ,
128138 name : user . name ,
129139 username : user . username ,
140+ email : user . email ,
130141 } ;
131142 } catch ( error ) {
132143 console . log ( error , "Not able to Create new user" ) ;
@@ -142,6 +153,8 @@ export const authOptions = {
142153 async jwt ( { token, user, account } : any ) {
143154 console . log ( "JWT Callback - User:" , user ) ;
144155 console . log ( "JWT Callback - Account:" , account ) ;
156+ console . log ( "OOOOOOOOOOO" , token . id ) ;
157+
145158 if ( user ) {
146159 token . id = user . id ;
147160 token . username = user . username ;
@@ -150,15 +163,16 @@ export const authOptions = {
150163 return token ;
151164 } ,
152165 async session ( { session, token } : any ) {
153- // const user = await db.user.findUnique({
154- // where: { id: token.sub },
155- // });
166+ console . log ( "Session Callback - Token:" , token ) ;
167+ console . log ( "Session Callback - Initial Session:" , session ) ;
156168
157169 if ( token && session . user ) {
158- session . user . id = token . id || null ;
170+ session . user . id = token . id as string ;
159171 session . user . username = token . username || null ;
160172 session . user . email = token . email || null ;
161173 }
174+
175+ console . log ( "Session Callback - Updated Session:" , session ) ;
162176 return session ;
163177 } ,
164178
@@ -171,9 +185,8 @@ export const authOptions = {
171185 } ) ;
172186
173187 if ( ! existingUser ) {
174- await db . user . create ( {
188+ const newUser = await db . user . create ( {
175189 data : {
176- // Make sure this matches your DB schema
177190 username :
178191 user . username ||
179192 ( account . provider === "github"
@@ -185,13 +198,14 @@ export const authOptions = {
185198 // image: user.image,
186199 } ,
187200 } ) ;
188- console . log ( "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" ) ;
189-
190- console . log ( {
191- id : user . id . toString ( ) ,
192- name : user . name ,
193- username : user . username ,
194- } ) ;
201+ user . id = newUser . id . toString ( ) ;
202+ // console.log({
203+ // id: user.id.toString(),
204+ // name: user.name,
205+ // username: user.username,
206+ // });
207+ } else {
208+ user . id = existingUser . id . toString ( ) ;
195209 }
196210 return true ;
197211 } catch ( error ) {
0 commit comments