Skip to content

Commit 54130a0

Browse files
sesion refactored with saga
1 parent 2c11817 commit 54130a0

File tree

8 files changed

+115
-38
lines changed

8 files changed

+115
-38
lines changed

src/App.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { connect } from "react-redux";
77
import ShopPage from "./pages/shop/shop.component";
88
import Header from "./components/header/header.component";
99
import SignInAndSignUpPage from "./pages/sign-in-and-sign-up/sign-in-and-sign-up.component";
10+
import {checkUserSession } from "./redux/user/user.actions";
1011
//proob comment
1112
//Firebase
1213
import { selectToArray } from "./redux/shop/shop.selectors";
@@ -23,27 +24,11 @@ class App extends React.Component {
2324
unsubscribeFromAuth = null;
2425

2526
componentDidMount() {
26-
/*
27-
this.unsubscribeFromAuth = auth.onAuthStateChanged(async userAuth => {
28-
if (userAuth) {
29-
const userRef = await createUserProfileDocument(userAuth);
30-
31-
userRef.onSnapshot(snapShot => {
32-
setCurrentUser({
33-
id: snapShot.id,
34-
...snapShot.data()
35-
});
36-
});
37-
}
38-
39-
40-
setCurrentUser(userAuth);
41-
//Insercion en la base de datos...
42-
/*addCollectionAndDocuments(
43-
"collections",
44-
collectionsArray.map(({ title, items }) => ({ title, items }))
45-
);
46-
});*/
27+
console.log("Me estoy fijando que esta pasando");
28+
const {checkUserSession} = this.props;
29+
const aux = checkUserSession();
30+
console.log("vino : ",aux);
31+
4732
}
4833

4934
componentWillUnmount() {
@@ -81,4 +66,8 @@ const mapStateToProps = createStructuredSelector({
8166
collectionsArray: selectToArray
8267
});
8368

84-
export default connect(mapStateToProps)(App);
69+
const mapDispatchToProps = dispatch => ({
70+
checkUserSession : () => dispatch(checkUserSession())
71+
})
72+
73+
export default connect(mapStateToProps,mapDispatchToProps)(App);

src/components/header/header.component.jsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import CartIcon from "../cart-icon/cart-icon.component";
77
import CartDropdown from "../cart-dropdown/cart-dropdown";
88
import { selectCartHidden } from "../../redux/cart/cart.selectors";
99
import { selectCurrentUser } from "../../redux/user/user.selectors";
10+
import {signOutStart} from "../../redux/user/user.actions";
1011
import {
1112
LogoContainer,
1213
OptionsContainer,
1314
OptionLink,
15+
OptionDiv,
1416
HeaderContainer
1517
} from "./header.styles";
1618

17-
const Header = ({ currentUser, cartHidden }) => (
19+
const Header = ({ currentUser, cartHidden,signOut }) => (
1820
<HeaderContainer>
1921
<LogoContainer to="/">
2022
<Logo className="logo" />
@@ -23,9 +25,9 @@ const Header = ({ currentUser, cartHidden }) => (
2325
<OptionLink to="/shop">SHOP</OptionLink>
2426
<OptionLink to="/shop">CONTACT</OptionLink>
2527
{currentUser ? (
26-
<OptionLink to="/" as="div" onClick={() => auth.signOut()}>
28+
<OptionDiv as="div" onClick={signOut}>
2729
SIGN OUT
28-
</OptionLink>
30+
</OptionDiv>
2931
) : (
3032
<OptionLink to="/signin">SIGN IN</OptionLink>
3133
)}
@@ -40,4 +42,9 @@ const mapStateToProps = createStructuredSelector({
4042
cartHidden: selectCartHidden
4143
});
4244

43-
export default connect(mapStateToProps)(Header);
45+
const mapDispatchToProps = dispatch =>({
46+
signOut: () => dispatch(signOutStart())
47+
})
48+
49+
50+
export default connect(mapStateToProps,mapDispatchToProps)(Header);

src/components/header/header.styles.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ export const OptionsContainer = styled.div`
3131
export const OptionLink = styled(Link)`
3232
${OptionsContainerStyles}
3333
`;
34+
35+
export const OptionDiv = styled.div`
36+
${OptionsContainerStyles}
37+
`;

src/firebase/firebase.utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,13 @@ export const googleProvider = new firebase.auth.GoogleAuthProvider();
9191
googleProvider.setCustomParameters({ prompt: "select_account" });
9292
export const signInWithGoogle = () => auth.signInWithPopup(googleProvider);
9393

94+
export const getCurrentUser = () => {
95+
return new Promise((resolve, reject) => {
96+
const unsubscribe = auth.onAuthStateChanged(userAuth => {
97+
unsubscribe();
98+
resolve(userAuth);
99+
}, reject);
100+
});
101+
};
102+
94103
export default firebase;

src/redux/user/user.actions.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,23 @@ export const signInFailure = (error) => ({
2727
payload : error
2828
})
2929

30+
export const signOutFailure = (error) =>({
31+
type : UserActionTypes.SIGN_OUT_FAILURE,
32+
payload : error
33+
})
34+
35+
export const signOutSuccess = (error) =>({
36+
type : UserActionTypes.SIGN_OUT_SUCCESS,
37+
payload : error
38+
})
39+
40+
export const checkUserSession = () => ({
41+
type : UserActionTypes.CHECK_USER_SESSION
42+
})
43+
44+
export const signOutStart = () =>({
45+
type : UserActionTypes.SIGN_OUT_START
46+
})
47+
3048

3149

src/redux/user/user.reducer.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ const userReducer = (state = INITIAL_STATE, action) => {
2424

2525
}
2626

27+
case UserActionTypes.SIGN_OUT_SUCCESS :
28+
return{
29+
...state,
30+
currentUser : null,
31+
error : null
32+
}
33+
34+
case UserActionTypes.SIGN_IN_FAILURE :
35+
return{
36+
...state,
37+
error : action.payload
38+
}
39+
2740

2841
default:
2942
return state;

src/redux/user/user.sagas.js

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {takeLatest, put, all, call} from "redux-saga/effects";
22
import UserActionTypes from "./user.types";
3-
import {signInSuccess,signInFailure} from "./user.actions";
4-
import {auth, googleProvider, createUserProfileDocument} from "../../firebase/firebase.utils";
3+
import {signInSuccess,signInFailure, signOutFailure, signOutSuccess} from "./user.actions";
4+
import {auth, googleProvider, createUserProfileDocument,getCurrentUser} from "../../firebase/firebase.utils";
55

6-
export function* getSnapShotFromUserAuth(userAuth){
6+
export function* getSnapshotFromUserAuth(userAuth){
77
console.log("Me esta llegando : ",userAuth);
88

99
try{
@@ -20,24 +20,39 @@ export function* getSnapShotFromUserAuth(userAuth){
2020

2121
}
2222

23-
export function* signInWithGoogle(){
23+
export function* isUserAthenticated(){
2424
try{
25-
const {user} = yield (auth.signInWithPopup(googleProvider));
26-
yield getSnapShotFromUserAuth(user);
27-
25+
const userAuth = yield getCurrentUser();
26+
if(!userAuth) return;
27+
yield getSnapshotFromUserAuth(userAuth);
28+
2829

2930
}
3031
catch(error){
3132
yield put(signInFailure(error))
32-
}
33-
33+
}
34+
35+
}
36+
37+
export function* onCheckUserSession(){
38+
yield takeLatest(UserActionTypes.CHECK_USER_SESSION,isUserAthenticated)
39+
40+
}
41+
42+
export function* signInWithGoogle(){
43+
try {
44+
const { user } = yield auth.signInWithPopup(googleProvider);
45+
yield getSnapshotFromUserAuth(user);
46+
} catch (error) {
47+
yield put(signInFailure(error));
48+
}
3449

3550
}
3651

3752
export function* signInWithEmail({payload : {email,password}}){
3853
try{
39-
const {user} = yield auth.signInWithEmailAndPassword(email,password);
40-
yield getSnapShotFromUserAuth(user);
54+
const {user} = auth.signInWithEmailAndPassword(email,password);
55+
yield getSnapshotFromUserAuth(user);
4156

4257

4358
}
@@ -48,6 +63,24 @@ export function* signInWithEmail({payload : {email,password}}){
4863
}
4964
}
5065

66+
export function* signOut(){
67+
console.log("Estoy viendo como estoy...");
68+
try{
69+
70+
yield auth.signOut();
71+
console.log("por ahora voy bien");
72+
yield put(signOutSuccess());
73+
74+
}
75+
catch(error){
76+
yield put(signOutFailure(error))
77+
}
78+
}
79+
80+
export function* onSignOutStart(){
81+
yield takeLatest(UserActionTypes.SIGN_OUT_START,signOut)
82+
}
83+
5184
export function* onGoogleSignInStart(){
5285
yield takeLatest(UserActionTypes.GOOGLE_SIGN_IN_START,signInWithGoogle)
5386
}
@@ -58,6 +91,6 @@ export function* onEmailSignInStart(){
5891

5992
export function* userSagas() {
6093
yield all([
61-
call(onGoogleSignInStart),call(onEmailSignInStart)
94+
call(onGoogleSignInStart),call(onCheckUserSession),call(onSignOutStart)
6295
]);
6396
}

src/redux/user/user.types.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const UserActionTypes = {
44
SIGN_IN_SUCCESS : 'SIGN_IN_SUCCESS',
55
SIGN_IN_FAILURE : 'SIGN_IN_FAILURE',
66
EMAIL_SIGN_IN_START : 'EMAIL_SIGN_IN_START',
7+
CHECK_USER_SESSION : 'CHECK_USER_SESSION',
8+
SIGN_OUT_FAILURE : 'SIGN_OUT_FAILURE',
9+
SIGN_OUT_SUCCESS : 'SIGN_OUT_SUCCESS',
10+
SIGN_OUT_START : 'SIGN_OUT_START'
711

812
};
913

0 commit comments

Comments
 (0)