import { useState, useEffect } from 'react';
// firebase auth에서 닉네임, 유저 아이디등을 받아올 수 있도록 import함
import { auth } from '../../services/firebase';
import {
MyProfileWrapper,
MyProfileImage,
MyProfileNickname,
NicknameModifyBox,
ModifyButton,
DeleteAccountBtn,
PasswordChange,
NicknameInput,
} from './style';
const MyProfile = () => {
const [nickname, setNickname] = useState<string | null | undefined>('');
useEffect(() => {
const userName = auth.currentUser?.displayName;
setNickname(userName);
}, [nickname]);
return (
<MyProfileWrapper>
<MyProfileImage>이미지</MyProfileImage>
<MyProfileNickname>{auth.currentUser?.displayName}</MyProfileNickname>
<NicknameModifyBox>
<NicknameInput placeholder="닉네임을 입력해주세요"></NicknameInput>
<ModifyButton>수정</ModifyButton>
</NicknameModifyBox>
<DeleteAccountBtn>회원탈퇴</DeleteAccountBtn>
<PasswordChange>비밀번호 변경</PasswordChange>
</MyProfileWrapper>
);
};
export default MyProfile;
recoil을 사용하면 로그인된 상태를 자동으로 확인이 되기 때문에 로그인 된 유저아이디의 닉네임도 받아올 수 있는 것
주어진 Recoil 상태의 값을 리턴합니다.
이 hook은 암묵적으로 주어진 상태에 컴포넌트를 구독합니다.
import { useRecoilValue } from 'recoil';
import { userInfo } from '../../atoms';
import {
MyProfileWrapper,
MyProfileImage,
MyProfileNickname,
NicknameModifyBox,
ModifyButton,
DeleteAccountBtn,
PasswordChange,
NicknameInput,
} from './style';
const MyProfile = () => {
// useRecoilValue 기능으로 userInfo를 받아옴
const user = useRecoilValue(userInfo);
return (
<MyProfileWrapper>
<MyProfileImage>이미지</MyProfileImage>
<MyProfileNickname>{user.userInfomation.displayName}</MyProfileNickname>
<NicknameModifyBox>
<NicknameInput placeholder="닉네임을 입력해주세요"></NicknameInput>
<ModifyButton>수정</ModifyButton>
</NicknameModifyBox>
<DeleteAccountBtn>회원탈퇴</DeleteAccountBtn>
<PasswordChange>비밀번호 변경</PasswordChange>
</MyProfileWrapper>
);
};
export default MyProfile;
// atom.ts
import { atom } from 'recoil';
interface UserInfomation {
displayName: string;
email: string;
photoURL: string;
uid: string;
}
const [nickname, setNickname] = useState<any>('');
// useRecoilValue 기능으로 userInfo를 받아옴
// const user = useRecoilValue(userInfo);
useEffect(() => {
onAuthStateChanged(auth, (user) => {
if (user) {
setNickname(auth.currentUser?.displayName);
console.log('로그인 되어있음');
} else if (!user) {
console.log('로그인 안됨');
}
});
if (!nickname) return;
}, []);
-recoil은 한줄로 가능하다.
const user = useRecoilValue(userInfo);
const MyProfile = () => {
const [nickname, setNickname] = useState<string>('');
const user = useRecoilValue(userInfo);
// 인풋하는 족족 데이터 받고 수정
const NicknameChangeInput = (event: any) => {
console.log(event.target.value);
setNickname(event.target.value);
};
return (
<NicknameInput
onChange={NicknameChangeInput}
placeholder="닉네임을 입력해주세요"
/>
// 로그인 상태인지 확인 함수
useEffect(() => {
auth.onAuthStateChanged((ddong) => {
// auth = getAuth() : currentUser이 담겨있는 배열(AuthImpl)
// user : displayName이 담겨있는 객체(UserImpl)
console.log('ddong', ddong);
if (ddong) {
console.log('auth.currentUser', auth.currentUser);
setCurrentUser(auth.currentUser);
// auth.currentUser : displayName이 담겨있는 객체(UserImpl)
// setCurrentUser : 함수?같은 건데 잘 모르겠음
setNickname(auth.currentUser?.displayName);
console.log(
'auth.currentUser?.displayName',
auth.currentUser?.displayName,
);
//auth.currentUser?.displayName : 원래 닉네임
console.log('로그인 되어있음');
} else if (!ddong) {
console.log('로그인 안됨');
}
});
if (!currentUser) return;
}, []);
//MyProfile.tsx
import { onAuthStateChanged } from 'firebase/auth';
import { useEffect, useState, useCallback } from 'react';
import { useRecoilValue } from 'recoil';
import { userInfo } from '../../atoms';
import { auth } from '../../services/firebase';
import {
MyProfileWrapper,
MyProfileImage,
MyProfileNickname,
NicknameModifyBox,
ModifyButton,
DeleteAccountBtn,
PasswordChange,
NicknameInput,
Colortext,
} from './style';
import { updateProfile } from 'firebase/auth';
const MyProfile = () => {
// nickname : 현재 nickname이 들어옴
const [nickname, setNickname] = useState<any>('');
console.log('nickname', nickname);
// currentUser : displayName이 담겨있는 객체
const [currentUser, setCurrentUser] = useState<any>('');
// useRecoilValue 기능으로 userInfo를 받아옴
// const user = useRecoilValue(userInfo);
// 로그인 상태인지 확인 함수
useEffect(() => {
auth.onAuthStateChanged((user) => {
// auth = getAuth() : currentUser이 담겨있는 배열(AuthImpl)
// user : displayName이 담겨있는 객체(UserImpl)
if (user) {
console.log('auth.currentUser', auth.currentUser);
setCurrentUser(auth.currentUser);
// auth.currentUser : displayName이 담겨있는 객체(UserImpl)
// setCurrentUser : 함수?같은 건데 잘 모르겠음
setNickname(auth.currentUser?.displayName);
console.log(
'auth.currentUser?.displayName',
auth.currentUser?.displayName,
);
//auth.currentUser?.displayName : 원래 닉네임
console.log('로그인 되어있음');
} else if (!user) {
console.log('로그인 안됨');
}
});
if (!currentUser) return;
}, []);
// 닉네임 바꿔주는 함수
// auth와 getAuth()는 같음
const onClick = async () => {
await updateProfile(currentUser, {
displayName: nickname,
photoURL: 'https://example.com/jane-q-user/profile.jpg',
})
.then(() => {
setNickname('');
console.log('nickname=>', nickname);
alert('Profile updated!');
})
.catch((error) => {
console.log('An error occurred');
});
};
// 인풋에 입력한 상태 그대로 ui표시
// onchange는 매번 set을 해주는 것
const NicknameChangeInput = (event: any) => {
setNickname(event.target.value);
console.log('event.target.value', event.target.value);
};
return (
<MyProfileWrapper>
<MyProfileImage>이미지</MyProfileImage>
<MyProfileNickname>{currentUser?.displayName}</MyProfileNickname>
<NicknameModifyBox>
<NicknameInput
placeholder="닉네임을 입력해주세요"
onChange={NicknameChangeInput}
value={nickname}
/>
<ModifyButton onClick={onClick}>수정</ModifyButton>
</NicknameModifyBox>
<DeleteAccountBtn>회원탈퇴</DeleteAccountBtn>
<PasswordChange>비밀번호 변경</PasswordChange>
</MyProfileWrapper>
);
};
export default MyProfile;
https://wakestand.tistory.com/851
implementation - 실제구현체