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 = {} }) {
console.log('loginSuccess', token);
export function loginSuccess(user) {
return {
type: types.LOGIN.SUCCESS,
token
user
};
}

View File

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

View File

@ -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;
}

View File

@ -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;

View File

@ -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;