Set/get auth from Redux

This commit is contained in:
Alex Gleason 2020-04-05 16:54:51 -05:00
parent 64cef8a56e
commit 3924a47ff7
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
4 changed files with 46 additions and 6 deletions

View file

@ -1,20 +1,24 @@
import api from '../api';
export function createApp() {
export const AUTH_APP_CREATED = 'AUTH_APP_CREATED';
export const AUTH_LOGGED_IN = 'AUTH_LOGGED_IN';
export function createAuthApp() {
return (dispatch, getState) => {
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',
scopes: 'read write follow push admin'
}).then(response => {
localStorage.setItem('app', JSON.stringify(response.data));
dispatch(authAppCreated(response.data));
});
}
}
export function logIn(username, password) {
return (dispatch, getState) => {
const app = JSON.parse(localStorage.getItem('app'));
const app = getState().getIn(['auth', 'app']);
api(getState).post('/oauth/token', {
client_id: app.client_id,
client_secret: app.client_secret,
@ -23,7 +27,21 @@ export function logIn(username, password) {
username: username,
password: password
}).then(response => {
localStorage.setItem('user', JSON.stringify(response.data));
dispatch(authLoggedIn(response.data));
});
}
}
export function authAppCreated(app) {
return {
type: AUTH_APP_CREATED,
app
};
}
export function authLoggedIn(user) {
return {
type: AUTH_LOGGED_IN,
user
};
}

View file

@ -1,7 +1,7 @@
import React from 'react';
import { connect } from 'react-redux'
import ImmutablePureComponent from 'react-immutable-pure-component';
import { createApp, logIn } from 'gabsocial/actions/auth';
import { createAuthApp, logIn } from 'gabsocial/actions/auth';
import { Redirect } from 'react-router-dom';
const mapStateToProps = (state, props) => ({
@ -11,7 +11,7 @@ const mapStateToProps = (state, props) => ({
class LoginForm extends ImmutablePureComponent {
componentWillMount() {
this.props.dispatch(createApp());
this.props.dispatch(createAuthApp());
}
getFormData = (form) => {

View file

@ -0,0 +1,20 @@
import { AUTH_APP_CREATED, AUTH_LOGGED_IN } from '../actions/auth';
import { Map as ImmutableMap, fromJS } from 'immutable';
const initialState = ImmutableMap({
app: JSON.parse(localStorage.getItem('app')),
user: JSON.parse(localStorage.getItem('user')),
});
export default function auth(state = initialState, action) {
switch(action.type) {
case AUTH_APP_CREATED:
localStorage.setItem('app', JSON.stringify(action.app)); // TODO: Better persistence
return state.set('app', ImmutableMap(action.app));
case AUTH_LOGGED_IN:
localStorage.setItem('user', JSON.stringify(action.user)); // TODO: Better persistence
return state.set('user', ImmutableMap(action.user));
default:
return state;
}
};

View file

@ -41,6 +41,7 @@ import patron from './patron';
import soapbox from './soapbox';
import instance from './instance';
import me from './me';
import auth from './auth';
const reducers = {
dropdown_menu,
@ -85,6 +86,7 @@ const reducers = {
soapbox,
instance,
me,
auth,
};
export default combineReducers(reducers);