Skip to content
Open

Ht2 #15

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/Auth.js
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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)
export default connect(null, { signUp, signIn })(AuthPage)
15 changes: 8 additions & 7 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -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"
})
apiKey: "AIzaSyB3LVTO7RSDrZAkHBkpzg9T5KkuoCoy4qo",
authDomain: `${appName}-${appId}.firebaseapp.com`,
databaseURL: `https://${appName}-${appId}.firebaseio.com`,
projectId: `${appName}-${appId}`,
storageBucket: `${appName}-${appId}.appspot.com`,
messagingSenderId: "651090769196"
})
58 changes: 48 additions & 10 deletions src/ducks/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В общем случае все правильно, хотя нам достаточно onAuthStateChange

type: SIGN_IN_SUCCESS,
payload: { user }
})

yield call(() => history.push('/people'))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

возьми push как AC из react-router-redux, сделай put

yield call(() => reset(moduleName))

} catch (error) {
yield put({
type: SIGN_IN_ERROR,
payload: { error }
})
}
}
}

export function * saga() {
yield all([
signUpSaga()
signUpSaga(),
signInSaga()
])
}
52 changes: 52 additions & 0 deletions src/ducks/auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {signUp, signUpSaga, SIGN_UP_START, SIGN_UP_SUCCESS,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хорошо, но в идеале можно еще и редюсер потестить

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}
}))
})