admin-page-util.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import type { IUserHasId } from '@growi/core';
  2. import { GetServerSidePropsContext } from 'next';
  3. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  4. import type { CrowiRequest } from '~/interfaces/crowi-request';
  5. import {
  6. getServerSideCommonProps, getNextI18NextConfig, CommonProps,
  7. } from '~/pages/utils/commons';
  8. /**
  9. * for Server Side Translations
  10. * @param context
  11. * @param props
  12. * @param namespacesRequired
  13. */
  14. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props, namespacesRequired?: string[] | undefined): Promise<void> {
  15. // preload all languages because of language lists in user setting
  16. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired, true);
  17. props._nextI18Next = nextI18NextConfig._nextI18Next;
  18. }
  19. export const retrieveServerSideProps: any = async(
  20. context: GetServerSidePropsContext, injectServerConfigurations?:(context: GetServerSidePropsContext, props) => Promise<void>,
  21. ) => {
  22. const req = context.req as CrowiRequest;
  23. const { user } = req;
  24. const result = await getServerSideCommonProps(context);
  25. // check for presence
  26. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  27. if (!('props' in result)) {
  28. throw new Error('invalid getSSP result');
  29. }
  30. const props: CommonProps = result.props as CommonProps;
  31. if (injectServerConfigurations != null) {
  32. await injectServerConfigurations(context, props);
  33. }
  34. if (user != null) {
  35. props.currentUser = user.toObject();
  36. }
  37. props.isAccessDeniedForNonAdminUser = props.currentUser == null
  38. ? true
  39. : !props.currentUser.admin;
  40. await injectNextI18NextConfigurations(context, props, ['admin', 'commons']);
  41. return {
  42. props,
  43. };
  44. };