@@ -16,26 +16,30 @@ import {
1616import { collection , doc , setDoc } from 'firebase/firestore' ;
1717import { database } from '@notes/database' ;
1818import { CollectionType } from '@notes/types' ;
19+ import { FirebaseError } from 'firebase/app' ;
20+ import { useRouter } from '@tanstack/react-router' ;
1921
2022export const AuthContext = createContext < TContextAuth | undefined > ( undefined ) ;
2123
2224export function AuthProvider ( { children } ) {
2325 const [ user , setUser ] = useState < User | null > ( getAuth ( ) . currentUser ) ;
2426 const [ rememberMe , setRememberMe ] = useState ( false ) ;
25- const [ loading , setLoading ] = useState ( true ) ;
27+ // const router = useRouter( );
2628
27- function setLoadingState ( loading : boolean ) {
28- setLoading ( loading ) ;
29- }
3029 async function signIn ( email : string , password : string ) {
3130 try {
3231 if ( ! rememberMe ) {
3332 await setPersistence ( auth , inMemoryPersistence ) ;
3433 }
3534
36- return signInWithEmailAndPassword ( auth , email , password ) ;
35+ const userCredential = await signInWithEmailAndPassword ( auth , email , password ) ;
36+
37+ setUser ( userCredential . user ) ;
38+ return userCredential ;
3739 } catch ( error ) {
38- if ( error instanceof Error ) {
40+ if ( error instanceof FirebaseError ) {
41+ throw new Error ( error . code ) ;
42+ } else if ( error instanceof Error ) {
3943 throw new Error ( error . message ) ;
4044 } else {
4145 throw new Error ( 'An unknown error occurred' ) ;
@@ -44,14 +48,22 @@ export function AuthProvider({ children }) {
4448 }
4549
4650 async function signUp ( email : string , password : string , displayName : string ) {
47- const userCredential = await createUserWithEmailAndPassword ( auth , email , password ) ;
48- // dodac jak mail juz jest uzywany
49- await setDoc ( doc ( collection ( database , CollectionType . USERS ) , userCredential . user . uid ) , { } ) ;
50- await sendEmailVerification ( userCredential . user ) ;
51- await updateProfile ( userCredential . user , {
52- displayName
53- } ) ;
54- return userCredential ;
51+ try {
52+ const userCredential = await createUserWithEmailAndPassword ( auth , email , password ) ;
53+
54+ await setDoc ( doc ( collection ( database , CollectionType . USERS ) , userCredential . user . uid ) , { } ) ;
55+ await sendEmailVerification ( userCredential . user ) ;
56+ await updateProfile ( userCredential . user , {
57+ displayName
58+ } ) ;
59+ return userCredential ;
60+ } catch ( err ) {
61+ if ( err instanceof FirebaseError ) {
62+ throw new Error ( err . code ) ;
63+ } else {
64+ throw new Error ( String ( err ) ) ;
65+ }
66+ }
5567 }
5668
5769 // sendPasswordResetEmail(auth, email)
@@ -68,22 +80,25 @@ export function AuthProvider({ children }) {
6880 return signOut ( auth ) ;
6981 }
7082 // add modal to display errors => maybe a global modal to be reused for every errors
71-
7283 useEffect ( ( ) => {
73- setLoading ( true ) ;
7484 const unsubscribe = auth . onAuthStateChanged ( user => {
7585 if ( user ) {
7686 setUser ( user ) ;
77- setLoading ( false ) ;
87+ // router.invalidate(); // this is outside the router,
7888 } else {
7989 setUser ( null ) ;
80- setLoading ( false ) ;
8190 }
8291 } ) ;
8392 return unsubscribe ;
8493 } , [ ] ) ;
8594
86- const value : TContextAuth = { user, signIn, signUp, signUserOut, loading, setLoadingState, setRememberMe } ;
95+ const value : TContextAuth = {
96+ user,
97+ signIn,
98+ signUp,
99+ signUserOut,
100+ setRememberMe
101+ } ;
87102
88103 return < AuthContext . Provider value = { value } > { children } </ AuthContext . Provider > ;
89104}
0 commit comments