diff --git a/app/gabsocial/actions/auth.js b/app/gabsocial/actions/auth.js new file mode 100644 index 000000000..e8a71f75b --- /dev/null +++ b/app/gabsocial/actions/auth.js @@ -0,0 +1,29 @@ +import api from '../api'; + +export function createApp() { + return (dispatch, getState) => { + api(getState).post('/api/v1/apps', { + client_name: `SoapboxFE_${(new Date()).toISOString()}`, + redirect_uris: 'urn:ietf:wg:oauth:2.0:oob', + scopes: 'read write follow push admin' + }).then(response => { + localStorage.setItem('app', JSON.stringify(response.data)); + }); + } +} + +export function logIn(username, password) { + return (dispatch, getState) => { + const app = JSON.parse(localStorage.getItem('app')); + api(getState).post('/oauth/token', { + client_id: app.client_id, + client_secret: app.client_secret, + redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', + grant_type: 'password', + username: username, + password: password + }).then(response => { + localStorage.setItem('user', JSON.stringify(response.data)); + }); + } +} diff --git a/app/gabsocial/api.js b/app/gabsocial/api.js index 8898bdeba..cae296c90 100644 --- a/app/gabsocial/api.js +++ b/app/gabsocial/api.js @@ -25,16 +25,20 @@ function setCSRFHeader() { ready(setCSRFHeader); -export default getState => axios.create({ - headers: Object.assign(csrfHeader, getState ? { - 'Authorization': `Bearer ${getState().getIn(['meta', 'access_token'], '')}`, - } : {}), +export default getState => { + // TODO: getState is no longer needed + const { access_token } = JSON.parse(localStorage.getItem('user')) || {}; + return axios.create({ + headers: Object.assign(csrfHeader, access_token ? { + 'Authorization': `Bearer ${access_token}`, + } : {}), - transformResponse: [function (data) { - try { - return JSON.parse(data); - } catch(Exception) { - return data; - } - }], -}); + transformResponse: [function (data) { + try { + return JSON.parse(data); + } catch(Exception) { + return data; + } + }], + }); +}; diff --git a/app/gabsocial/features/auth_login/index.js b/app/gabsocial/features/auth_login/index.js index 55d65e8cd..ace46472f 100644 --- a/app/gabsocial/features/auth_login/index.js +++ b/app/gabsocial/features/auth_login/index.js @@ -1,21 +1,23 @@ import React from 'react'; +import { connect } from 'react-redux' import ImmutablePureComponent from 'react-immutable-pure-component'; +import { createApp, logIn } from 'gabsocial/actions/auth'; -export default class LoginForm extends ImmutablePureComponent { - constructor(props) { - super(props); - this.handleSubmit = this.handleSubmit.bind(this); +class LoginForm extends ImmutablePureComponent { + + componentWillMount() { + this.props.dispatch(createApp()); } - getFormData(form) { + getFormData = (form) => { return Object.fromEntries( Array.from(form).map(i => [i.name, i.value]) ); } - handleSubmit(event) { + handleSubmit = (event) => { const {username, password} = this.getFormData(event.target); - console.log(username + ' ' + password); + this.props.dispatch(logIn(username, password)); event.preventDefault(); } @@ -29,3 +31,5 @@ export default class LoginForm extends ImmutablePureComponent { ) } } + +export default connect()(LoginForm); diff --git a/webpack/development.js b/webpack/development.js index 4ba66b1fa..b714f1bb0 100644 --- a/webpack/development.js +++ b/webpack/development.js @@ -67,7 +67,7 @@ module.exports = merge(sharedConfig, { '/api': backendUrl, '/nodeinfo': backendUrl, '/socket': backendUrl, - '/oauth/revoke': backendUrl, + '/oauth': backendUrl, '/.well-known/webfinger': backendUrl, '/static': backendUrl, '/patron': patronUrl,