Skip to content

Commit

Permalink
Log user out from UI on 401
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Dec 20, 2024
1 parent 4e8de68 commit 579fcd3
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 48 deletions.
6 changes: 0 additions & 6 deletions ui/src/actions/beamline.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import {
sendPrepareBeamlineForNewSample,
} from '../api/beamline';
import { sendLogFrontEndTraceBack } from '../api/log';
// The different states a beamline attribute can assume.
export const STATE = {
IDLE: 'READY',
BUSY: 'BUSY',
ABORT: 'UNUSABLE',
};

// Action types
export const BL_UPDATE_HARDWARE_OBJECT = 'BL_UPDATE_HARDWARE_OBJECT';
Expand Down
20 changes: 3 additions & 17 deletions ui/src/actions/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { fetchAvailableWorkflows } from '../api/workflow';
import { fetchAvailableTasks, fetchQueueState } from '../api/queue';

import { showErrorPanel, applicationFetched } from './general';
import { fetchLoginInfo, sendLogIn, sendSignOut } from '../api/login';
import { sendSignOut } from '../api/login';
import { fetchLoginInfo, sendLogIn } from '../api/loginBase';
import { fetchDetectorInfo } from '../api/detector';
import { fetchSampleChangerInitialState } from '../api/sampleChanger';
import { fetchHarvesterInitialState } from '../api/harvester';
Expand All @@ -22,21 +23,6 @@ export function setLoginInfo(loginInfo) {
};
}

export function resetLoginInfo() {
return setLoginInfo({
beamlineName: '',
synchrotronName: '',
loginType: '',
user: '',
proposalList: [],
selectedProposal: '',
selectedProposalID: '',
loggedIn: false,
rootPath: '',
useSSO: false,
});
}

export function showProposalsForm() {
return {
type: 'SHOW_PROPOSALS_FORM',
Expand Down Expand Up @@ -88,7 +74,7 @@ export function logIn(proposal, password) {

export function signOut() {
return async (dispatch) => {
dispatch(resetLoginInfo()); // disconnect sockets before actually logging out (cf. `App.jsx`)
dispatch(setLoginInfo({ loggedIn: false })); // disconnect sockets before actually logging out (cf. `App.jsx`)
dispatch(applicationFetched(false));

try {
Expand Down
16 changes: 16 additions & 0 deletions ui/src/api/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable promise/prefer-await-to-callbacks */
import baseApi from './apiBase';
import { fetchLoginInfo } from './loginBase';
import { store } from '../store';

const api = baseApi
.options({ credendials: 'include' })
.catcher(401, async (error) => {
// User got logged out somehow: refetch login info to update local state and redirect to login page.
// Don't use `getLoginInfo` action to avoid import cycle.
const loginInfo = await fetchLoginInfo();
store.dispatch({ type: 'SET_LOGIN_INFO', loginInfo });
throw error;
});

export default api;
5 changes: 2 additions & 3 deletions ui/src/api/index.js → ui/src/api/apiBase.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import wretch from 'wretch';
import safeJsonAddon from './addons/safeJson';

const api = wretch('/mxcube/api/v0.1')
const baseApi = wretch('/mxcube/api/v0.1')
.addon(safeJsonAddon())
.options({ credendials: 'include' })
.headers({ Accept: 'application/json' });

export default api;
export default baseApi;
2 changes: 1 addition & 1 deletion ui/src/api/beamline.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

const endpoint = api.url('/beamline');

Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/detector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

const endpoint = api.url('/detector');

Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/diffractometer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

const endpoint = api.url('/diffractometer');

Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/harvester.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

const endpoint = api.url('/harvester');

Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/lims.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

const endpoint = api.url('/lims');

Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/log.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

const endpoint = api.url('/log');

Expand Down
10 changes: 1 addition & 9 deletions ui/src/api/login.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import api from '.';
import api from './api';

const endpoint = api.url('/login');

export function sendLogIn(proposal, password, previousUser) {
return endpoint.post({ proposal, password, previousUser }, '/').safeJson();
}

export function sendSignOut() {
return endpoint.headers({ Accept: '*/*' }).get('/signout').res();
}

export function fetchLoginInfo() {
return endpoint.get('/login_info').safeJson();
}

export function sendFeedback(sender, content) {
return endpoint.post({ sender, content }, '/send_feedback').res();
}
Expand Down
15 changes: 15 additions & 0 deletions ui/src/api/loginBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Unauthenticated `/login/*` endpoints.
* Separate from authenticated endpoints to avoid import cycle with `api.js`.
*/
import baseApi from './apiBase';

const endpoint = baseApi.url('/login');

export function sendLogIn(proposal, password, previousUser) {
return endpoint.post({ proposal, password, previousUser }, '/').safeJson();
}

export function fetchLoginInfo() {
return endpoint.get('/login_info').safeJson();
}
2 changes: 1 addition & 1 deletion ui/src/api/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

export function fetchUIProperties() {
return api.get('/uiproperties').safeJson();
Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/queue.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

const endpoint = api.url('/queue');

Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/remoteAccess.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

const endpoint = api.url('/ra');

Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/sampleChanger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

const endpoint = api.url('/sample_changer');

Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/sampleview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

const endpoint = api.url('/sampleview');

Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/workflow.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api from '.';
import api from './api';

const endpoint = api.url('/workflow');

Expand Down
8 changes: 7 additions & 1 deletion ui/src/reducers/beamline.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* eslint-disable sonarjs/no-duplicate-string */
import { STATE } from '../actions/beamline';
import { RUNNING, HW_STATE } from '../constants';

// The different states a beamline attribute can assume.
export const STATE = {
IDLE: 'READY',
BUSY: 'BUSY',
ABORT: 'UNUSABLE',
};

/**
* Initial redux state for beamline hardwareObjects, object containing each beamline
* attribute (name, attribute object). Each attribute object in turn have the
Expand Down

0 comments on commit 579fcd3

Please sign in to comment.