@@ -15,7 +15,7 @@ import {
1515 User ,
1616 UserCredential ,
1717} from "firebase/auth" ;
18- import { Navigate , NavigateFunction } from "react-router-dom" ;
18+ import { useNavigate , Navigate , NavigateFunction } from "react-router-dom" ;
1919
2020import { auth } from "../utils/auth/firebase" ;
2121import { useBackendContext } from "./hooks/useBackendContext" ;
@@ -34,7 +34,7 @@ interface AuthContextProps {
3434 role,
3535 } : SignupInfo ) => Promise < UserCredential > ;
3636 login : ( { email, password } : EmailPassword ) => Promise < EmailAuthCredential > ;
37- createCode : ( ) => Promise < void > ;
37+ createCode : ( email : string , authCredential : EmailAuthCredential ) => Promise < void > ;
3838 logout : ( ) => Promise < void > ;
3939 authenticate : ( { code } : Authenticate ) => Promise < UserCredential | void > ;
4040 resetPassword : ( { email } : Pick < EmailPassword , "email" > ) => Promise < void > ;
@@ -75,6 +75,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
7575 const [ authCredential , setAuthCredential ] =
7676 useState < EmailAuthCredential | null > ( null ) ;
7777 const [ email , setEmail ] = useState < string | null > ( null ) ;
78+ const navigate = useNavigate ( ) ;
7879
7980 const signup = async ( {
8081 email,
@@ -103,59 +104,77 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
103104 throw new Error ( "Not authorized to create this type of user" ) ;
104105 }
105106
106- await backend . delete ( `users/email/${ email } ` ) ;
107+ // await backend.delete(`users/email/${email}`);
107108
108109 const userCredential = await createUserWithEmailAndPassword (
109110 auth ,
110111 email ,
111112 password
112113 ) ;
113114
114- await backend . post ( "/users/create" , {
115- email : email ,
116- firebaseUid : userCredential . user . uid ,
117- firstName : firstName ,
118- lastName : lastName ,
119- phoneNumber : phoneNumber ,
120- role,
121- } ) ;
115+ try {
116+ await backend . put ( "/users/updateUser" , {
117+ email : email ,
118+ firstName : firstName ,
119+ lastName : lastName ,
120+ phoneNumber : phoneNumber ,
121+ firebaseUid : userCredential . user . uid ,
122+ } ) ;
123+ } catch ( error ) {
124+ console . error ( "Error updating user:" , error ) ;
125+ }
126+
127+ // await backend.post("/users/create", {
128+ // email: email,
129+ // firebaseUid: userCredential.user.uid,
130+ // firstName: firstName,
131+ // lastName: lastName,
132+ // phoneNumber: phoneNumber,
133+ // role,
134+ // });
122135
123136 return userCredential ;
124137 } ;
125138
126139 const login = async ( { email, password } : EmailPassword ) => {
127140 if ( currentUser ) {
141+ // TODO CHANGE TO REDIRECT IF LOGGED IN
128142 signOut ( auth ) ;
129143 }
144+
145+ const user = await backend . get ( `/users/email/${ email } ` )
146+
147+ if ( user . data . length === 0 ) {
148+ throw new Error ( "Incorrect username or password" ) ;
149+ }
150+
130151 const authCredential = EmailAuthProvider . credential ( email , password ) ;
131152 setAuthCredential ( authCredential ) ;
132153 setEmail ( email ) ;
133154
134155 return authCredential ;
135156 } ;
136157
137- const createCode = async ( ) => {
138- if ( authCredential && email ) {
139- try {
140- // Delete all the stale codes associated with this email
141- // await backend.delete(`authentification/email?email=${email}`);
142-
143- // Create new code for them
144- const now = new Date ( ) ;
145- const validUntil = new Date ( now . getTime ( ) + 24 * 60 * 60 * 1000 ) ;
146-
147- const authData = await backend . post ( "/authentification" , {
148- email : email ,
149- validUntil : validUntil ,
150- } ) ;
151- //const code = authData?.data[0]?.code;
152-
153- // Send the code to the user via email
154-
155- return ;
156- } catch ( error ) {
157- console . error ( "Error signing in with credential:" , error ) ;
158- }
158+ const createCode = async ( email : string , authCredential : EmailAuthCredential ) => {
159+ try {
160+
161+ // Delete all the stale codes associated with this email
162+ // await backend.delete(`authentification/email?email=${email}`);
163+
164+ // Create new code for them
165+ const now = new Date ( ) ;
166+ const validUntil = new Date ( now . getTime ( ) + 24 * 60 * 60 * 1000 ) ;
167+
168+ const authData = await backend . post ( `/authentification` , {
169+ email : email ,
170+ validUntil : validUntil ,
171+ } ) ;
172+
173+
174+ //const code = authData?.data[0]?.code;
175+ // Send the code to the user via email
176+ } catch ( error ) {
177+ console . error ( "Error creating code:" , error ) ;
159178 }
160179 } ;
161180
@@ -164,12 +183,11 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
164183 const response = await backend . post (
165184 `/authentification/verify?email=${ email } &code=${ code } `
166185 ) ;
167- if ( response . data . length == 0 ) {
186+ if ( response . data . length === 0 ) {
168187 throw new Error ( "Invalid code. Try again." ) ;
169188 }
170189
171190 const userCredential = await signInWithCredential ( auth , authCredential ) ;
172-
173191 // we have to update the currnet user role BEFORE we sign in or else the app won't know what role we are currently
174192 const userData = await backend . get ( `/users/${ userCredential . user . uid } ` ) ;
175193 setCurrentUserRole ( userData . data [ 0 ] ?. role ) ;
0 commit comments