From b7e5fd629385523cab7bb520f8710cd4392342b7 Mon Sep 17 00:00:00 2001 From: Aleksandr Gavrilov Date: Wed, 25 Oct 2017 19:45:02 +0300 Subject: [PATCH 1/5] fix firebase config --- src/config.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/config.js b/src/config.js index 2782a0b..5c9f8f6 100644 --- a/src/config.js +++ b/src/config.js @@ -1,12 +1,13 @@ import firebase from 'firebase' export const appName = 'advreact-1610' +const appId = 'ec352' firebase.initializeApp({ - apiKey: "AIzaSyB31xpTtp4Jln_hb2kAbE4PGf6Mi8EgLyA", - authDomain: `${appName}.firebaseapp.com`, - databaseURL: `https://${appName}.firebaseio.com`, - projectId: appName, - storageBucket: "", - messagingSenderId: "397157634637" -}) \ No newline at end of file + apiKey: "AIzaSyB3LVTO7RSDrZAkHBkpzg9T5KkuoCoy4qo", + authDomain: `${appName}-${appId}.firebaseapp.com`, + databaseURL: `https://${appName}-${appId}.firebaseio.com`, + projectId: `${appName}-${appId}`, + storageBucket: `${appName}-${appId}.appspot.com`, + messagingSenderId: "651090769196" +}) From 1a1cf7702a26382897f26b50757a0e56c9855e84 Mon Sep 17 00:00:00 2001 From: Aleksandr Gavrilov Date: Wed, 25 Oct 2017 20:49:52 +0300 Subject: [PATCH 2/5] add signIn saga --- src/components/routes/Auth.js | 6 ++--- src/ducks/auth.js | 50 ++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/components/routes/Auth.js b/src/components/routes/Auth.js index a183ff9..ec4898e 100644 --- a/src/components/routes/Auth.js +++ b/src/components/routes/Auth.js @@ -1,7 +1,7 @@ import React, { Component } from 'react' import {Route, NavLink} from 'react-router-dom' import {connect} from 'react-redux' -import {signUp} from '../../ducks/auth' +import {signUp, signIn} from '../../ducks/auth' import SignIn from '../auth/SignIn' import SignUp from '../auth/SignUp' @@ -22,8 +22,8 @@ class AuthPage extends Component { ) } - handleSignIn = (values) => console.log('---', 'sign in', values) + handleSignIn = ({email, password}) => this.props.signIn(email, password) handleSignUp = ({email, password}) => this.props.signUp(email, password) } -export default connect(null, { signUp })(AuthPage) \ No newline at end of file +export default connect(null, { signUp, signIn })(AuthPage) \ No newline at end of file diff --git a/src/ducks/auth.js b/src/ducks/auth.js index 25f3bf7..c046edf 100644 --- a/src/ducks/auth.js +++ b/src/ducks/auth.js @@ -14,7 +14,9 @@ export const SIGN_UP_START = `${prefix}/SIGN_UP_START` export const SIGN_UP_SUCCESS = `${prefix}/SIGN_UP_SUCCESS` export const SIGN_UP_ERROR = `${prefix}/SIGN_UP_ERROR` +export const SIGN_IN_START = `${prefix}/SIGN_IN_START` export const SIGN_IN_SUCCESS = `${prefix}/SIGN_IN_SUCCESS` +export const SIGN_IN_ERROR = `${prefix}/SIGN_IN_ERROR` /** * Reducer @@ -26,10 +28,11 @@ export const ReducerRecord = Record({ }) export default function reducer(state = new ReducerRecord(), action) { - const {type, payload} = action + const { error, type, payload } = action switch (type) { case SIGN_UP_START: + case SIGN_IN_START: return state.set('loading', true) case SIGN_UP_SUCCESS: @@ -38,6 +41,12 @@ export default function reducer(state = new ReducerRecord(), action) { .set('user', payload.user) .set('loading', false) + case SIGN_UP_ERROR: + case SIGN_IN_ERROR: + return state + .set('loading', false) + .set('error', error) + default: return state } @@ -60,14 +69,12 @@ export function signUp(email, password) { } } -firebase.auth().onAuthStateChanged(user => { - if (!user) return - - window.store.dispatch({ - type: SIGN_IN_SUCCESS, - payload: { user } - }) -}) +export function signIn(email, password) { + return { + type: SIGN_IN_START, + payload: { email, password } + } +} /** * Sagas @@ -96,8 +103,31 @@ export function * signUpSaga() { } } +export function * signInSaga() { + const auth = firebase.auth() + + while (true) { + const { payload } = yield take(SIGN_IN_START) + + try { + const user = yield call([auth, auth.signInWithEmailAndPassword], payload.email, payload.password) // https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword + + yield put({ + type: SIGN_IN_SUCCESS, + payload: { user } + }) + } catch (error) { + yield put({ + type: SIGN_IN_ERROR, + payload: { error } + }) + } + } +} + export function * saga() { yield all([ - signUpSaga() + signUpSaga(), + signInSaga() ]) } \ No newline at end of file From 32d729639039cf42ad3f050abfb9b32822eee662 Mon Sep 17 00:00:00 2001 From: Aleksandr Gavrilov Date: Wed, 25 Oct 2017 21:02:12 +0300 Subject: [PATCH 3/5] add redirect on signIn to /people --- src/ducks/auth.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ducks/auth.js b/src/ducks/auth.js index c046edf..85502d3 100644 --- a/src/ducks/auth.js +++ b/src/ducks/auth.js @@ -3,6 +3,7 @@ import {Record} from 'immutable' import firebase from 'firebase' import {createSelector} from 'reselect' import {call, put, all, take} from 'redux-saga/effects' +import history from '../history' /** * Constants @@ -116,6 +117,8 @@ export function * signInSaga() { type: SIGN_IN_SUCCESS, payload: { user } }) + + history.push('/people') } catch (error) { yield put({ type: SIGN_IN_ERROR, From c06ac4c780defe3c7e86762ac294878a33693dec Mon Sep 17 00:00:00 2001 From: Aleksandr Gavrilov Date: Wed, 25 Oct 2017 21:24:52 +0300 Subject: [PATCH 4/5] add reset forms on SignIn/SignUp --- src/ducks/auth.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ducks/auth.js b/src/ducks/auth.js index 85502d3..6943390 100644 --- a/src/ducks/auth.js +++ b/src/ducks/auth.js @@ -4,6 +4,7 @@ import firebase from 'firebase' import {createSelector} from 'reselect' import {call, put, all, take} from 'redux-saga/effects' import history from '../history' +import {reset} from 'redux-form'; /** * Constants @@ -95,6 +96,8 @@ export function * signUpSaga() { type: SIGN_UP_SUCCESS, payload: {user} }) + + yield call(() => reset(moduleName)) } catch (error) { yield put({ type: SIGN_UP_ERROR, @@ -118,7 +121,9 @@ export function * signInSaga() { payload: { user } }) - history.push('/people') + yield call(() => history.push('/people')) + yield call(() => reset(moduleName)) + } catch (error) { yield put({ type: SIGN_IN_ERROR, From 5f42c32c6498b6543cf8a776e1b05249003de7c9 Mon Sep 17 00:00:00 2001 From: Aleksandr Gavrilov Date: Wed, 25 Oct 2017 22:39:26 +0300 Subject: [PATCH 5/5] add auth test --- src/ducks/auth.test.js | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/ducks/auth.test.js diff --git a/src/ducks/auth.test.js b/src/ducks/auth.test.js new file mode 100644 index 0000000..53ec0ee --- /dev/null +++ b/src/ducks/auth.test.js @@ -0,0 +1,52 @@ +import {signUp, signUpSaga, SIGN_UP_START, SIGN_UP_SUCCESS, + signIn, signInSaga, SIGN_IN_START, SIGN_IN_SUCCESS} from './auth' +import {call, put, take} from 'redux-saga/effects' +import firebase from 'firebase' + +it('should sign up', () => { + const user = { + email: 'authtest@example.com', + password: '1234567890' + } + + const auth = firebase.auth() + + const requestAction = signUp(user.email, user.password) + + const saga = signUpSaga() + + expect(saga.next().value).toEqual(take(SIGN_UP_START)) + + expect(JSON.stringify(saga.next(requestAction).value)).toEqual(JSON.stringify( + call([auth, auth.createUserWithEmailAndPassword], user.email, user.password) + )) + + expect(saga.next(user).value).toEqual(put({ + type: SIGN_UP_SUCCESS, + payload: {user} + })) +}) + +it('should sign in', () => { + const user = { + email: 'authtest@example.com', + password: '1234567890' + } + + const auth = firebase.auth() + + const requestAction = signIn(user.email, user.password) + + const saga = signInSaga() + + expect(saga.next().value).toEqual(take(SIGN_IN_START)) + + expect(saga.next(requestAction).value).toEqual( + call([auth, auth.signInWithEmailAndPassword], user.email, user.password) + ) + + expect(saga.next(user).value).toEqual(put({ + type: SIGN_IN_SUCCESS, + payload: {user} + })) +}) \ No newline at end of file