Verify if user has username on login (#64)
* get /me from rest api and append to user reducer * Removed comments from login saga * user reducer logic * Jump to set username screen when user is logged without username * lint fix after merge * lint fix
This commit is contained in:
parent
a568e826e5
commit
c7e35540f5
|
@ -17,6 +17,7 @@ export const LOGIN = createRequestTypes('LOGIN', [
|
||||||
'REGISTER_SUBMIT',
|
'REGISTER_SUBMIT',
|
||||||
'REGISTER_REQUEST',
|
'REGISTER_REQUEST',
|
||||||
'REGISTER_SUCCESS',
|
'REGISTER_SUCCESS',
|
||||||
|
'REGISTER_INCOMPLETE',
|
||||||
'SET_USERNAME_SUBMIT',
|
'SET_USERNAME_SUBMIT',
|
||||||
'SET_USERNAME_REQUEST',
|
'SET_USERNAME_REQUEST',
|
||||||
'SET_USERNAME_SUCCESS'
|
'SET_USERNAME_SUCCESS'
|
||||||
|
|
|
@ -32,6 +32,11 @@ export function registerSuccess(credentials) {
|
||||||
credentials
|
credentials
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
export function registerIncomplete() {
|
||||||
|
return {
|
||||||
|
type: types.LOGIN.REGISTER_INCOMPLETE
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function setUsernameSubmit(credentials) {
|
export function setUsernameSubmit(credentials) {
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -7,6 +7,7 @@ import { appInit } from '../actions';
|
||||||
import AuthRoutes from './routes/AuthRoutes';
|
import AuthRoutes from './routes/AuthRoutes';
|
||||||
import PublicRoutes from './routes/PublicRoutes';
|
import PublicRoutes from './routes/PublicRoutes';
|
||||||
import Loading from '../presentation/Loading';
|
import Loading from '../presentation/Loading';
|
||||||
|
import * as NavigationService from './routes/NavigationService';
|
||||||
|
|
||||||
@connect(
|
@connect(
|
||||||
state => ({
|
state => ({
|
||||||
|
@ -27,6 +28,10 @@ export default class Routes extends React.Component {
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
this.props.appInit();
|
this.props.appInit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidUpdate() {
|
||||||
|
NavigationService.setNavigator(this.navigator);
|
||||||
|
}
|
||||||
render() {
|
render() {
|
||||||
const { login, app } = this.props;
|
const { login, app } = this.props;
|
||||||
|
|
||||||
|
@ -35,9 +40,9 @@ export default class Routes extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((login.token && !login.failure && !login.isRegistering) || app.ready) {
|
if ((login.token && !login.failure && !login.isRegistering) || app.ready) {
|
||||||
return (<AuthRoutes />);
|
return (<AuthRoutes ref={nav => this.navigator = nav} />);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (<PublicRoutes />);
|
return (<PublicRoutes ref={nav => this.navigator = nav} />);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { NavigationActions } from 'react-navigation';
|
||||||
|
|
||||||
|
const config = {};
|
||||||
|
|
||||||
|
export function setNavigator(nav) {
|
||||||
|
if (nav) {
|
||||||
|
config.navigator = nav;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function navigate(routeName, params) {
|
||||||
|
if (config.navigator && routeName) {
|
||||||
|
const action = NavigationActions.navigate({ routeName, params });
|
||||||
|
config.navigator.dispatch(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function goBack() {
|
||||||
|
if (config.navigator) {
|
||||||
|
const action = NavigationActions.back({});
|
||||||
|
config.navigator.dispatch(action);
|
||||||
|
}
|
||||||
|
}
|
|
@ -122,6 +122,17 @@ const RocketChat = {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
me({ server, token, userId }) {
|
||||||
|
return fetch(`${ server }/api/v1/me`, {
|
||||||
|
method: 'get',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-Auth-Token': token,
|
||||||
|
'X-User-Id': userId
|
||||||
|
}
|
||||||
|
}).then(response => response.json());
|
||||||
|
},
|
||||||
|
|
||||||
register({ credentials }) {
|
register({ credentials }) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
Meteor.call('registerUser', credentials, (err, userId) => {
|
Meteor.call('registerUser', credentials, (err, userId) => {
|
||||||
|
|
|
@ -74,6 +74,11 @@ export default function login(state = initialState, action) {
|
||||||
isFetching: false,
|
isFetching: false,
|
||||||
isRegistering: false
|
isRegistering: false
|
||||||
};
|
};
|
||||||
|
case types.LOGIN.REGISTER_INCOMPLETE:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
isRegistering: true
|
||||||
|
};
|
||||||
case types.FORGOT_PASSWORD.INIT:
|
case types.FORGOT_PASSWORD.INIT:
|
||||||
return initialState;
|
return initialState;
|
||||||
case types.FORGOT_PASSWORD.REQUEST:
|
case types.FORGOT_PASSWORD.REQUEST:
|
||||||
|
|
|
@ -5,6 +5,7 @@ import {
|
||||||
loginRequest,
|
loginRequest,
|
||||||
loginSubmit,
|
loginSubmit,
|
||||||
registerRequest,
|
registerRequest,
|
||||||
|
registerIncomplete,
|
||||||
loginSuccess,
|
loginSuccess,
|
||||||
loginFailure,
|
loginFailure,
|
||||||
setToken,
|
setToken,
|
||||||
|
@ -16,6 +17,7 @@ import {
|
||||||
forgotPasswordFailure
|
forgotPasswordFailure
|
||||||
} from '../actions/login';
|
} from '../actions/login';
|
||||||
import RocketChat from '../lib/rocketchat';
|
import RocketChat from '../lib/rocketchat';
|
||||||
|
import * as NavigationService from '../containers/routes/NavigationService';
|
||||||
|
|
||||||
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
||||||
const getUser = state => state.login;
|
const getUser = state => state.login;
|
||||||
|
@ -24,6 +26,7 @@ const loginCall = args => (args.resume ? RocketChat.login(args) : RocketChat.log
|
||||||
const registerCall = args => RocketChat.register(args);
|
const registerCall = args => RocketChat.register(args);
|
||||||
const setUsernameCall = args => RocketChat.setUsername(args);
|
const setUsernameCall = args => RocketChat.setUsername(args);
|
||||||
const logoutCall = args => RocketChat.logout(args);
|
const logoutCall = args => RocketChat.logout(args);
|
||||||
|
const meCall = args => RocketChat.me(args);
|
||||||
const forgotPasswordCall = args => RocketChat.forgotPassword(args);
|
const forgotPasswordCall = args => RocketChat.forgotPassword(args);
|
||||||
|
|
||||||
const getToken = function* getToken() {
|
const getToken = function* getToken() {
|
||||||
|
@ -43,35 +46,17 @@ const getToken = function* getToken() {
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLoginWhenServerChanges = function* handleLoginWhenServerChanges() {
|
const handleLoginWhenServerChanges = function* handleLoginWhenServerChanges() {
|
||||||
// do {
|
|
||||||
try {
|
try {
|
||||||
yield take(types.METEOR.SUCCESS);
|
yield take(types.METEOR.SUCCESS);
|
||||||
yield call(getToken);
|
yield call(getToken);
|
||||||
// const { navigator } = yield select(state => state);
|
|
||||||
|
|
||||||
const user = yield select(getUser);
|
const user = yield select(getUser);
|
||||||
if (user.token) {
|
if (user.token) {
|
||||||
yield put(loginRequest({ resume: user.token }));
|
yield put(loginRequest({ resume: user.token }));
|
||||||
// console.log('AEEEEEEEEOOOOO');
|
|
||||||
// // wait for a response
|
|
||||||
// const { error } = yield race({
|
|
||||||
// success: take(types.LOGIN.SUCCESS),
|
|
||||||
// error: take(types.LOGIN.FAILURE)
|
|
||||||
// });
|
|
||||||
// console.log('AEEEEEEEEOOOOO', error);
|
|
||||||
// if (!error) {
|
|
||||||
// navigator.resetTo({
|
|
||||||
// screen: 'Rooms'
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
// navigator.resetTo({
|
|
||||||
// screen: 'Rooms'
|
|
||||||
// });
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
// } while (true);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const saveToken = function* saveToken() {
|
const saveToken = function* saveToken() {
|
||||||
|
@ -82,8 +67,20 @@ const saveToken = function* saveToken() {
|
||||||
|
|
||||||
const handleLoginRequest = function* handleLoginRequest({ credentials }) {
|
const handleLoginRequest = function* handleLoginRequest({ credentials }) {
|
||||||
try {
|
try {
|
||||||
const response = yield call(loginCall, credentials);
|
const server = yield select(getServer);
|
||||||
yield put(loginSuccess(response));
|
const user = yield call(loginCall, credentials);
|
||||||
|
|
||||||
|
// GET /me from REST API
|
||||||
|
const me = yield call(meCall, { server, token: user.token, userId: user.id });
|
||||||
|
|
||||||
|
// if user has username
|
||||||
|
if (me.username) {
|
||||||
|
user.username = me.username;
|
||||||
|
} else {
|
||||||
|
yield put(registerIncomplete());
|
||||||
|
}
|
||||||
|
|
||||||
|
yield put(loginSuccess(user));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.error === 403) {
|
if (err.error === 403) {
|
||||||
yield put(logout());
|
yield put(logout());
|
||||||
|
@ -98,13 +95,7 @@ const handleLoginSubmit = function* handleLoginSubmit({ credentials }) {
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleRegisterSubmit = function* handleRegisterSubmit({ credentials }) {
|
const handleRegisterSubmit = function* handleRegisterSubmit({ credentials }) {
|
||||||
// put a login request
|
|
||||||
yield put(registerRequest(credentials));
|
yield put(registerRequest(credentials));
|
||||||
// wait for a response
|
|
||||||
// yield race({
|
|
||||||
// success: take(types.LOGIN.REGISTER_SUCCESS),
|
|
||||||
// error: take(types.LOGIN.FAILURE)
|
|
||||||
// });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleRegisterRequest = function* handleRegisterRequest({ credentials }) {
|
const handleRegisterRequest = function* handleRegisterRequest({ credentials }) {
|
||||||
|
@ -141,6 +132,10 @@ const handleLogout = function* handleLogout() {
|
||||||
yield call(logoutCall, { server });
|
yield call(logoutCall, { server });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleRegisterIncomplete = function* handleRegisterIncomplete() {
|
||||||
|
yield call(NavigationService.navigate, 'Register');
|
||||||
|
};
|
||||||
|
|
||||||
const handleForgotPasswordRequest = function* handleForgotPasswordRequest({ email }) {
|
const handleForgotPasswordRequest = function* handleForgotPasswordRequest({ email }) {
|
||||||
try {
|
try {
|
||||||
yield call(forgotPasswordCall, email);
|
yield call(forgotPasswordCall, email);
|
||||||
|
@ -158,6 +153,7 @@ const root = function* root() {
|
||||||
yield takeLatest(types.LOGIN.REGISTER_REQUEST, handleRegisterRequest);
|
yield takeLatest(types.LOGIN.REGISTER_REQUEST, handleRegisterRequest);
|
||||||
yield takeLatest(types.LOGIN.REGISTER_SUBMIT, handleRegisterSubmit);
|
yield takeLatest(types.LOGIN.REGISTER_SUBMIT, handleRegisterSubmit);
|
||||||
yield takeLatest(types.LOGIN.REGISTER_SUCCESS, handleRegisterSuccess);
|
yield takeLatest(types.LOGIN.REGISTER_SUCCESS, handleRegisterSuccess);
|
||||||
|
yield takeLatest(types.LOGIN.REGISTER_INCOMPLETE, handleRegisterIncomplete);
|
||||||
yield takeLatest(types.LOGIN.SET_USERNAME_SUBMIT, handleSetUsernameSubmit);
|
yield takeLatest(types.LOGIN.SET_USERNAME_SUBMIT, handleSetUsernameSubmit);
|
||||||
yield takeLatest(types.LOGIN.SET_USERNAME_REQUEST, handleSetUsernameRequest);
|
yield takeLatest(types.LOGIN.SET_USERNAME_REQUEST, handleSetUsernameRequest);
|
||||||
yield takeLatest(types.LOGOUT, handleLogout);
|
yield takeLatest(types.LOGOUT, handleLogout);
|
||||||
|
|
Loading…
Reference in New Issue