Refactor auth app creation
This commit is contained in:
parent
238eed4c1a
commit
652c79b6ac
|
@ -1,5 +1,5 @@
|
||||||
import api from '../api';
|
import api from '../api';
|
||||||
import { showAlert } from 'gabsocial/actions/alerts';
|
import { showAlert, showAlertForError } from 'gabsocial/actions/alerts';
|
||||||
import { fetchMe } from 'gabsocial/actions/me';
|
import { fetchMe } from 'gabsocial/actions/me';
|
||||||
|
|
||||||
export const AUTH_APP_CREATED = 'AUTH_APP_CREATED';
|
export const AUTH_APP_CREATED = 'AUTH_APP_CREATED';
|
||||||
|
@ -11,25 +11,39 @@ export const AUTH_REGISTER_REQUEST = 'AUTH_REGISTER_REQUEST';
|
||||||
export const AUTH_REGISTER_SUCCESS = 'AUTH_REGISTER_SUCCESS';
|
export const AUTH_REGISTER_SUCCESS = 'AUTH_REGISTER_SUCCESS';
|
||||||
export const AUTH_REGISTER_FAIL = 'AUTH_REGISTER_FAIL';
|
export const AUTH_REGISTER_FAIL = 'AUTH_REGISTER_FAIL';
|
||||||
|
|
||||||
|
const hasAuthApp = getState => getState().hasIn(['auth', 'app', 'access_token']);
|
||||||
|
|
||||||
|
export function initAuthApp() {
|
||||||
|
return (dispatch, getState) => {
|
||||||
|
if (!hasAuthApp(getState)) dispatch(createAuthApp()).then(() => {
|
||||||
|
dispatch(createAuthAppToken());
|
||||||
|
}).catch(error => {
|
||||||
|
dispatch(showAlertForError(error));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function createAuthApp() {
|
export function createAuthApp() {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
const appToken = getState().getIn(['auth', 'app', 'access_token']);
|
return api(getState, 'app').post('/api/v1/apps', {
|
||||||
if (appToken) return new Promise(_ => _()); // Skip for now, FIXME: call verify_credentials
|
client_name: `SoapboxFE_${(new Date()).toISOString()}`, // TODO: Add commit hash to client_name
|
||||||
return api(getState).post('/api/v1/apps', {
|
|
||||||
// TODO: Add commit hash to client_name
|
|
||||||
client_name: `SoapboxFE_${(new Date()).toISOString()}`,
|
|
||||||
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));
|
dispatch(authAppCreated(response.data));
|
||||||
}).then(() => {
|
});
|
||||||
const app = getState().getIn(['auth', 'app']);
|
};
|
||||||
return api(getState).post('/oauth/token', {
|
}
|
||||||
client_id: app.get('client_id'),
|
|
||||||
client_secret: app.get('client_secret'),
|
export function createAuthAppToken() {
|
||||||
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
return (dispatch, getState) => {
|
||||||
grant_type: 'client_credentials',
|
const app = getState().getIn(['auth', 'app']);
|
||||||
});
|
|
||||||
|
return api(getState).post('/oauth/token', {
|
||||||
|
client_id: app.get('client_id'),
|
||||||
|
client_secret: app.get('client_secret'),
|
||||||
|
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
||||||
|
grant_type: 'client_credentials',
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
dispatch(authAppAuthorized(response.data));
|
dispatch(authAppAuthorized(response.data));
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
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 { createAuthApp, logIn } from 'gabsocial/actions/auth';
|
import { initAuthApp, logIn } from 'gabsocial/actions/auth';
|
||||||
import { fetchMe } from 'gabsocial/actions/me';
|
import { fetchMe } from 'gabsocial/actions/me';
|
||||||
// import { Link } from 'react-router-dom';
|
|
||||||
|
|
||||||
export default @connect()
|
export default @connect()
|
||||||
class LoginForm extends ImmutablePureComponent {
|
class LoginForm extends ImmutablePureComponent {
|
||||||
|
@ -14,7 +13,7 @@ class LoginForm extends ImmutablePureComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
this.props.dispatch(createAuthApp());
|
this.props.dispatch(initAuthApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
getFormData = (form) => {
|
getFormData = (form) => {
|
||||||
|
|
Loading…
Reference in New Issue