-
Notifications
You must be signed in to change notification settings - Fork 14
Ht2 #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
algv
wants to merge
5
commits into
romabelka:master
Choose a base branch
from
algv:HT2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ht2 #15
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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')) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| ]) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import {signUp, signUpSaga, SIGN_UP_START, SIGN_UP_SUCCESS, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
| })) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В общем случае все правильно, хотя нам достаточно onAuthStateChange