2020/02/07
// 최상위인 _app.js에서 실행
componentDidMount() {
unKnownUser()
}
// unKnownUser함수
export const unKonwnUser = () => {
if (Auth.currentUser === null) {
Auth.signInAnonymously()
.then(res => {
if (res) {
Auth.onAuthStateChanged(user => {
console.log("익명유저", user);
sessionStorage.setItem("anonymousUid", user.uid);
sessionStorage.setItem("isAnonymous", user.isAnonymous);
});
}
})
.catch(error => {
console.log(error);
});
}
};
signInAnonymously
method를 이용하여 가입시킨다.firebase.auth()
이다.onAuthStateChanged
를 통해서 user의 정보를 가져온다.linkWithCredential
method를 이용한다.export const linkEmail = (email, password) => {
const credential = firebase.auth.EmailAuthProvider.credential(
email,
password
);
Auth.currentUser
.linkWithCredential(credential)
.then(res => {
if (res) {
console.log('이것은 연동1', res);
Auth.onAuthStateChanged(user => {
console.log('유저', user);
sessionStorage.setItem('email', user.email);
sessionStorage.setItem('uid', user.uid);
sessionStorage.setItem('isAnonymous', user.isAnonymous);
AuthStore.emailLoginUser(user.email, user.uid, user.isAnonymous);
});
AuthStore.emailLoginUser();
moveHome();
}
})
.catch(e => {
console.log(e);
});
};
linkWithPopup
method를 사용한다.export const linkGoogleSignUp = () => {
const provider = new firebase.auth.GoogleAuthProvider();
Auth.currentUser
.linkWithPopup(provider)
.then(res => {
console.log('성공', res);
sessionStorage.setItem('accessToken', res.credential.accessToken);
sessionStorage.setItem('idToken', res.credential.idToken);
sessionStorage.setItem('uid', res.user.uid);
sessionStorage.setItem('email', res.user.email);
AuthStore.googleUser(res.user.email, res.user.uid, res.user.displayName);
AuthStore.isLogined();
moveHome();
})
.catch(e => {
console.log('실패', e);
});
};
// _app.js에서 실행한다.
componentDidMount() {
CountryCodeStore.getGeoInfo();
unKonwnUser();
const token = sessionStorage.getItem("idToken") || "";
if (token) {
AuthStore.isLogined();
} else {
AuthStore.isLogouted();
}
}