-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
102d1c9
commit a47f616
Showing
11 changed files
with
6,178 additions
and
4,029 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
# misc | ||
.DS_Store | ||
.env | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { UserData } from 'types/userData'; | ||
import { AuthContext } from './auth.context'; | ||
import { firebaseAuth, registerUser, loginUser } from 'services/firebase'; | ||
|
||
export const AuthProvider: React.FC = ({ children }) => { | ||
const [currentUser, setCurrentUser] = useState<UserData | null>(null); | ||
|
||
useEffect(() => { | ||
const unsubscribe = firebaseAuth.onAuthStateChanged((user: any) => { | ||
setCurrentUser(user); | ||
console.log("onAuthStateChanged", currentUser); | ||
}); | ||
|
||
return unsubscribe; | ||
}, []); | ||
|
||
const register = async (email: string, password: string, fullName: string, phoneNumber: string, address: string, gender: 'male' | 'female') => { | ||
try { | ||
await registerUser(email, password, fullName, phoneNumber, address, gender); | ||
} catch (error) { | ||
console.error(error); | ||
throw new Error("Oops! Something went wrong."); | ||
} | ||
}; | ||
|
||
const login = async (email: string, password: string) => { | ||
try { | ||
const result = await loginUser(email, password); | ||
|
||
if (!result.status && result.message === 'email_not_verified') { | ||
throw new Error("email_not_verified"); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
|
||
if (error instanceof Error) { | ||
if (error.message === 'email_not_verified') { | ||
throw new Error("email_not_verified"); | ||
} | ||
return; | ||
} | ||
|
||
throw new Error("Oops! Something went wrong."); | ||
} | ||
}; | ||
|
||
return ( | ||
<AuthContext.Provider value={{ currentUser, register, login }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createContext } from 'react'; | ||
import { UserData } from 'types/userData'; | ||
|
||
interface Context { | ||
currentUser: UserData | null; | ||
register: (email: string, password: string, fullName: string, phoneNumber: string, address: string, gender: 'male' | 'female') => void; | ||
login: (email: string, password: string) => void; | ||
} | ||
|
||
export const AuthContext = createContext<Context>({ | ||
currentUser: null, | ||
register: () => {}, | ||
login: () => {}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './AuthProvider'; | ||
export * from './auth.context'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.