2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-17 12:35:46 +00:00

fix auth for email actions in MFA scenarios

This commit is contained in:
Matthias Mair
2024-12-28 17:57:13 +01:00
parent a584334a24
commit ef14310fc4
2 changed files with 68 additions and 45 deletions

View File

@ -201,7 +201,7 @@ export function handleMfaLogin(
location: Location<any>, location: Location<any>,
values: { code: string } values: { code: string }
) { ) {
const { session, setToken } = useUserState.getState(); const { session, setToken, setSession } = useUserState.getState();
api api
.post( .post(
@ -212,6 +212,7 @@ export function handleMfaLogin(
{ headers: { 'X-Session-Token': session } } { headers: { 'X-Session-Token': session } }
) )
.then((response) => { .then((response) => {
setSession(response.data.meta.session_token);
setToken(response.data.meta.access_token); setToken(response.data.meta.access_token);
followRedirect(navigate, location?.state); followRedirect(navigate, location?.state);
}); });

View File

@ -99,11 +99,15 @@ export function SecurityContent() {
function EmailContent() { function EmailContent() {
const [value, setValue] = useState<string>(''); const [value, setValue] = useState<string>('');
const [newEmailValue, setNewEmailValue] = useState(''); const [newEmailValue, setNewEmailValue] = useState('');
const [user] = useUserState((state) => [state.user]); const [session] = useUserState((state) => [state.session]);
const { isLoading, data, refetch } = useQuery({ const { isLoading, data, refetch } = useQuery({
queryKey: ['emails'], queryKey: ['emails'],
queryFn: () => queryFn: () =>
api.get(apiUrl(ApiEndpoints.user_emails)).then((res) => res.data) api
.get(apiUrl(ApiEndpoints.user_emails), {
headers: { 'X-Session-Token': session }
})
.then((res) => res.data.data)
}); });
function runServerAction( function runServerAction(
@ -122,7 +126,11 @@ function EmailContent() {
act = api.delete; act = api.delete;
break; break;
} }
act(apiUrl(url), { email: value }) act(
apiUrl(url),
{ email: value },
{ headers: { 'X-Session-Token': session } }
)
.then(() => { .then(() => {
refetch(); refetch();
}) })
@ -131,9 +139,13 @@ function EmailContent() {
function addEmail() { function addEmail() {
api api
.post(apiUrl(ApiEndpoints.user_emails), { .post(
apiUrl(ApiEndpoints.user_emails),
{
email: newEmailValue email: newEmailValue
}) },
{ headers: { 'X-Session-Token': session } }
)
.then(() => { .then(() => {
refetch(); refetch();
}) })
@ -142,10 +154,14 @@ function EmailContent() {
function changePrimary() { function changePrimary() {
api api
.post(apiUrl(ApiEndpoints.user_emails), { .post(
apiUrl(ApiEndpoints.user_emails),
{
email: value, email: value,
primary: true primary: true
}) },
{ headers: { 'X-Session-Token': session } }
)
.then(() => { .then(() => {
refetch(); refetch();
}) })
@ -157,6 +173,11 @@ function EmailContent() {
return ( return (
<Grid> <Grid>
<Grid.Col span={6}> <Grid.Col span={6}>
{data.length == 0 ? (
<Text>
<Trans>Currently no emails are registered</Trans>
</Text>
) : (
<Radio.Group <Radio.Group
value={value} value={value}
onChange={setValue} onChange={setValue}
@ -166,8 +187,8 @@ function EmailContent() {
<Stack mt='xs'> <Stack mt='xs'>
{data.map((email: any) => ( {data.map((email: any) => (
<Radio <Radio
key={email.id} key={email.email}
value={String(email.id)} value={String(email.email)}
label={ label={
<Group justify='space-between'> <Group justify='space-between'>
{email.email} {email.email}
@ -191,6 +212,7 @@ function EmailContent() {
))} ))}
</Stack> </Stack>
</Radio.Group> </Radio.Group>
)}
</Grid.Col> </Grid.Col>
<Grid.Col span={6}> <Grid.Col span={6}>
<Stack> <Stack>