diff --git a/app/gabsocial/actions/auth.js b/app/gabsocial/actions/auth.js index 401106b13..ff3b68c47 100644 --- a/app/gabsocial/actions/auth.js +++ b/app/gabsocial/actions/auth.js @@ -1,7 +1,8 @@ import api from '../api'; -export const AUTH_APP_CREATED = 'AUTH_APP_CREATED'; -export const AUTH_LOGGED_IN = 'AUTH_LOGGED_IN'; +export const AUTH_APP_CREATED = 'AUTH_APP_CREATED'; +export const AUTH_APP_AUTHORIZED = 'AUTH_APP_AUTHORIZED'; +export const AUTH_LOGGED_IN = 'AUTH_LOGGED_IN'; export function createAuthApp() { return (dispatch, getState) => { @@ -12,6 +13,16 @@ export function createAuthApp() { scopes: 'read write follow push admin' }).then(response => { dispatch(authAppCreated(response.data)); + }).then(() => { + const app = getState().getIn(['auth', 'app']); + return api(getState).post('/oauth/token', { + client_id: app.get('client_id'), + client_secret: app.get('client_secret'), + redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', + grant_type: 'client_credentials' + }); + }).then(response => { + dispatch(authAppAuthorized(response.data)); }); } } @@ -20,8 +31,8 @@ export function logIn(username, password) { return (dispatch, getState) => { const app = getState().getIn(['auth', 'app']); api(getState).post('/oauth/token', { - client_id: app.client_id, - client_secret: app.client_secret, + client_id: app.get('client_id'), + client_secret: app.get('client_secret'), redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', grant_type: 'password', username: username, @@ -39,6 +50,13 @@ export function authAppCreated(app) { }; } +export function authAppAuthorized(app) { + return { + type: AUTH_APP_AUTHORIZED, + app + }; +} + export function authLoggedIn(user) { return { type: AUTH_LOGGED_IN, diff --git a/app/gabsocial/api.js b/app/gabsocial/api.js index cae296c90..d7eaec283 100644 --- a/app/gabsocial/api.js +++ b/app/gabsocial/api.js @@ -26,8 +26,9 @@ function setCSRFHeader() { ready(setCSRFHeader); export default getState => { - // TODO: getState is no longer needed - const { access_token } = JSON.parse(localStorage.getItem('user')) || {}; + const user_token = getState().getIn(['auth', 'user', 'access_token']); + const app_token = getState().getIn(['auth', 'app', 'access_token']); + const access_token = user_token || app_token; return axios.create({ headers: Object.assign(csrfHeader, access_token ? { 'Authorization': `Bearer ${access_token}`, diff --git a/app/gabsocial/containers/gabsocial.js b/app/gabsocial/containers/gabsocial.js index 34f33c578..f6219840d 100644 --- a/app/gabsocial/containers/gabsocial.js +++ b/app/gabsocial/containers/gabsocial.js @@ -40,8 +40,7 @@ const mapStateToProps = (state) => { return { showIntroduction, me, - // accessToken: state.getIn(['auth', 'user', 'access_token']), - accessToken: JSON.parse(localStorage.getItem('user')).access_token, + accessToken: state.getIn(['auth', 'user', 'access_token']), streamingUrl: state.getIn(['instance', 'urls', 'streaming_api']), } } diff --git a/app/gabsocial/reducers/auth.js b/app/gabsocial/reducers/auth.js index fe49446e8..1738ce64d 100644 --- a/app/gabsocial/reducers/auth.js +++ b/app/gabsocial/reducers/auth.js @@ -1,18 +1,26 @@ -import { AUTH_APP_CREATED, AUTH_LOGGED_IN } from '../actions/auth'; +import { + AUTH_APP_CREATED, + AUTH_LOGGED_IN, + AUTH_APP_AUTHORIZED, +} 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')), + app: ImmutableMap(JSON.parse(localStorage.getItem('soapbox:auth:app'))), + user: ImmutableMap(JSON.parse(localStorage.getItem('soapbox:auth: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 + localStorage.setItem('soapbox:auth:app', JSON.stringify(action.app)); // TODO: Better persistence return state.set('app', ImmutableMap(action.app)); + case AUTH_APP_AUTHORIZED: + const merged = state.get('app').merge(ImmutableMap(action.app)); + localStorage.setItem('soapbox:auth:app', JSON.stringify(merged)); // TODO: Better persistence + return state.set('app', merged); case AUTH_LOGGED_IN: - localStorage.setItem('user', JSON.stringify(action.user)); // TODO: Better persistence + localStorage.setItem('soapbox:auth:user', JSON.stringify(action.user)); // TODO: Better persistence return state.set('user', ImmutableMap(action.user)); default: return state; diff --git a/app/gabsocial/stream.js b/app/gabsocial/stream.js index 698f4a285..fad857557 100644 --- a/app/gabsocial/stream.js +++ b/app/gabsocial/stream.js @@ -7,8 +7,7 @@ const randomIntUpTo = max => Math.floor(Math.random() * Math.floor(max)); export function connectStream(path, pollingRefresh = null, callbacks = () => ({ onConnect() {}, onDisconnect() {}, onReceive() {} })) { return (dispatch, getState) => { const streamingAPIBaseURL = getState().getIn(['instance', 'urls', 'streaming_api']); - // const accessToken: state.getIn(['auth', 'user', 'access_token']); - const accessToken = JSON.parse(localStorage.getItem('user')).access_token; + const accessToken = getState().getIn(['auth', 'user', 'access_token']); const { onConnect, onDisconnect, onReceive } = callbacks(dispatch, getState); let polling = null;