Set/get auth from Redux
This commit is contained in:
parent
64cef8a56e
commit
3924a47ff7
4 changed files with 46 additions and 6 deletions
|
@ -1,20 +1,24 @@
|
||||||
import api from '../api';
|
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) => {
|
return (dispatch, getState) => {
|
||||||
api(getState).post('/api/v1/apps', {
|
api(getState).post('/api/v1/apps', {
|
||||||
|
// TODO: Add commit hash to client_name
|
||||||
client_name: `SoapboxFE_${(new Date()).toISOString()}`,
|
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 => {
|
||||||
localStorage.setItem('app', JSON.stringify(response.data));
|
dispatch(authAppCreated(response.data));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function logIn(username, password) {
|
export function logIn(username, password) {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
const app = JSON.parse(localStorage.getItem('app'));
|
const app = getState().getIn(['auth', 'app']);
|
||||||
api(getState).post('/oauth/token', {
|
api(getState).post('/oauth/token', {
|
||||||
client_id: app.client_id,
|
client_id: app.client_id,
|
||||||
client_secret: app.client_secret,
|
client_secret: app.client_secret,
|
||||||
|
@ -23,7 +27,21 @@ export function logIn(username, password) {
|
||||||
username: username,
|
username: username,
|
||||||
password: password
|
password: password
|
||||||
}).then(response => {
|
}).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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
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 { createApp, logIn } from 'gabsocial/actions/auth';
|
import { createAuthApp, logIn } from 'gabsocial/actions/auth';
|
||||||
import { Redirect } from 'react-router-dom';
|
import { Redirect } from 'react-router-dom';
|
||||||
|
|
||||||
const mapStateToProps = (state, props) => ({
|
const mapStateToProps = (state, props) => ({
|
||||||
|
@ -11,7 +11,7 @@ const mapStateToProps = (state, props) => ({
|
||||||
class LoginForm extends ImmutablePureComponent {
|
class LoginForm extends ImmutablePureComponent {
|
||||||
|
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
this.props.dispatch(createApp());
|
this.props.dispatch(createAuthApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
getFormData = (form) => {
|
getFormData = (form) => {
|
||||||
|
|
20
app/gabsocial/reducers/auth.js
Normal file
20
app/gabsocial/reducers/auth.js
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
|
@ -41,6 +41,7 @@ import patron from './patron';
|
||||||
import soapbox from './soapbox';
|
import soapbox from './soapbox';
|
||||||
import instance from './instance';
|
import instance from './instance';
|
||||||
import me from './me';
|
import me from './me';
|
||||||
|
import auth from './auth';
|
||||||
|
|
||||||
const reducers = {
|
const reducers = {
|
||||||
dropdown_menu,
|
dropdown_menu,
|
||||||
|
@ -85,6 +86,7 @@ const reducers = {
|
||||||
soapbox,
|
soapbox,
|
||||||
instance,
|
instance,
|
||||||
me,
|
me,
|
||||||
|
auth,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default combineReducers(reducers);
|
export default combineReducers(reducers);
|
||||||
|
|
Loading…
Reference in a new issue