-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
136 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,39 +1,59 @@ | ||
import React, { useState } from 'react' | ||
import { auth, googleProvider } from '../config/firebase' | ||
import { createUserWithEmailAndPassword, signInWithPopup, signOut } from 'firebase/auth' | ||
import { createUserWithEmailAndPassword, signInWithEmailAndPassword, signInWithPopup, signOut } from 'firebase/auth' | ||
import { useAuth } from '../Contexts/Auth' | ||
import { Link } from 'react-router-dom'; | ||
|
||
function Auth() { | ||
const { currentUser, userLoggedIn } = useAuth() | ||
const [email, setEmail] = useState(""); | ||
const [password, setPassword] = useState(""); | ||
const [isSigningIn, setIsSigningIn] = useState(false) | ||
|
||
console.log(auth?.currentUser?.email) | ||
const signIn = async () =>{ | ||
try{ | ||
await createUserWithEmailAndPassword(auth, email, password)} catch(err){ | ||
console.error(err) | ||
} | ||
} | ||
// const signUp = async () =>{ | ||
// try{ | ||
// await createUserWithEmailAndPassword(auth, email, password)} catch(err){ | ||
// console.error(err) | ||
// } | ||
// } | ||
// const signIn = async () =>{ | ||
// if (!isSigningIn) | ||
// try{ | ||
// setIsSigningIn(true) | ||
// await signInWithEmailAndPassword(auth, email, password)} catch(err){ | ||
// console.error(err) | ||
// } | ||
// } | ||
|
||
const signInWithGoogle = async () =>{ | ||
if (!isSigningIn) | ||
try{ | ||
await signInWithPopup(auth, googleProvider)} catch(err){ | ||
console.error(err) | ||
} | ||
} | ||
|
||
const logout = async () =>{ | ||
try{ | ||
await signOut(auth)} catch(err){ | ||
console.error(err) | ||
} | ||
} | ||
return ( | ||
|
||
return ( | ||
<div> | ||
{/* <input type="text" placeholder='Email' onChange={(e)=> setEmail(e.target.value)}/> | ||
<input type="text" placeholder='Password'onChange={(e)=> setPassword(e.target.value)} type="password"/> | ||
<button onClick={signUp}>Sign up</button> | ||
<input type="text" placeholder='Email' onChange={(e)=> setEmail(e.target.value)}/> | ||
<input type="text" placeholder='Password'onChange={(e)=> setPassword(e.target.value)} type="password"/> | ||
<button onClick={signIn}>Sign in</button> */} | ||
<button onClick={signInWithGoogle}>Sign in with Google</button> | ||
<button onClick={logout}>Log out</button> | ||
<button onClick={()=>{logout(). then(()=> <Link to="/"/>)}}>Log out</button> | ||
<h4>Logged in as: {auth?.currentUser?.displayName}</h4> | ||
</div> | ||
) | ||
) | ||
} | ||
|
||
export default Auth |
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,12 +1,45 @@ | ||
// import { useState } from "react"; | ||
// import a | ||
import { useContext, useEffect, useState} from "react"; | ||
import { auth } from "../config/firebase"; | ||
import { onAuthStateChanged } from "firebase/auth"; | ||
import { createContext } from "react"; | ||
import React from "react"; | ||
|
||
// const AuthContext = React.createContext(); | ||
|
||
// export function AuthProvider({ children }){ | ||
// const [currentUser, setCurrentUser] = useState(null); | ||
// const [userLoggedIn, setUserLoggedIn] = useState(false); | ||
// const [loading, setLoading] = useState(true); | ||
const AuthContext = React.createContext(); | ||
|
||
export function useAuth(){ | ||
return useContext(AuthContext) | ||
} | ||
|
||
// } | ||
export function AuthProvider({ children }){ | ||
const [currentUser, setCurrentUser] = useState(null); | ||
const [userLoggedIn, setUserLoggedIn] = useState(false); | ||
const [loading, setLoading] = useState(true); | ||
|
||
useEffect(()=>{ | ||
const unsubscribe = onAuthStateChanged(auth, initializeUser); | ||
return unsubscribe | ||
}, []) | ||
|
||
async function initializeUser(user){ | ||
if (user) { | ||
setCurrentUser({...user}); | ||
setUserLoggedIn(true) | ||
} else { | ||
setCurrentUser(null); | ||
setUserLoggedIn(false) | ||
} | ||
setLoading(false) | ||
} | ||
|
||
const value = { | ||
currentUser, | ||
userLoggedIn, | ||
loading | ||
} | ||
return( | ||
<AuthContext.Provider value={value}> | ||
{!loading && 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