@@ -4,6 +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" ;
78import z from "zod" ;
89
910interface credentialsTypes {
@@ -15,8 +16,8 @@ interface credentialsTypes {
1516
1617const userInput = z . object ( {
1718 username : z . string ( ) ,
18- name : z . string ( ) ,
19- email : z . string ( ) . email ( ) ,
19+ name : z . string ( ) . optional ( ) ,
20+ email : z . string ( ) . email ( ) . optional ( ) ,
2021 password : z . string ( ) ,
2122} ) ;
2223
@@ -52,7 +53,11 @@ export const authOptions = {
5253 name : creds . name ,
5354 } ) ;
5455 } ;
55-
56+ const validationResult = validatedUserInput ( credentials ) ;
57+ if ( ! validationResult . success ) {
58+ console . log ( "Validation failed" , validationResult . error ) ;
59+ return null ;
60+ }
5661 if ( ! validatedUserInput ) return null ;
5762
5863 const hashedPassword = await bcrypt . hash ( credentials . password , 10 ) ;
@@ -70,7 +75,6 @@ export const authOptions = {
7075 credentials . password ,
7176 existingUser . password
7277 ) ;
73- console . log ( "This is the password" , passwordValidation ) ;
7478 if ( passwordValidation ) {
7579 console . log ( "This is userEmail" , existingUser . email ) ;
7680 console . log ( "This is username" , existingUser . username ) ;
@@ -80,8 +84,9 @@ export const authOptions = {
8084 email : existingUser . email ,
8185 name : existingUser . name ,
8286 } ;
87+ } else {
88+ console . log ( "Invalid password for existing user" ) ;
8389 }
84-
8590 console . log ( "This is name" , existingUser . name ) ;
8691 } catch ( error ) {
8792 console . log ( "Error while LogIn" , error ) ;
@@ -119,6 +124,8 @@ export const authOptions = {
119124 async jwt ( { token, user } : any ) {
120125 if ( user ) {
121126 token . id = user . id ;
127+ token . username = user . username ;
128+ token . email = user . email ;
122129 }
123130 return token ;
124131 } ,
@@ -127,14 +134,17 @@ export const authOptions = {
127134 // where: { id: token.sub },
128135 // });
129136
130- if ( token ) {
131- session . user . id = token . sub ;
137+ if ( token && session . user ) {
138+ session . user . id = token . id || null ;
139+ session . user . username = token . username || null ;
140+ session . user . email = token . email || null ;
132141 }
133-
134142 return session ;
135143 } ,
136144 } ,
137145 pages : {
138146 signIn : "/signin" ,
147+ error : "/error" ,
148+ signOut : "/signin" ,
139149 } ,
140150} ;
0 commit comments