Get basic login working
This commit is contained in:
parent
86e9c711a7
commit
8f07c6ea2a
4 changed files with 57 additions and 20 deletions
29
app/gabsocial/actions/auth.js
Normal file
29
app/gabsocial/actions/auth.js
Normal file
|
@ -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);
|
||||
|
||||
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;
|
||||
}
|
||||
}],
|
||||
});
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue