From c498901aac2e5c6cd094458958356435b8742491 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Thu, 17 Aug 2017 17:15:24 -0300 Subject: [PATCH] auto login connect --- app/actions/login.js | 5 ++--- app/lib/rocketchat.js | 10 ++++++---- app/reducers/login.js | 6 +++++- app/sagas/logger.js | 14 +++++++------- app/sagas/login.js | 18 +++++++++--------- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/app/actions/login.js b/app/actions/login.js index 95a30706..90cd937c 100644 --- a/app/actions/login.js +++ b/app/actions/login.js @@ -7,11 +7,10 @@ export function loginRequest(credentials) { }; } -export function loginSuccess({ token = {} }) { - console.log('loginSuccess', token); +export function loginSuccess(user) { return { type: types.LOGIN.SUCCESS, - token + user }; } diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index b4235300..42def78f 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -61,10 +61,12 @@ const RocketChat = { reduxStore.dispatch(disconnect()); }); Meteor.ddp.on('connected', (err) => { - !err && reduxStore.dispatch(connectSuccess()); - !err && resolve(); + console.log('connected'); + reduxStore.dispatch(connectSuccess()); + resolve(); }); Meteor.ddp.on('loggin', () => { + console.log('Meteor.ddp.on(\'loggin\','); reduxStore.dispatch(loginSuccess({})); }); Meteor.ddp.on('connected', () => { @@ -212,7 +214,7 @@ const RocketChat = { } return reject(err); } - if (data.messages.length) { + if (data && data.messages.length) { realm.write(() => { data.messages.forEach((message) => { message.temp = false; @@ -224,7 +226,7 @@ const RocketChat = { } if (cb) { - if (data.messages.length < 20) { + if (data && data.messages.length < 20) { cb({ end: true }); } else { cb({ end: false }); diff --git a/app/reducers/login.js b/app/reducers/login.js index 6cd900f6..3ed4e74d 100644 --- a/app/reducers/login.js +++ b/app/reducers/login.js @@ -19,6 +19,7 @@ export default function login(state = initialState, action) { return { ...state, isFetching: false, isAuthenticated: true, + user: action.user, // token: action.token, failure: false // user: action.user @@ -32,7 +33,10 @@ export default function login(state = initialState, action) { }; case types.LOGOUT: console.log('LOGOUT'); - return initialState; + return { ...state, + isFetching: false, + isAuthenticated: false + }; default: return state; } diff --git a/app/sagas/logger.js b/app/sagas/logger.js index 85781127..a8c5f485 100644 --- a/app/sagas/logger.js +++ b/app/sagas/logger.js @@ -1,12 +1,12 @@ import { select, takeEvery } from 'redux-saga/effects'; const root = function* watchAndLog() { - yield takeEvery('*', function* logger(action) { - const state = yield select(); - const tmp = { ...state }; - delete tmp.settings; - console.log('action', action); - console.log('state after', tmp); - }); + // yield takeEvery('*', function* logger(action) { + // const state = yield select(); + // const tmp = { ...state }; + // delete tmp.settings; + // console.log('action', action); + // console.log('state after', tmp); + // }); }; export default root; diff --git a/app/sagas/login.js b/app/sagas/login.js index dfa053ec..62555fd3 100644 --- a/app/sagas/login.js +++ b/app/sagas/login.js @@ -1,19 +1,20 @@ import React from 'react'; import { take, put, call, takeLast, fork, select } from 'redux-saga/effects'; import * as types from '../actions/actionsTypes'; -import { loginSuccess, loginFailure, logout } from '../actions/login'; +import { loginRequest, loginSuccess, loginFailure, logout } from '../actions/login'; import RocketChat from '../lib/rocketchat'; -const getUser = state => state.login; +const getUser = state => state.login.user; function loginCall(args) { - return RocketChat.loginWithPassword(args); + return args.resume ? RocketChat.login(args) : RocketChat.loginWithPassword(args); } const auto = function* auto() { while (true) { + yield take(types.LOGOUT); yield take(types.METEOR.SUCCESS); const user = yield select(getUser); if (user.token) { - RocketChat.login({ resume: user.token }); + yield put(loginRequest({ resume: user.token })); } } }; @@ -21,22 +22,21 @@ const watchLoginRequest = function* watchLoginRequest() { while (true) { try { yield take(types.METEOR.SUCCESS); - console.log('\n\n[LOGIN METEOR CONNECTED]\n\n'); + // console.log('\n\n[LOGIN METEOR CONNECTED]\n\n'); const payload = yield take(types.LOGIN.REQUEST); try { const response = yield call(loginCall, payload); - console.log(response); yield put(loginSuccess(response)); console.log('\n\n[LOGIN SUCCESS]\n\n'); } catch (err) { - console.log('\n\n[LOGIN FAILURE]\n\n', err); + // console.log('\n\n[LOGIN FAILURE]\n\n', err); yield put(loginFailure(err.status)); } yield take(types.METEOR.DISCONNECT); console.log('\n\n[METEOR DISCONNECT LOGOUT]\n\n'); yield put(logout()); } catch (e) { - console.log(e); + console.error(e); } } }; @@ -45,4 +45,4 @@ const root = function* root() { yield fork(watchLoginRequest); yield fork(auto); }; -export default watchLoginRequest; +export default root;