diff --git a/app/gabsocial/actions/auth.js b/app/gabsocial/actions/auth.js index e8a71f75b..401106b13 100644 --- a/app/gabsocial/actions/auth.js +++ b/app/gabsocial/actions/auth.js @@ -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 + }; +} diff --git a/app/gabsocial/features/auth_login/index.js b/app/gabsocial/features/auth_login/index.js index 1fda6d890..ff6d11fba 100644 --- a/app/gabsocial/features/auth_login/index.js +++ b/app/gabsocial/features/auth_login/index.js @@ -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) => { diff --git a/app/gabsocial/reducers/auth.js b/app/gabsocial/reducers/auth.js new file mode 100644 index 000000000..fe49446e8 --- /dev/null +++ b/app/gabsocial/reducers/auth.js @@ -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; + } +}; diff --git a/app/gabsocial/reducers/index.js b/app/gabsocial/reducers/index.js index e7e6f10b7..c91f2f0cb 100644 --- a/app/gabsocial/reducers/index.js +++ b/app/gabsocial/reducers/index.js @@ -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);