Get basic login working
This commit is contained in:
parent
86e9c711a7
commit
8f07c6ea2a
|
@ -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));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,16 +25,20 @@ function setCSRFHeader() {
|
||||||
|
|
||||||
ready(setCSRFHeader);
|
ready(setCSRFHeader);
|
||||||
|
|
||||||
export default getState => axios.create({
|
export default getState => {
|
||||||
headers: Object.assign(csrfHeader, getState ? {
|
// TODO: getState is no longer needed
|
||||||
'Authorization': `Bearer ${getState().getIn(['meta', 'access_token'], '')}`,
|
const { access_token } = JSON.parse(localStorage.getItem('user')) || {};
|
||||||
} : {}),
|
return axios.create({
|
||||||
|
headers: Object.assign(csrfHeader, access_token ? {
|
||||||
|
'Authorization': `Bearer ${access_token}`,
|
||||||
|
} : {}),
|
||||||
|
|
||||||
transformResponse: [function (data) {
|
transformResponse: [function (data) {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(data);
|
return JSON.parse(data);
|
||||||
} catch(Exception) {
|
} catch(Exception) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
|
@ -1,21 +1,23 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { connect } from 'react-redux'
|
||||||
import ImmutablePureComponent from 'react-immutable-pure-component';
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
||||||
|
import { createApp, logIn } from 'gabsocial/actions/auth';
|
||||||
|
|
||||||
export default class LoginForm extends ImmutablePureComponent {
|
class LoginForm extends ImmutablePureComponent {
|
||||||
constructor(props) {
|
|
||||||
super(props);
|
componentWillMount() {
|
||||||
this.handleSubmit = this.handleSubmit.bind(this);
|
this.props.dispatch(createApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
getFormData(form) {
|
getFormData = (form) => {
|
||||||
return Object.fromEntries(
|
return Object.fromEntries(
|
||||||
Array.from(form).map(i => [i.name, i.value])
|
Array.from(form).map(i => [i.name, i.value])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSubmit(event) {
|
handleSubmit = (event) => {
|
||||||
const {username, password} = this.getFormData(event.target);
|
const {username, password} = this.getFormData(event.target);
|
||||||
console.log(username + ' ' + password);
|
this.props.dispatch(logIn(username, password));
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,3 +31,5 @@ export default class LoginForm extends ImmutablePureComponent {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default connect()(LoginForm);
|
||||||
|
|
|
@ -67,7 +67,7 @@ module.exports = merge(sharedConfig, {
|
||||||
'/api': backendUrl,
|
'/api': backendUrl,
|
||||||
'/nodeinfo': backendUrl,
|
'/nodeinfo': backendUrl,
|
||||||
'/socket': backendUrl,
|
'/socket': backendUrl,
|
||||||
'/oauth/revoke': backendUrl,
|
'/oauth': backendUrl,
|
||||||
'/.well-known/webfinger': backendUrl,
|
'/.well-known/webfinger': backendUrl,
|
||||||
'/static': backendUrl,
|
'/static': backendUrl,
|
||||||
'/patron': patronUrl,
|
'/patron': patronUrl,
|
||||||
|
|
Loading…
Reference in New Issue