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