@@ -4,7 +4,7 @@ import { JWT } from "next-auth/jwt";
44import CredentialsProvider from "next-auth/providers/credentials" ;
55import GithubProvider from "next-auth/providers/github" ;
66import GoogleProvider from "next-auth/providers/google" ;
7- import { signOut } from "next-auth/react" ;
7+ import { signIn , signOut } from "next-auth/react" ;
88import z from "zod" ;
99
1010interface credentialsTypes {
@@ -26,10 +26,28 @@ export const authOptions = {
2626 GithubProvider ( {
2727 clientId : process . env . GITHUB_ID || "" ,
2828 clientSecret : process . env . GITHUB_SECRET || "" ,
29+ profile ( profile ) {
30+ return {
31+ id : profile . id . toString ( ) ,
32+ name : profile . name || profile . login ,
33+ email : profile . email ,
34+ username : profile . login ,
35+ image : profile . avatar_url ,
36+ } ;
37+ } ,
2938 } ) ,
3039 GoogleProvider ( {
3140 clientId : process . env . GOOGLE_CLIENT_ID || "" ,
3241 clientSecret : process . env . GOOGLE_CLIENT_SECRET || "" ,
42+ profile ( profile ) {
43+ return {
44+ id : profile . sub ,
45+ name : profile . name ,
46+ email : profile . email ,
47+ username : profile . email . split ( "@" ) [ 0 ] ,
48+ image : profile . picture ,
49+ } ;
50+ } ,
3351 } ) ,
3452
3553 CredentialsProvider ( {
@@ -118,10 +136,12 @@ export const authOptions = {
118136 } ,
119137 } ) ,
120138 ] ,
121- Secret : process . env . NEXTAUTH_SECRET || "secr3t" ,
139+ secret : process . env . NEXTAUTH_SECRET || "secr3t" ,
122140
123141 callbacks : {
124- async jwt ( { token, user } : any ) {
142+ async jwt ( { token, user, account } : any ) {
143+ console . log ( "JWT Callback - User:" , user ) ;
144+ console . log ( "JWT Callback - Account:" , account ) ;
125145 if ( user ) {
126146 token . id = user . id ;
127147 token . username = user . username ;
@@ -141,6 +161,48 @@ export const authOptions = {
141161 }
142162 return session ;
143163 } ,
164+
165+ async signIn ( { user, profile, account } : any ) {
166+ try {
167+ const existingUser = await db . user . findFirst ( {
168+ where : {
169+ OR : [ { email : user . email } , { username : user . username } ] ,
170+ } ,
171+ } ) ;
172+
173+ if ( ! existingUser ) {
174+ await db . user . create ( {
175+ data : {
176+ // Make sure this matches your DB schema
177+ username :
178+ user . username ||
179+ ( account . provider === "github"
180+ ? profile . login
181+ : profile . email . split ( "@" ) [ 0 ] ) ,
182+ email : user . email || `${ user . id } @${ account . provider } .user` ,
183+ name : user . name ,
184+ password : "" ,
185+ // image: user.image,
186+ } ,
187+ } ) ;
188+ console . log ( "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" ) ;
189+
190+ console . log ( {
191+ id : user . id . toString ( ) ,
192+ name : user . name ,
193+ username : user . username ,
194+ } ) ;
195+ }
196+ return {
197+ id : user . id . toString ( ) ,
198+ name : user . name ,
199+ username : user . username ,
200+ } ;
201+ } catch ( error ) {
202+ console . error ( "SignIn Error " , error ) ;
203+ return false ;
204+ }
205+ } ,
144206 } ,
145207 pages : {
146208 signIn : "/signin" ,
0 commit comments