Authorize app with token
This commit is contained in:
parent
3924a47ff7
commit
83a711cd3e
5 changed files with 40 additions and 15 deletions
|
@ -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,
|
||||
|
|
|
@ -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}`,
|
||||
|
|
|
@ -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']),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue