📝 To Do List
✔
// 닉네임 바꿔주는 함수
const submitHandle = async (e: any) => {
console.log('currentUser', currentUser);
e.preventDefault();
console.log('imgFile', imgFile);
// imgFile의 길이가 0이 아니라면
if (imgFile.length !== 0) {
// Firebase storage에 이미지 업로드
const imgRef = ref(storage, `profileUploadImg/${fileName + uuidv4()}`);
const response = await uploadString(imgRef, imgFile, 'data_url');
const downloadImageUrl = await getDownloadURL(response.ref);
setImgUploadUrl(downloadImageUrl);
console.log('downloadImageUrl', downloadImageUrl);
// 프로필 업데이트를 해주는데
// displayName은 nickname으로
// photoURL은 downloadImageUrl로 프로필 업데이트를 해달라.
await updateProfile(currentUser, {
displayName: nickname,
photoURL: downloadImageUrl,
})
.then(() => {
alert('Profile updated!');
setNickname('');
console.log('imgUploadUrl', imgUploadUrl);
// setImgUploadUrl('');
})
.catch((error) => {});
// imgFile의 길이가 0이라면 displayName을 nickname으로만 바꿔줘
} else {
await updateProfile(currentUser, {
displayName: nickname,
})
.then(() => {
alert('Profile updated!');
setNickname('');
console.log('imgUploadUrl', imgUploadUrl);
// setImgUploadUrl('');
})
.catch((error) => {});
}
};
import { onAuthStateChanged } from 'firebase/auth';
import { useEffect, useState } from 'react';
import { auth } from '../../services/firebase';
import {
MyProfileWrapper,
MyProfileNickname,
NicknameModifyBox,
ModifyButton,
NicknameInput,
ProfileImgLabel,
ProfileImgInput,
NewImageForm,
} from './style';
import { updateProfile } from 'firebase/auth';
import { storage } from '../../../src/services/firebase';
import { getDownloadURL, ref, uploadString } from 'firebase/storage';
import { v4 as uuidv4 } from 'uuid';
const MyProfile = () => {
const [nickname, setNickname] = useState<any>('');
const [currentUser, setCurrentUser] = useState<any>('');
const [imgFile, setImgFile] = useState(''); // 이미지 파일
const [fileName, setFileName] = useState(''); // 이미지 파일 이름.확장자
const [imgUploadUrl, setImgUploadUrl] = useState<any>(
auth.currentUser?.photoURL,
);
console.log('================================');
console.log('imgUploadUrl ', imgUploadUrl);
// 이미지 파일 input onChange 함수
const newProfileImgOnChangeHandler = (
// event : currentTarget과 target이 들어있는 객체
event: React.ChangeEvent<HTMLInputElement>,
) => {
const target = event.currentTarget;
const theFile = (target.files as FileList)[0]; // 이벤트로부터 파일을 얻어와서 첫번째 파일만 받음
console.log('theFile', theFile);
setFileName(theFile.name);
const reader = new FileReader();
reader.readAsDataURL(theFile); // file객체를 data url로 바꿔줌
// 파일 읽기를 끝내면 state로 만들어둔 setImgFile에 값을 넣어줌
reader.onloadend = (finishedEvent: any) => {
console.log('finishedEvent', finishedEvent);
setImgFile(finishedEvent.currentTarget.result);
};
};
useEffect(() => {
auth.onAuthStateChanged((user) => {
if (user) {
console.log('user', user);
// updateProfile(user )
setCurrentUser(auth.currentUser);
setImgUploadUrl(user.photoURL);
// setNickname(auth.currentUser?.displayName);
// setImgFile(user?.photoURL);
} else {
return console.log('로그인 안됨');
}
});
}, [currentUser]);
useEffect(() => {}, [imgUploadUrl]);
// 닉네임 바꿔주는 함수
const submitHandle = async (e: any) => {
console.log('currentUser', currentUser);
e.preventDefault();
console.log('imgFile', imgFile);
if (imgFile.length !== 0) {
// Firebase storage에 이미지 업로드
const imgRef = ref(storage, `profileUploadImg/${fileName + uuidv4()}`);
const response = await uploadString(imgRef, imgFile, 'data_url');
const downloadImageUrl = await getDownloadURL(response.ref);
setImgUploadUrl(downloadImageUrl);
console.log('downloadImageUrl', downloadImageUrl);
await updateProfile(currentUser, {
displayName: nickname,
photoURL: downloadImageUrl,
})
.then(() => {
alert('Profile updated!');
setNickname('');
console.log('imgUploadUrl', imgUploadUrl);
// setImgUploadUrl('');
})
.catch((error) => {});
} else {
await updateProfile(currentUser, {
displayName: nickname,
})
.then(() => {
alert('Profile updated!');
setNickname('');
console.log('imgUploadUrl', imgUploadUrl);
// setImgUploadUrl('');
})
.catch((error) => {});
}
};
const NicknameChangeInput = (event: any) => {
setNickname(event.target.value);
};
return (
<NewImageForm onSubmit={submitHandle}>
<MyProfileWrapper>
<div>
{/* // label태그와 input태그를 연결함으로써 label만 눌러도 프로필 업데이트가 되게 연결시킴 */}
<ProfileImgLabel htmlFor="profileUploadImg">
<img src={imgUploadUrl} style={{ width: 150, height: 150 }} />
</ProfileImgLabel>
{/* // 인풋에서 열기버튼을 누르면 파이어베이스에 올라가게 */}
<ProfileImgInput
type="file"
accept="image/*"
id="profileUploadImg"
onChange={newProfileImgOnChangeHandler}
style={{ display: 'none' }}
// style={{ display: 'none' }}
/>
</div>
{/* <ImgModifyButton>이미지 수정완료</ImgModifyButton> */}
<MyProfileNickname>{currentUser?.displayName}</MyProfileNickname>
<NicknameModifyBox>
<NicknameInput
placeholder="닉네임을 입력해주세요"
onChange={NicknameChangeInput}
value={nickname}
/>
<ModifyButton type="submit">수정</ModifyButton>
</NicknameModifyBox>
</MyProfileWrapper>
</NewImageForm>
);
};
export default MyProfile;
// firebase.ts
// Initialize Firebase
import { getAuth } from 'firebase/auth';
export const auth = getAuth(app);
// MyProfile.tsx
import { auth } from '../../services/firebase';
updateProfile
메소드를 사용하여 사용자의 기본 프로필 정보(사용자의 표시 이름 및 프로필 사진 URL)를 업데이트할 수 있다.import { updateProfile } from 'firebase/auth';
// updateProfile 메소드를 사용해서 currentUser의 displayName은 'Jane'으로 업데이트 해주고, photoURL은 'https://example.com/jane-q-user/profile.jpg'로 업데이트해줘
updateProfile(currentUser, {
displayName: 'Jane',
photoURL: 'https://example.com/jane-q-user/profile.jpg',
})
.then(() => {
alert('Profile updated!');
})
.catch((error: any) => {
});
❓하지만 이 둘의 차이가 정확히 뭔지 모르겠다.
⭐⭐⭐ 나중에 이거 읽어보기
https://github.com/woob0129/VirtualTraveler/blob/master/wiki/about_firebase_auth.md
useEffect(() => {
auth.onAuthStateChanged((user) => {
if (user) {
setCurrentUser(auth.currentUser);
} else {
return console.log('로그인 안됨');
}
});
}, []);
// 닉네임, 이미지 바꿔주는 함수
// 바꾸려는 닉네임과 이미지를 파이어베이스에 업데이트해주는 코드
const submitNicknameImgChange = async (e: any) => {
e.preventDefault();
await updateProfile(currentUser, {
displayName: nickname,
photoURL: 'https://example.com/jane-q-user/profile.jpg',
})
.then(() => {
alert('Profile updated!');
setNickname('');
})
.catch((error: any) => {
error;
});
};
const [imgFile, setImgFile] = useState(''); // 이미지 파일
const [imgFileName, setImgFileName] = useState(''); // 이미지 파일 이름.jpg
const [imgUploadUrl, setImgUploadUrl] = useState<any>(
auth.currentUser?.photoURL,
); // 업로드한 이미지 url