์ ์ ํ๋กํ ์ ๋ฐ์ดํธํ๊ณ ๋ค๋ฅธ ์ปดํฌ๋ํธ ๊ฐฑ์ ํ๊ณ ์ถ์ ์๋ ์์ผ๋ ๊ทธ๋ ํธํ๋ผ๊ณ ๋ค๋น๊ฒ์ด์ ์๋ userObj๋ฅผ ๋ณด๋ด์ ์ ์ ์ ๋ณด๋ฅผ ํ๋ฉด์ ํ์ํด ๋ณด์!
const AppRouter = ({ isLoggedIn, userObj }) => {
return (
<Router>
{isLoggedIn && <Navigation userObj={userObj} />}
<Routes>
import { Link } from "react-router-dom";
const Navigation = ({ userObj }) => (
<nav>
<ul>
<li>
<Link to="/">ํ</Link>
</li>
<li>
<Link to="/profile">{userObj.displayName}์ ํ๋กํ</Link>
</li>
</ul>
</nav>
);
export default Navigation;
์ด๋ฐ์์ผ๋ก ์ ์ ์ ์ต์ ๋ฐ์ดํฐ๋ฅผ ํ์ฉํ ์ ์๋ค.
<form onSubmit={onSubmit}>
<input
onChange={onChange}
type="text"
placeholder="์ด๋ฆ"
value={newDisplayName}
/>
<input type="submit" value="์ ์ฅ" />
</form>
์ฌ์ฉ์์ ํ์ ์ด๋ฆ
displayName๋ฐ ํ๋กํ ์ฌ์ง URLphotoURL๋ฑ์ ๊ธฐ๋ณธ ํ๋กํ ์ ๋ณด๋ฅผ ์ ๋ฐ์ดํธํ ๋๋updateProfile()๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
- updateProfile
updateProfile ( profile : { displayName ?: string | null ; photoURL ?: string | null } ) : Promise < void >- ์์
import { getAuth, updateProfile } from "firebase/auth"; const auth = getAuth(); updateProfile(auth.currentUser, { displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg" }).then(() => { // Profile updated! // ... }).catch((error) => { // An error occurred // ... });
๋ณด๋ค์ํผ updateProfile()์ ์ฌ์ฉ์์ displayName๊ณผ photoURL์ ์ ๋ฐ์ดํธํด์ฃผ๋ ๋ฉ์๋์ด๋ค. ๋ณด๋ค ๋ง์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ ค๋ฉด ๋ค๋ฅธ ๋ฉ์๋๋ฅผ ์ฐธ๊ณ ํ๋ฉด ๋๋ค.
const [newDisplayName, setNewDisplayName] = useState(userObj.displayName);
const onChange = (event) => {
const {
target: { value },
} = event;
setNewDisplayName(value);
};
const onSubmit = async (event) => {
event.preventDefault();
//์ด๋ฆ ์์ ํ๋ฉด updateProfile() ๋ฉ์๋ ์ฌ์ฉํด ํ๋กํ ์
๋ฐ์ดํธํ๊ธฐ
if (userObj.displayName !== newDisplayName) {
//console.log(userObj.updateProfile);
await updateProfile(userObj, { displayName: newDisplayName });
}
};
updateProfile()๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ ๋ง๊ณ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์์ displayName์ ๋ณ๊ฒฝํ ์๋ ์๋ค. firestore์์ users ์ฝ๋ ์ ๋ง๊ณ ๋ํ๋จผํธ ์์ฑํด์ ์ ์ ์ ๊ดํ ๋ฐ์ดํฐ ๋ชจ๋ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ์ด๋ค.
์ด์จ๋ firebase.User์ ๋ฉ์๋๋ฅผ ํ์ฉํ์ฌ ํ๋กํ์ ์ฌ์ฉ์ ํ์๋ช
์ ๋ฐ๊พธ๋ ์์
์ ์ํํด ๋ณด์๋ค.
updateProfile()๋ฉ์๋๋ ์ฌ์ฉ์ ํ์๋ช
๊ณผ, ํ๋กํ ์ฌ์ง ๋๊ฐ์ง๋ฅผ ์
๋ฐ์ดํธ ํ ์ ์๋ ๋ฉ์๋์ด๊ธฐ ๋๋ฌธ์, ํ๋กํ ์ฌ์ง ์
๋ก๋ ๊ธฐ๋ฅ๊ณผ ์์ ํ๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํด ๋ณด์!
1. ํ๋กํ ์ฌ์ง ์
๋ก๋ ํ๋ ํผ ๋ง๋ค๊ธฐ
2. profilePhoto ๋ฒ์ผ ๋ง๋ค์ด์
3. ๋ค์ด๋ก๋ url ๊ฐ์ ธ์์ ์์ photoURL์ ๋ฃ์ด์ฃผ๋ฉด ๋จ