33 lines
852 B
JavaScript
33 lines
852 B
JavaScript
import { initializeApp } from "firebase/app";
|
|
import { getAuth, onAuthStateChanged } from "firebase/auth";
|
|
import env from "../env";
|
|
import { store } from "../redux/store";
|
|
import { signInSuccess, unauthorizedUser } from "../redux/user/user.actions";
|
|
|
|
initializeApp(env.firebase);
|
|
|
|
export const auth = getAuth();
|
|
|
|
export const unsubscribe = onAuthStateChanged(auth, (user) => {
|
|
store.dispatch(
|
|
user
|
|
? signInSuccess({
|
|
uid: user.uid,
|
|
email: user.email,
|
|
displayName: user.displayName,
|
|
photoURL: user.photoURL,
|
|
authorized: true,
|
|
})
|
|
: unauthorizedUser()
|
|
);
|
|
});
|
|
|
|
export const getCurrentUser = () => {
|
|
return new Promise((resolve, reject) => {
|
|
const unsubscribe = auth.onAuthStateChanged((userAuth) => {
|
|
unsubscribe();
|
|
resolve(userAuth);
|
|
}, reject);
|
|
});
|
|
};
|