import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
import { useState, useEffect } from "react";
import {
createUserWithEmailAndPassword, // 🔥 1
signInWithEmailAndPassword, // 🔥 2
onAuthStateChanged, // 🔥 3
signOut, // 🔥 4
getAuth, // 🔥 5
updatePassword, // 🔥 6
} from "firebase/auth";
import { auth } from "./firebase";
function App() {
const [registerEmail, setRegisterEmail] = useState("");
const [registerPassword, setRegisterPassword] = useState("");
const [loginEmail, setLoginEmail] = useState("");
const [loginPassword, setLoginPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const [user, setUser] = useState({});
const auth2 = getAuth(); // 🔥 5
const currentUser = auth2.currentUser;
// console.log(currentUser);
useEffect(() => {
onAuthStateChanged(auth, (currentUser) => { // 🔥 3
setUser(currentUser);
});
}, [user]);
console.log(user);
const register = async () => { // 🔥 1
try {
const user = await createUserWithEmailAndPassword(auth, registerEmail, registerPassword);
console.log(user);
} catch (error) {
console.log(error.message);
}
};
const login = async () => { // 🔥 2
try {
const user = await signInWithEmailAndPassword(auth, loginEmail, loginPassword);
console.log(user);
} catch (error) {
console.log(error.message);
}
};
const logout = async () => { await signOut(auth); }; // 🔥 4
const handleNewPassword = async () => { // 🔥 6
try {
if (user) {
updatePassword(user, newPassword);
} else {
alert("로그인 하세요.");
}
} catch (error) {
console.log(error);
}
};
return (
<div className="App">
<div>
<h3> Register User </h3>
<input placeholder="Email..." onChange={(event) => {setRegisterEmail(event.target.value); }} />
<input placeholder="Password..." onChange={(event) => { setRegisterPassword(event.target.value); }} />
<button onClick={register}> Create User</button>
</div>
<div>
<h3> Login </h3>
<input placeholder="Email..." onChange={(event) => { setLoginEmail(event.target.value); }} />
<input placeholder="Password..." onChange={(event) => { setLoginPassword(event.target.value); }} />
<button onClick={login}> Login</button>
</div>
<h4> User Logged In: </h4>
{user?.email}
<button onClick={logout}> Sign Out </button>
<h4>Change Password</h4>
<input placeholder="NewPassword..." onChange={event => { setNewPassword(event.target.value); }} />
<button onClick={handleNewPassword}> New Password </button>
</div>
);
}
export default App;
(alias) function createUserWithEmailAndPassword(auth: Auth, email: string, password: string): Promise<UserCredential>
import createUserWithEmailAndPassword
(alias) function signInWithEmailAndPassword(auth: Auth, email: string, password: string): Promise<UserCredential>
import signInWithEmailAndPassword
(alias) function onAuthStateChanged(auth: Auth, nextOrObserver: NextOrObserver<User>, error?: ErrorFn, completed?: CompleteFn): Unsubscribe
import onAuthStateChanged
(alias) function signOut(auth: Auth): Promise<void>
import signOut
(alias) function getAuth(app?: FirebaseApp): Auth
import getAuth
(alias) function updatePassword(user: User, newPassword: string): Promise<void>
import updatePassword
더 볼것