user-activation.page.tsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { NextPage, GetServerSideProps, GetServerSidePropsContext } from 'next';
  2. import { useTranslation } from 'next-i18next';
  3. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  4. import Head from 'next/head';
  5. import CompleteUserRegistrationForm from '~/components/CompleteUserRegistrationForm';
  6. import { NoLoginLayout } from '~/components/Layout/NoLoginLayout';
  7. import type { CrowiRequest } from '~/interfaces/crowi-request';
  8. import type { UserActivationErrorCode } from '~/interfaces/errors/user-activation';
  9. import type { RegistrationMode } from '~/interfaces/registration-mode';
  10. import { IUserRegistrationOrder } from '~/server/models/user-registration-order';
  11. import {
  12. getServerSideCommonProps, getNextI18NextConfig, generateCustomTitle, CommonProps,
  13. } from './utils/commons';
  14. type Props = CommonProps & {
  15. token: string
  16. email: string
  17. errorCode?: UserActivationErrorCode
  18. registrationMode: RegistrationMode
  19. isEmailAuthenticationEnabled: boolean
  20. }
  21. const UserActivationPage: NextPage<Props> = (props: Props) => {
  22. const { t } = useTranslation();
  23. const title = generateCustomTitle(props, t('User Activation'));
  24. return (
  25. <NoLoginLayout>
  26. <Head>
  27. <title>{title}</title>
  28. </Head>
  29. <CompleteUserRegistrationForm
  30. token={props.token}
  31. email={props.email}
  32. errorCode={props.errorCode}
  33. registrationMode={props.registrationMode}
  34. isEmailAuthenticationEnabled={props.isEmailAuthenticationEnabled}
  35. />
  36. </NoLoginLayout>
  37. );
  38. };
  39. /**
  40. * for Server Side Translations
  41. * @param context
  42. * @param props
  43. * @param namespacesRequired
  44. */
  45. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  46. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  47. props._nextI18Next = nextI18NextConfig._nextI18Next;
  48. }
  49. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  50. const result = await getServerSideCommonProps(context);
  51. const req: CrowiRequest = context.req as CrowiRequest;
  52. // check for presence
  53. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  54. if (!('props' in result)) {
  55. throw new Error('invalid getSSP result');
  56. }
  57. const props: Props = result.props as Props;
  58. if (context.query.userRegistrationOrder != null) {
  59. const userRegistrationOrder = context.query.userRegistrationOrder as unknown as IUserRegistrationOrder;
  60. props.email = userRegistrationOrder.email;
  61. props.token = userRegistrationOrder.token;
  62. }
  63. if (typeof context.query.errorCode === 'string') {
  64. props.errorCode = context.query.errorCode as UserActivationErrorCode;
  65. }
  66. props.registrationMode = req.crowi.configManager.getConfig('crowi', 'security:registrationMode');
  67. props.isEmailAuthenticationEnabled = req.crowi.configManager.getConfig('crowi', 'security:passport-local:isEmailAuthenticationEnabled');
  68. await injectNextI18NextConfigurations(context, props, ['translation']);
  69. return {
  70. props,
  71. };
  72. };
  73. export default UserActivationPage;