Delay creating auth app until trying to login or register
This commit is contained in:
parent
3c159460eb
commit
5c0c1932e0
3 changed files with 24 additions and 18 deletions
|
@ -14,7 +14,7 @@ export const AUTH_REGISTER_FAIL = 'AUTH_REGISTER_FAIL';
|
|||
const hasAppToken = getState => getState().hasIn(['auth', 'app', 'access_token']);
|
||||
const noOp = () => () => new Promise(f => f());
|
||||
|
||||
export function initAuthApp() {
|
||||
function initAuthApp() {
|
||||
return (dispatch, getState) => {
|
||||
const hasToken = hasAppToken(getState);
|
||||
const action = hasToken ? noOp : createAppAndToken;
|
||||
|
@ -28,7 +28,7 @@ export function initAuthApp() {
|
|||
function createAppAndToken() {
|
||||
return (dispatch, getState) => {
|
||||
return dispatch(createApp()).then(() => {
|
||||
dispatch(createAppToken());
|
||||
return dispatch(createAppToken());
|
||||
});
|
||||
};
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ function createApp() {
|
|||
redirect_uris: 'urn:ietf:wg:oauth:2.0:oob',
|
||||
scopes: 'read write follow push admin',
|
||||
}).then(response => {
|
||||
dispatch(authAppCreated(response.data));
|
||||
return dispatch(authAppCreated(response.data));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
@ -60,12 +60,12 @@ function createAppToken() {
|
|||
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
||||
grant_type: 'client_credentials',
|
||||
}).then(response => {
|
||||
dispatch(authAppAuthorized(response.data));
|
||||
return dispatch(authAppAuthorized(response.data));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function logIn(username, password) {
|
||||
function createUserToken(username, password) {
|
||||
return (dispatch, getState) => {
|
||||
const app = getState().getIn(['auth', 'app']);
|
||||
return api(getState, 'app').post('/oauth/token', {
|
||||
|
@ -75,9 +75,17 @@ export function logIn(username, password) {
|
|||
grant_type: 'password',
|
||||
username: username,
|
||||
password: password,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function logIn(username, password) {
|
||||
return (dispatch, getState) => {
|
||||
return dispatch(initAuthApp()).then(() => {
|
||||
return dispatch(createUserToken(username, password));
|
||||
}).then(response => {
|
||||
dispatch(authLoggedIn(response.data));
|
||||
}).catch((error) => {
|
||||
return dispatch(authLoggedIn(response.data));
|
||||
}).catch(error => {
|
||||
dispatch(showAlert('Login failed.', 'Invalid username or password.'));
|
||||
throw error;
|
||||
});
|
||||
|
@ -94,10 +102,12 @@ export function logOut() {
|
|||
export function register(params) {
|
||||
return (dispatch, getState) => {
|
||||
dispatch({ type: AUTH_REGISTER_REQUEST });
|
||||
return api(getState, 'app').post('/api/v1/accounts', params).then(response => {
|
||||
return dispatch(initAuthApp()).then(() => {
|
||||
return api(getState, 'app').post('/api/v1/accounts', params);
|
||||
}).then(response => {
|
||||
dispatch({ type: AUTH_REGISTER_SUCCESS, token: response.data });
|
||||
dispatch(authLoggedIn(response.data));
|
||||
dispatch(fetchMe());
|
||||
return dispatch(fetchMe());
|
||||
}).catch(error => {
|
||||
dispatch({ type: AUTH_REGISTER_FAIL, error });
|
||||
});
|
||||
|
|
|
@ -1,19 +1,14 @@
|
|||
import React from 'react';
|
||||
import { connect } from 'react-redux';
|
||||
import ImmutablePureComponent from 'react-immutable-pure-component';
|
||||
import { initAuthApp, logIn } from 'gabsocial/actions/auth';
|
||||
import { logIn } from 'gabsocial/actions/auth';
|
||||
import { fetchMe } from 'gabsocial/actions/me';
|
||||
|
||||
export default @connect()
|
||||
class LoginForm extends ImmutablePureComponent {
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = { isLoading: false };
|
||||
}
|
||||
|
||||
componentWillMount() {
|
||||
this.props.dispatch(initAuthApp());
|
||||
state = {
|
||||
isLoading: false,
|
||||
}
|
||||
|
||||
getFormData = (form) => {
|
||||
|
@ -27,7 +22,7 @@ class LoginForm extends ImmutablePureComponent {
|
|||
const { username, password } = this.getFormData(event.target);
|
||||
dispatch(logIn(username, password)).then(() => {
|
||||
return dispatch(fetchMe());
|
||||
}).catch((error) => {
|
||||
}).catch(error => {
|
||||
this.setState({ isLoading: false });
|
||||
});
|
||||
this.setState({ isLoading: true });
|
||||
|
|
|
@ -43,6 +43,7 @@ class RegistrationForm extends ImmutablePureComponent {
|
|||
}
|
||||
|
||||
onSubmit = e => {
|
||||
this.setState({ submissionLoading: true });
|
||||
this.props.dispatch(register(this.state.params.toJS()));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue