-
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.
sign up, login, logout, login with google and login with github
- Loading branch information
1 parent
49995af
commit 2c3a598
Showing
19 changed files
with
513 additions
and
64 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { initializeApp } from 'firebase/app'; | ||
import { getAuth, GoogleAuthProvider, GithubAuthProvider } from 'firebase/auth'; | ||
|
||
const firebaseConfig = { | ||
apiKey: import.meta.env.VITE_FIREBASE_API_KEY, | ||
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN, | ||
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID, | ||
storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET, | ||
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID, | ||
appId: import.meta.env.VITE_FIREBASE_APP_ID, | ||
}; | ||
|
||
const app = initializeApp(firebaseConfig); | ||
const auth = getAuth(app); | ||
const googleAuthProvider = new GoogleAuthProvider(); | ||
const githubAuthProvider = new GithubAuthProvider(); | ||
const SERVER_URL = import.meta.env.VITE_SERVER_URL || 'http://localhost:8080'; | ||
|
||
export { SERVER_URL, auth, googleAuthProvider, githubAuthProvider }; |
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
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
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
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 |
---|---|---|
@@ -1,13 +1,18 @@ | ||
header { | ||
.header { | ||
position: sticky; | ||
width: 100%; | ||
height: 6vh; | ||
height: 8vh; | ||
top: 0; | ||
left: 0; | ||
color: black; | ||
background: white; | ||
padding: 1vh 0 1vh 15vh; | ||
box-shadow: black 0 0 1px; | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
|
||
div { | ||
display: flex; | ||
align-items: center; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import './Header.scss'; | ||
import { useAuth } from '@ui/contexts/auth/useAuth'; | ||
|
||
function Header() { | ||
return <header></header>; | ||
const { currentUser, logout } = useAuth(); | ||
return ( | ||
<header className="header"> | ||
<p></p> | ||
<div> | ||
<p>{currentUser?.email}</p> | ||
<button onClick={logout}>Logout</button> | ||
</div> | ||
</header> | ||
); | ||
} | ||
|
||
export default Header; |
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,94 @@ | ||
import { auth, googleAuthProvider, githubAuthProvider } from '@config'; | ||
import { createContext, ReactNode, useEffect, useState } from 'react'; | ||
import { | ||
signInWithPopup, | ||
createUserWithEmailAndPassword, | ||
signInWithEmailAndPassword, | ||
signOut, | ||
updateProfile, | ||
User, | ||
UserCredential, | ||
} from 'firebase/auth'; | ||
|
||
export type AuthContextType = { | ||
currentUser: User | null; | ||
login: (email: string, password: string) => Promise<UserCredential>; | ||
loginWithGoogle: () => Promise<UserCredential>; | ||
loginWithGithub: () => Promise<UserCredential>; | ||
signup: (email: string, password: string) => Promise<UserCredential>; | ||
logout: () => Promise<void>; | ||
updateUserProfile: (user: User, profile: UserProfile) => Promise<void>; | ||
}; | ||
|
||
export const AuthContext = createContext<AuthContextType>({ | ||
currentUser: null, | ||
login: async () => ({}) as UserCredential, | ||
loginWithGoogle: async () => ({}) as UserCredential, | ||
loginWithGithub: async () => ({}) as UserCredential, | ||
signup: async () => ({}) as UserCredential, | ||
logout: async () => {}, | ||
updateUserProfile: async () => {}, | ||
}); | ||
|
||
type AuthProviderProps = { | ||
children: ReactNode; | ||
}; | ||
|
||
type UserProfile = { | ||
displayName?: string; | ||
photoURL?: string; | ||
}; | ||
|
||
export function AuthProvider({ children }: AuthProviderProps) { | ||
const [currentUser, setCurrentUser] = useState<User | null>(null); | ||
const [loading, setLoading] = useState(true); | ||
|
||
function signup(email: string, password: string) { | ||
return createUserWithEmailAndPassword(auth, email, password); | ||
} | ||
|
||
function login(email: string, password: string) { | ||
return signInWithEmailAndPassword(auth, email, password); | ||
} | ||
|
||
function logout() { | ||
return signOut(auth); | ||
} | ||
|
||
function updateUserProfile(user: User, profile: UserProfile) { | ||
return updateProfile(user, profile); | ||
} | ||
|
||
function loginWithGoogle() { | ||
return signInWithPopup(auth, googleAuthProvider); | ||
} | ||
|
||
function loginWithGithub() { | ||
return signInWithPopup(auth, githubAuthProvider); | ||
} | ||
|
||
useEffect(() => { | ||
return auth.onAuthStateChanged(user => { | ||
setCurrentUser(user); | ||
setLoading(false); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<AuthContext.Provider | ||
value={{ | ||
currentUser, | ||
login, | ||
loginWithGoogle, | ||
loginWithGithub, | ||
signup, | ||
logout, | ||
updateUserProfile, | ||
}} | ||
> | ||
{!loading && children} | ||
</AuthContext.Provider> | ||
); | ||
} | ||
|
||
export default AuthProvider; |
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,6 @@ | ||
import { useContext } from 'react'; | ||
import { AuthContext, AuthContextType } from '@ui/contexts/auth/AuthContext'; | ||
|
||
export function useAuth(): AuthContextType { | ||
return useContext(AuthContext); | ||
} |
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,38 @@ | ||
.auth { | ||
.auth-box { | ||
margin-top: 8vh; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
|
||
.auth-form { | ||
margin-top: 1vh; | ||
} | ||
|
||
.auth-submit-btn { | ||
margin-top: 3vh; | ||
margin-bottom: 2vh; | ||
text-transform: capitalize; | ||
} | ||
|
||
.auth-links-container { | ||
display: flex; | ||
gap: 1vh; | ||
} | ||
|
||
.remember-me * { | ||
user-select: none; | ||
} | ||
|
||
a { | ||
color: #1976d2; | ||
text-decoration: underline; | ||
font-size: smaller; | ||
font-weight: 500; | ||
} | ||
|
||
a:hover { | ||
color: #1976d2; | ||
} | ||
} |
Oops, something went wrong.