mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-04-25 03:49:11 +00:00
feat(RolePicker): when not authed, redirect to /auth/login?r=id
Signed-off-by: Katalina Okano <git@kat.cafe>
This commit is contained in:
parent
765a0f2b22
commit
6d8f40e30e
1 changed files with 47 additions and 15 deletions
|
@ -1,5 +1,6 @@
|
||||||
import { NextPage, NextPageContext } from 'next';
|
import { NextPage, NextPageContext } from 'next';
|
||||||
import Head from 'next/head';
|
import Head from 'next/head';
|
||||||
|
import { useRouter } from 'next/router';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import {
|
import {
|
||||||
Member,
|
Member,
|
||||||
|
@ -10,12 +11,18 @@ import {
|
||||||
TransactionType,
|
TransactionType,
|
||||||
UserGuildPermissions,
|
UserGuildPermissions,
|
||||||
} from 'roleypoly/common/types';
|
} from 'roleypoly/common/types';
|
||||||
import { apiFetch } from 'roleypoly/common/utils/isomorphicFetch';
|
import { apiFetch, getSessionKey } from 'roleypoly/common/utils/isomorphicFetch';
|
||||||
import { RolePickerTemplate } from 'roleypoly/design-system/templates/role-picker';
|
import { RolePickerTemplate } from 'roleypoly/design-system/templates/role-picker';
|
||||||
import { useAppShellProps } from 'roleypoly/providers/appShellData';
|
import { useAppShellProps } from 'roleypoly/providers/appShellData';
|
||||||
|
|
||||||
type Props = {
|
type Props =
|
||||||
|
| {
|
||||||
data: PresentableGuild;
|
data: PresentableGuild;
|
||||||
|
redirect: null;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
data: null;
|
||||||
|
redirect: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const createUpdatePayload = (
|
const createUpdatePayload = (
|
||||||
|
@ -48,6 +55,18 @@ const createUpdatePayload = (
|
||||||
};
|
};
|
||||||
|
|
||||||
const RolePickerPage: NextPage<Props> = (props) => {
|
const RolePickerPage: NextPage<Props> = (props) => {
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (props.redirect !== null) {
|
||||||
|
void router.replace(props.redirect || '/auth/login');
|
||||||
|
}
|
||||||
|
}, [props.redirect]);
|
||||||
|
|
||||||
|
if (!props.data) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const { appShellProps } = useAppShellProps();
|
const { appShellProps } = useAppShellProps();
|
||||||
|
|
||||||
const [isPending, updatePending] = React.useState(false);
|
const [isPending, updatePending] = React.useState(false);
|
||||||
|
@ -108,19 +127,32 @@ RolePickerPage.getInitialProps = async (context: NextPageContext): Promise<Props
|
||||||
throw new Error('serverID missing');
|
throw new Error('serverID missing');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!getSessionKey()) {
|
||||||
|
return {
|
||||||
|
data: null,
|
||||||
|
redirect: `/auth/login?r=${serverID}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const pickerData = await apiFetch<PresentableGuild>(
|
const pickerData = await apiFetch<PresentableGuild>(
|
||||||
`/get-picker-data/${serverID}`,
|
`/get-picker-data/${serverID}`,
|
||||||
undefined,
|
undefined,
|
||||||
context
|
context
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!pickerData) {
|
if (pickerData) {
|
||||||
throw new Error('TODO: picker fetch failed');
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
data: pickerData,
|
data: pickerData,
|
||||||
|
redirect: null,
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return {
|
||||||
|
data: null,
|
||||||
|
redirect: `/auth/login?r=${serverID}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default RolePickerPage;
|
export default RolePickerPage;
|
||||||
|
|
Loading…
Add table
Reference in a new issue