Commit 59c8baff authored by Marjolaine Bodin's avatar Marjolaine Bodin
Browse files

#522 login with different affiliation

parent efe9e1e3
Pipeline #53246 passed with stage
in 9 minutes and 14 seconds
......@@ -18,7 +18,11 @@ export function doSignIn(plugin, username, password) {
dispatch({ type: LOG_IN, username });
axios
.post(`${ICATPLUS.server}/session`, { plugin, username, password })
.post(`${ICATPLUS.server}/session`, {
plugin,
username,
password,
})
.then(({ data }) => {
if (!data || !data.sessionId) {
throw new Error('Invalid authentication response');
......@@ -51,13 +55,14 @@ export function doSignIn(plugin, username, password) {
};
}
export function doSilentRefreshFromSSO() {
export function doSilentRefreshFromSSO(affiliation) {
return (dispatch) => {
axios
.post(`${ICATPLUS.server}/session`, {
plugin: ICAT.authentication.sso.plugin,
username: null,
password: keycloak.idToken,
smisPk: affiliation,
})
.then(({ data }) => {
if (!data || !data.sessionId) {
......@@ -90,44 +95,3 @@ export function doSilentRefreshFromSSO() {
});
};
}
export function switchAffiliation(name) {
return (dispatch) => {
axios
.post(`${ICATPLUS.server}/session`, {
plugin: ICAT.authentication.sso.plugin,
username: null,
password: keycloak.idToken,
smisPk: name.split('-')[1],
})
.then(({ data }) => {
if (!data || !data.sessionId) {
throw new Error('Invalid switch authentication response');
}
const {
sessionId,
name,
fullName,
isAdministrator,
isInstrumentScientist,
lifeTimeMinutes,
affiliations,
} = data;
dispatch({
type: LOGGED_IN,
sessionId,
name,
fullName,
isAdministrator,
isInstrumentScientist,
lifeTimeMinutes,
affiliations,
});
})
.catch((error) => {
console.error(error);
dispatch({ type: LOGIN_ERROR, error: 'Switch authentication failed' });
});
};
}
import React from 'react';
import { MenuItem, NavDropdown } from 'react-bootstrap';
import { useSelector } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import { dateFormatter } from '../Investigation/utils';
import { switchAffiliation } from '../../actions/login';
import { doSilentRefreshFromSSO } from '../../actions/login';
function AffiliationMenu() {
const { affiliations } = useSelector((state) => state.user);
const dispatch = useDispatch();
function getAffiliationName(createTime) {
return dateFormatter(createTime, 'DD/MM/YYYY', false);
}
function handleAffiliation(name) {
switchAffiliation(name);
dispatch(doSilentRefreshFromSSO(name.split('-')[1]));
}
return (
......@@ -24,7 +25,7 @@ function AffiliationMenu() {
id="basic-nav-dropdown"
>
{affiliations.map(({ name, createTime }) => (
<MenuItem eventKey={name} onClick={handleAffiliation(name)}>
<MenuItem eventKey={name} onClick={() => handleAffiliation(name)}>
{getAffiliationName(createTime)}
</MenuItem>
))}
......
......@@ -19,6 +19,7 @@ const initialState = {
isInstrumentScientist: false,
isAuthenticating: false,
isSessionExpired: false,
affiliations: [],
error: null,
};
......@@ -49,6 +50,7 @@ const user = (state = initialState, action) => {
state.username === ICAT.authentication.anonymous.username,
isAdministrator: action.isAdministrator,
isInstrumentScientist: action.isInstrumentScientist,
affiliations: action.affiliations,
};
break;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment