Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/routes/AuthPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SignInForm from '../auth/SignInForm'
import SignUpForm from '../auth/SignUpForm'
import {Route, NavLink} from 'react-router-dom'
import {connect} from 'react-redux'
import {signUp, moduleName} from '../../ducks/auth'
import {signUp, moduleName, signIn} from '../../ducks/auth'
import Loader from '../common/Loader'

class AuthPage extends Component {
Expand All @@ -25,10 +25,10 @@ class AuthPage extends Component {
)
}

handleSignIn = (values) => console.log('---', values)
handleSignIn = ({email, password}) => this.props.signIn(email, password)
handleSignUp = ({email, password}) => this.props.signUp(email, password)
}

export default connect(state => ({
loading: state[moduleName].loading
}), {signUp})(AuthPage)
}), {signUp, signIn})(AuthPage)
31 changes: 31 additions & 0 deletions src/ducks/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const moduleName = 'auth'
export const SIGN_UP_REQUEST = `${appName}/${moduleName}/SIGN_UP_REQUEST`
export const SIGN_UP_SUCCESS = `${appName}/${moduleName}/SIGN_UP_SUCCESS`
export const SIGN_UP_ERROR = `${appName}/${moduleName}/SIGN_UP_ERROR`
export const SIGN_IN_REQUEST = `${appName}/${moduleName}/SIGN_IN_REQUEST`
export const SIGN_IN_SUCCESS = `${appName}/${moduleName}/SIGN_IN_SUCCESS`
export const SIGN_IN_ERROR = `${appName}/${moduleName}/SIGN_IN_ERROR`
export const SIGN_OUT_REQUEST = `${appName}/${moduleName}/SIGN_OUT_REQUEST`
export const SIGN_OUT_SUCCESS = `${appName}/${moduleName}/SIGN_OUT_SUCCESS`

Expand Down Expand Up @@ -53,6 +55,13 @@ export function signUp(email, password) {
}
}

export function signIn(email, password) {
return {
type: SIGN_IN_REQUEST,
payload: {email, password}
}
}

export function signOut() {
return {
type: SIGN_OUT_REQUEST
Expand Down Expand Up @@ -103,6 +112,27 @@ export function signUp(email, password) {
}
*/


export const signInSaga = function * (action) {
const auth = firebase.auth()

try {
const user = yield call(
[auth, auth.signInWithEmailAndPassword],
action.payload.email, action.payload.password
)
yield put({
type: SIGN_IN_SUCCESS,
payload: {user}
})
} catch (error) {
yield put({
type: SIGN_IN_ERROR,
error
})
}
}

export const watchStatusChange = function * () {
const auth = firebase.auth()
try {
Expand Down Expand Up @@ -144,6 +174,7 @@ export const saga = function * () {
yield all([
signUpSaga(),
watchStatusChange(),
takeEvery(SIGN_IN_REQUEST, signInSaga),
takeEvery(SIGN_OUT_REQUEST, signOutSaga)
])
}
96 changes: 96 additions & 0 deletions src/ducks/auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import firebase from 'firebase'
import {push} from 'react-router-redux'
import {all, cps, call, put, take, takeEvery} from 'redux-saga/effects'
import {
signUpSaga, signOutSaga, watchStatusChange, signInSaga, saga as authSaga,
SIGN_UP_REQUEST, SIGN_UP_SUCCESS, SIGN_UP_ERROR,
SIGN_IN_REQUEST, SIGN_IN_SUCCESS, SIGN_IN_ERROR,
SIGN_OUT_REQUEST, SIGN_OUT_SUCCESS,
} from './auth';

const auth = firebase.auth()
const user = {
email: 'a@a.aa',
password: 'aaaaaaaaaa',
}

describe('test auth sagas', () => {
it('should sign up', () => {
const saga = signUpSaga();

expect(saga.next().value).toEqual(take(SIGN_UP_REQUEST))

const action = {
type: SIGN_UP_REQUEST,
payload: { ...user }
}

expect(saga.next(action).value).toEqual(
call([auth, auth.createUserWithEmailAndPassword], user.email, user.password)
)

expect(saga.next(user).value).toEqual(put({
type: SIGN_UP_SUCCESS,
payload: { user }
}))

expect(saga.throw('test').value).toEqual(put({
type: SIGN_UP_ERROR,
error: 'test',
}))
})

it('should sign in', () => {
const action = {
type: SIGN_IN_REQUEST,
payload: { ...user }
}

const saga = signInSaga(action);

expect(saga.next().value).toEqual(
call([auth, auth.signInWithEmailAndPassword], user.email, user.password)
)

expect(saga.next(user).value).toEqual(put({
type: SIGN_IN_SUCCESS,
payload: { user }
}))

expect(saga.throw('test').value).toEqual(put({
type: SIGN_IN_ERROR,
error: 'test',
}))
})

it('should sign out', () => {
const action = {
type: SIGN_OUT_REQUEST,
}

const saga = signOutSaga(action);

expect(saga.next().value).toEqual(
call([auth, auth.signOut])
)

expect(saga.next().value).toEqual(put({
type: SIGN_OUT_SUCCESS
}))

expect(saga.next().value).toEqual(put(push('/auth/signin')))
})

it('should watch auth status', () => {
const saga = watchStatusChange();

expect(saga.next().value).toEqual(
cps([auth, auth.onAuthStateChanged])
)

expect(saga.throw(user).value).toEqual(put({
type: SIGN_IN_SUCCESS,
payload: { user },
}))
})
})
3 changes: 3 additions & 0 deletions src/ducks/people.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {reset} from 'redux-form'
import {appName} from '../config'
import {Record, List} from 'immutable'
import {put, call, takeEvery} from 'redux-saga/effects'
Expand Down Expand Up @@ -46,6 +47,8 @@ export const addPersonSaga = function * (action) {
type: ADD_PERSON,
payload: {...action.payload, id}
})

yield put(reset('person'))
}

/*
Expand Down