Authorize app with token
This commit is contained in:
parent
3924a47ff7
commit
83a711cd3e
5 changed files with 40 additions and 15 deletions
|
@ -1,6 +1,7 @@
|
||||||
import api from '../api';
|
import api from '../api';
|
||||||
|
|
||||||
export const AUTH_APP_CREATED = 'AUTH_APP_CREATED';
|
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 const AUTH_LOGGED_IN = 'AUTH_LOGGED_IN';
|
||||||
|
|
||||||
export function createAuthApp() {
|
export function createAuthApp() {
|
||||||
|
@ -12,6 +13,16 @@ export function createAuthApp() {
|
||||||
scopes: 'read write follow push admin'
|
scopes: 'read write follow push admin'
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
dispatch(authAppCreated(response.data));
|
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) => {
|
return (dispatch, getState) => {
|
||||||
const app = getState().getIn(['auth', '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.get('client_id'),
|
||||||
client_secret: app.client_secret,
|
client_secret: app.get('client_secret'),
|
||||||
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
||||||
grant_type: 'password',
|
grant_type: 'password',
|
||||||
username: username,
|
username: username,
|
||||||
|
@ -39,6 +50,13 @@ export function authAppCreated(app) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function authAppAuthorized(app) {
|
||||||
|
return {
|
||||||
|
type: AUTH_APP_AUTHORIZED,
|
||||||
|
app
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function authLoggedIn(user) {
|
export function authLoggedIn(user) {
|
||||||
return {
|
return {
|
||||||
type: AUTH_LOGGED_IN,
|
type: AUTH_LOGGED_IN,
|
||||||
|
|
|
@ -26,8 +26,9 @@ function setCSRFHeader() {
|
||||||
ready(setCSRFHeader);
|
ready(setCSRFHeader);
|
||||||
|
|
||||||
export default getState => {
|
export default getState => {
|
||||||
// TODO: getState is no longer needed
|
const user_token = getState().getIn(['auth', 'user', 'access_token']);
|
||||||
const { access_token } = JSON.parse(localStorage.getItem('user')) || {};
|
const app_token = getState().getIn(['auth', 'app', 'access_token']);
|
||||||
|
const access_token = user_token || app_token;
|
||||||
return axios.create({
|
return axios.create({
|
||||||
headers: Object.assign(csrfHeader, access_token ? {
|
headers: Object.assign(csrfHeader, access_token ? {
|
||||||
'Authorization': `Bearer ${access_token}`,
|
'Authorization': `Bearer ${access_token}`,
|
||||||
|
|
|
@ -40,8 +40,7 @@ const mapStateToProps = (state) => {
|
||||||
return {
|
return {
|
||||||
showIntroduction,
|
showIntroduction,
|
||||||
me,
|
me,
|
||||||
// accessToken: state.getIn(['auth', 'user', 'access_token']),
|
accessToken: state.getIn(['auth', 'user', 'access_token']),
|
||||||
accessToken: JSON.parse(localStorage.getItem('user')).access_token,
|
|
||||||
streamingUrl: state.getIn(['instance', 'urls', 'streaming_api']),
|
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';
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
||||||
|
|
||||||
const initialState = ImmutableMap({
|
const initialState = ImmutableMap({
|
||||||
app: JSON.parse(localStorage.getItem('app')),
|
app: ImmutableMap(JSON.parse(localStorage.getItem('soapbox:auth:app'))),
|
||||||
user: JSON.parse(localStorage.getItem('user')),
|
user: ImmutableMap(JSON.parse(localStorage.getItem('soapbox:auth:user'))),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default function auth(state = initialState, action) {
|
export default function auth(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case AUTH_APP_CREATED:
|
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));
|
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:
|
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));
|
return state.set('user', ImmutableMap(action.user));
|
||||||
default:
|
default:
|
||||||
return state;
|
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() {} })) {
|
export function connectStream(path, pollingRefresh = null, callbacks = () => ({ onConnect() {}, onDisconnect() {}, onReceive() {} })) {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
const streamingAPIBaseURL = getState().getIn(['instance', 'urls', 'streaming_api']);
|
const streamingAPIBaseURL = getState().getIn(['instance', 'urls', 'streaming_api']);
|
||||||
// const accessToken: state.getIn(['auth', 'user', 'access_token']);
|
const accessToken = getState().getIn(['auth', 'user', 'access_token']);
|
||||||
const accessToken = JSON.parse(localStorage.getItem('user')).access_token;
|
|
||||||
const { onConnect, onDisconnect, onReceive } = callbacks(dispatch, getState);
|
const { onConnect, onDisconnect, onReceive } = callbacks(dispatch, getState);
|
||||||
|
|
||||||
let polling = null;
|
let polling = null;
|
||||||
|
|
Loading…
Reference in a new issue