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/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" +}) diff --git a/src/ducks/auth.js b/src/ducks/auth.js index 25f3bf7..6943390 100644 --- a/src/ducks/auth.js +++ b/src/ducks/auth.js @@ -3,6 +3,8 @@ 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' +import {reset} from 'redux-form'; /** * Constants @@ -14,7 +16,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 +30,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 +43,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 +71,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 @@ -87,6 +96,8 @@ export function * signUpSaga() { type: SIGN_UP_SUCCESS, payload: {user} }) + + yield call(() => reset(moduleName)) } catch (error) { yield put({ type: SIGN_UP_ERROR, @@ -96,8 +107,35 @@ 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 } + }) + + yield call(() => history.push('/people')) + yield call(() => reset(moduleName)) + + } catch (error) { + yield put({ + type: SIGN_IN_ERROR, + payload: { error } + }) + } + } +} + export function * saga() { yield all([ - signUpSaga() + signUpSaga(), + signInSaga() ]) } \ No newline at end of file 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