auto login connect

This commit is contained in:
Guilherme Gazzo 2017-08-17 17:15:24 -03:00
parent 0de8cf65a4
commit c498901aac
No known key found for this signature in database
GPG Key ID: 1F85C9AD922D0829
5 changed files with 29 additions and 24 deletions

View File

@ -7,11 +7,10 @@ export function loginRequest(credentials) {
}; };
} }
export function loginSuccess({ token = {} }) { export function loginSuccess(user) {
console.log('loginSuccess', token);
return { return {
type: types.LOGIN.SUCCESS, type: types.LOGIN.SUCCESS,
token user
}; };
} }

View File

@ -61,10 +61,12 @@ const RocketChat = {
reduxStore.dispatch(disconnect()); reduxStore.dispatch(disconnect());
}); });
Meteor.ddp.on('connected', (err) => { Meteor.ddp.on('connected', (err) => {
!err && reduxStore.dispatch(connectSuccess()); console.log('connected');
!err && resolve(); reduxStore.dispatch(connectSuccess());
resolve();
}); });
Meteor.ddp.on('loggin', () => { Meteor.ddp.on('loggin', () => {
console.log('Meteor.ddp.on(\'loggin\',');
reduxStore.dispatch(loginSuccess({})); reduxStore.dispatch(loginSuccess({}));
}); });
Meteor.ddp.on('connected', () => { Meteor.ddp.on('connected', () => {
@ -212,7 +214,7 @@ const RocketChat = {
} }
return reject(err); return reject(err);
} }
if (data.messages.length) { if (data && data.messages.length) {
realm.write(() => { realm.write(() => {
data.messages.forEach((message) => { data.messages.forEach((message) => {
message.temp = false; message.temp = false;
@ -224,7 +226,7 @@ const RocketChat = {
} }
if (cb) { if (cb) {
if (data.messages.length < 20) { if (data && data.messages.length < 20) {
cb({ end: true }); cb({ end: true });
} else { } else {
cb({ end: false }); cb({ end: false });

View File

@ -19,6 +19,7 @@ export default function login(state = initialState, action) {
return { ...state, return { ...state,
isFetching: false, isFetching: false,
isAuthenticated: true, isAuthenticated: true,
user: action.user,
// token: action.token, // token: action.token,
failure: false failure: false
// user: action.user // user: action.user
@ -32,7 +33,10 @@ export default function login(state = initialState, action) {
}; };
case types.LOGOUT: case types.LOGOUT:
console.log('LOGOUT'); console.log('LOGOUT');
return initialState; return { ...state,
isFetching: false,
isAuthenticated: false
};
default: default:
return state; return state;
} }

View File

@ -1,12 +1,12 @@
import { select, takeEvery } from 'redux-saga/effects'; import { select, takeEvery } from 'redux-saga/effects';
const root = function* watchAndLog() { const root = function* watchAndLog() {
yield takeEvery('*', function* logger(action) { // yield takeEvery('*', function* logger(action) {
const state = yield select(); // const state = yield select();
const tmp = { ...state }; // const tmp = { ...state };
delete tmp.settings; // delete tmp.settings;
console.log('action', action); // console.log('action', action);
console.log('state after', tmp); // console.log('state after', tmp);
}); // });
}; };
export default root; export default root;

View File

@ -1,19 +1,20 @@
import React from 'react'; import React from 'react';
import { take, put, call, takeLast, fork, select } from 'redux-saga/effects'; import { take, put, call, takeLast, fork, select } from 'redux-saga/effects';
import * as types from '../actions/actionsTypes'; 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'; import RocketChat from '../lib/rocketchat';
const getUser = state => state.login; const getUser = state => state.login.user;
function loginCall(args) { function loginCall(args) {
return RocketChat.loginWithPassword(args); return args.resume ? RocketChat.login(args) : RocketChat.loginWithPassword(args);
} }
const auto = function* auto() { const auto = function* auto() {
while (true) { while (true) {
yield take(types.LOGOUT);
yield take(types.METEOR.SUCCESS); yield take(types.METEOR.SUCCESS);
const user = yield select(getUser); const user = yield select(getUser);
if (user.token) { if (user.token) {
RocketChat.login({ resume: user.token }); yield put(loginRequest({ resume: user.token }));
} }
} }
}; };
@ -21,22 +22,21 @@ const watchLoginRequest = function* watchLoginRequest() {
while (true) { while (true) {
try { try {
yield take(types.METEOR.SUCCESS); 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); const payload = yield take(types.LOGIN.REQUEST);
try { try {
const response = yield call(loginCall, payload); const response = yield call(loginCall, payload);
console.log(response);
yield put(loginSuccess(response)); yield put(loginSuccess(response));
console.log('\n\n[LOGIN SUCCESS]\n\n'); console.log('\n\n[LOGIN SUCCESS]\n\n');
} catch (err) { } 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 put(loginFailure(err.status));
} }
yield take(types.METEOR.DISCONNECT); yield take(types.METEOR.DISCONNECT);
console.log('\n\n[METEOR DISCONNECT LOGOUT]\n\n'); console.log('\n\n[METEOR DISCONNECT LOGOUT]\n\n');
yield put(logout()); yield put(logout());
} catch (e) { } catch (e) {
console.log(e); console.error(e);
} }
} }
}; };
@ -45,4 +45,4 @@ const root = function* root() {
yield fork(watchLoginRequest); yield fork(watchLoginRequest);
yield fork(auto); yield fork(auto);
}; };
export default watchLoginRequest; export default root;