-
Notifications
You must be signed in to change notification settings - Fork 21
hw5 #28
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
momonomonoto
wants to merge
2
commits into
romabelka:master
Choose a base branch
from
momonomonoto:newHw5
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
hw5 #28
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| import firebase from 'firebase' | ||
|
|
||
| export const appName = "advreact-21-08" | ||
| export const appName = "advreact-21-081" | ||
| export const firebaseConfig = { | ||
| apiKey: "AIzaSyDjA6CeIHuni5lNm4ML1b-TSxJltsYUO8g", | ||
| authDomain: `${appName}.firebaseapp.com`, | ||
| databaseURL: `https://${appName}.firebaseio.com`, | ||
| projectId: appName, | ||
| storageBucket: `${appName}.appspot.com`, | ||
| messagingSenderId: "789814589283" | ||
| apiKey: "AIzaSyC0hcpDsBhr85usoHIwloBeYbkDHeUcqKo", | ||
| authDomain: "advreact-21-081.firebaseapp.com", | ||
| databaseURL: "https://advreact-21-081.firebaseio.com", | ||
| projectId: "advreact-21-081", | ||
| storageBucket: "advreact-21-081.appspot.com", | ||
| messagingSenderId: "450237050585" | ||
| } | ||
|
|
||
| firebase.initializeApp(firebaseConfig) |
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 |
|---|---|---|
|
|
@@ -2,15 +2,14 @@ import firebase from 'firebase' | |
| import {appName} from '../config' | ||
| import {Record} from 'immutable' | ||
| //import store from '../redux' | ||
| import {all, cps, call, put, take, takeEvery} from 'redux-saga/effects' | ||
| import {all, cps, call, put, take, takeEvery, spawn, cancel, fork} from 'redux-saga/effects' | ||
| import {delay, eventChannel} from 'redux-saga' | ||
| import {push} from 'react-router-redux' | ||
|
|
||
| export const ReducerRecord = Record({ | ||
| user: null, | ||
| error: null, | ||
| loading: false | ||
| }) | ||
|
|
||
| export const moduleName = 'auth' | ||
| export const SIGN_UP_REQUEST = `${appName}/${moduleName}/SIGN_UP_REQUEST` | ||
| export const SIGN_UP_SUCCESS = `${appName}/${moduleName}/SIGN_UP_SUCCESS` | ||
|
|
@@ -20,62 +19,50 @@ export const SIGN_IN_ERROR = `${appName}/${moduleName}/SIGN_IN_ERROR` | |
| export const SIGN_IN_SUCCESS = `${appName}/${moduleName}/SIGN_IN_SUCCESS` | ||
| export const SIGN_OUT_REQUEST = `${appName}/${moduleName}/SIGN_OUT_REQUEST` | ||
| export const SIGN_OUT_SUCCESS = `${appName}/${moduleName}/SIGN_OUT_SUCCESS` | ||
|
|
||
| //console.log('---', ReducerRecord) | ||
| export default function reducer(state = new ReducerRecord(), action) { | ||
| const {type, payload, error} = action | ||
|
|
||
| switch (type) { | ||
| case SIGN_UP_REQUEST: | ||
| case SIGN_IN_REQUEST: | ||
| return state.set('loading', true) | ||
|
|
||
| case SIGN_IN_SUCCESS: | ||
| return state | ||
| .set('loading', false) | ||
| .set('user', payload.user) | ||
| .set('error', null) | ||
|
|
||
| case SIGN_UP_ERROR: | ||
| case SIGN_IN_ERROR: | ||
| return state | ||
| .set('loading', false) | ||
| .set('error', error) | ||
|
|
||
| case SIGN_OUT_SUCCESS: | ||
| return new ReducerRecord() | ||
|
|
||
| default: | ||
| return state | ||
| } | ||
| } | ||
|
|
||
| export function signUp(email, password) { | ||
| return { | ||
| type: SIGN_UP_REQUEST, | ||
| payload: {email, password} | ||
| } | ||
| } | ||
|
|
||
| export function signIn(email, password) { | ||
| return { | ||
| type: SIGN_IN_REQUEST, | ||
| payload: {email, password} | ||
| } | ||
| } | ||
|
|
||
| export function signOut() { | ||
| return { | ||
| type: SIGN_OUT_REQUEST | ||
| } | ||
| } | ||
|
|
||
| export const signUpSaga = function * () { | ||
| export const signUpSaga = function *() { | ||
| const auth = firebase.auth() | ||
|
|
||
| while (true) { | ||
| const action = yield take(SIGN_UP_REQUEST) | ||
|
|
||
| try { | ||
| const user = yield call( | ||
| [auth, auth.createUserWithEmailAndPassword], | ||
|
|
@@ -93,13 +80,66 @@ export const signUpSaga = function * () { | |
| } | ||
| } | ||
| } | ||
|
|
||
| export const signInSaga = function * () { | ||
| // const createAuthSocket = (payload) => eventChannel(emit => { | ||
| // const unsubscribe = firebase.auth().onAuthStateChanged( | ||
| // user => emit({user}), | ||
| // error => emit({error}) | ||
| // ) | ||
| // return unsubscribe; | ||
| // }) | ||
|
|
||
| const createAuthSocket = () => eventChannel(emmit => { | ||
| const auth = firebase.auth() | ||
| const callback = (user) => emmit({user}) | ||
| auth.onAuthStateChanged(callback) | ||
| return () => {} | ||
| }) | ||
|
|
||
| export const checkAuth = function *(payload) { | ||
| const auth = firebase.auth(); | ||
| try { | ||
| yield call( | ||
| [auth, auth.signInWithEmailAndPassword], | ||
| payload.email, payload.password | ||
| ) | ||
| } | ||
| catch (error) { | ||
| yield put({ | ||
| type: SIGN_IN_ERROR | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| export const realtimeSync = function *(payload) { | ||
| const socket = yield call(checkAuth,payload) | ||
| const socketChannel = yield call(createAuthSocket, socket) | ||
| while(true) { | ||
| const {user} = yield take(socketChannel) | ||
| if(user) { | ||
| yield put({ | ||
| type: SIGN_IN_SUCCESS, | ||
| payload: {user} | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export const cancellableSync = function *() { | ||
| let task | ||
| while (true) { | ||
| const action = yield take(SIGN_IN_REQUEST) | ||
| const {payload} = yield take(SIGN_IN_REQUEST) | ||
|
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. не очень понял, что тего ты хочешь достичь |
||
| if (payload) { | ||
| task = yield fork(realtimeSync, payload) | ||
| } else if (task) { | ||
| yield cancel(task) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export const signInSaga = function *() { | ||
| const auth = firebase.auth() | ||
| while (true) { | ||
| const action = yield take(SIGN_IN_REQUEST) | ||
| try { | ||
| const user = yield call( | ||
| [auth, auth.signInWithEmailAndPassword], | ||
|
|
@@ -119,26 +159,25 @@ export const signInSaga = function * () { | |
| } | ||
|
|
||
| /* | ||
| export function signUp(email, password) { | ||
| return (dispatch) => { | ||
| dispatch({ | ||
| type: SIGN_UP_REQUEST | ||
| }) | ||
|
|
||
| firebase.auth().createUserWithEmailAndPassword(email, password) | ||
| .then(user => dispatch({ | ||
| type: SIGN_UP_SUCCESS, | ||
| payload: {user} | ||
| })) | ||
| .catch(error => dispatch({ | ||
| type: SIGN_UP_ERROR, | ||
| error | ||
| })) | ||
| } | ||
| } | ||
| */ | ||
|
|
||
| export const watchStatusChange = function * () { | ||
| export function signUp(email, password) { | ||
| return (dispatch) => { | ||
| dispatch({ | ||
| type: SIGN_UP_REQUEST | ||
| }) | ||
|
|
||
| firebase.auth().createUserWithEmailAndPassword(email, password) | ||
| .then(user => dispatch({ | ||
| type: SIGN_UP_SUCCESS, | ||
| payload: {user} | ||
| })) | ||
| .catch(error => dispatch({ | ||
| type: SIGN_UP_ERROR, | ||
| error | ||
| })) | ||
| } | ||
| } | ||
| */ | ||
| export const watchStatusChange = function *() { | ||
| const auth = firebase.auth() | ||
| try { | ||
| yield cps([auth, auth.onAuthStateChanged]) | ||
|
|
@@ -149,36 +188,32 @@ export const watchStatusChange = function * () { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| firebase.auth().onAuthStateChanged(user => { | ||
| const store = require('../redux').default | ||
| store.dispatch({ | ||
| type: SIGN_IN_SUCCESS, | ||
| payload: {user} | ||
| }) | ||
| }) | ||
|
|
||
| */ | ||
|
|
||
| export const signOutSaga = function * () { | ||
| firebase.auth().onAuthStateChanged(user => { | ||
| const store = require('../redux').default | ||
| store.dispatch({ | ||
| type: SIGN_IN_SUCCESS, | ||
| payload: {user} | ||
| }) | ||
| }) | ||
|
|
||
| */ | ||
| export const signOutSaga = function *() { | ||
| const auth = firebase.auth() | ||
|
|
||
| try { | ||
| yield call([auth, auth.signOut]) | ||
| yield put({ | ||
| type: SIGN_OUT_SUCCESS | ||
| }) | ||
| yield put(push('/auth/signin')) | ||
| } catch (_) { | ||
|
|
||
| } | ||
| } | ||
|
|
||
| export const saga = function * () { | ||
| export const saga = function *() { | ||
| yield spawn(cancellableSync); | ||
| yield all([ | ||
| signUpSaga(), | ||
| signInSaga(), | ||
| // signInSaga(), | ||
| watchStatusChange(), | ||
| takeEvery(SIGN_OUT_REQUEST, signOutSaga) | ||
| ]) | ||
|
|
||
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.
зачем ты возвращаешь пустую функцию? Такое тебя в результате приведет к неприятным багам. auth.onAuthStateChanged возвращает нужный колбек