Skip to content

Commit

Permalink
fix: make loginMethods configurable via environment variables
Browse files Browse the repository at this point in the history
refs: LINK-2245
  • Loading branch information
voneiden committed Mar 5, 2025
1 parent e69f0b8 commit 1701fbe
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ NEXT_PUBLIC_MATOMO_ENABLED=false
NEXT_PUBLIC_LINKED_EVENTS_URL=https://linkedevents-api.dev.hel.ninja/linkedevents-dev/v1
NEXT_PUBLIC_WEB_STORE_INTEGRATION_ENABLED=false
NEXT_PUBLIC_WEB_STORE_API_BASE_URL=https://checkout-test-api.test.hel.ninja/v1

NEXT_PUBLIC_ATTENDANCE_LIST_LOGIN_METHODS=suomi_fi
NEXT_PUBLIC_SIGNUPS_LOGIN_METHODS=helsinki_tunnus,helsinkiad
3 changes: 3 additions & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ NEXT_PUBLIC_WEB_STORE_INTEGRATION_ENABLED=true
NEXT_PUBLIC_WEB_STORE_API_BASE_URL=https://checkout-test-api.test.hel.ninja/v1

E2E_TESTS_ENV_URL=http://localhost:3001/

NEXT_PUBLIC_ATTENDANCE_LIST_LOGIN_METHODS=suomi_fi
NEXT_PUBLIC_SIGNUPS_LOGIN_METHODS=helsinki_tunnus,helsinkiad
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ ARG NEXT_PUBLIC_LINKED_EVENTS_URL
ARG NEXT_PUBLIC_SENTRY_DSN
ARG SENTRY_AUTH_TOKEN
ARG NEXT_PUBLIC_ENVIRONMENT
ARG NEXT_PUBLIC_ATTENDANCE_LIST_LOGIN_METHODS
ARG NEXT_PUBLIC_SIGNUPS_LOGIN_METHODS

ARG OIDC_ISSUER
ARG OIDC_API_TOKENS_URL
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Use .env.local for development.
cp .env.local.example .env.local

| Name | Description |
| ----------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
|-------------------------------------------|----------------------------------------------------------------------------------------------------------------------------|
| PORT | Port where app is running. Default is 3001 |
| NEXT_PUBLIC_LINKED_EVENTS_URL | linkedevents api base url |
| NEXT_PUBLIC_ENVIRONMENT | Environment used in Sentry. Use local for development |
Expand All @@ -96,7 +96,9 @@ Use .env.local for development.
| NEXTAUTH_URL | Canonical url of the site used by next-auth |
| NEXT_ENV | 'development' or 'production' |
| NEXT_PUBLIC_WEB_STORE_INTEGRATION_ENABLED | Flag to enable Tapla integration. Default is false |
| NEXT_PUBLIC_WEB_STORE_API_BASE_URL  | Base url for the Tapla endpoints. Default is https://checkout-test-api.test.hel.ninja/v1 |
| NEXT_PUBLIC_WEB_STORE_API_BASE_URL | Base url for the Tapla endpoints. Default is https://checkout-test-api.test.hel.ninja/v1 |
| NEXT_PUBLIC_ATTENDANCE_LIST_LOGIN_METHODS | Login methods to use for attendance list viewing/editing. Default is suomi_fi |
| NEXT_PUBLIC_SIGNUPS_LOGIN_METHODS | Login methods to use for signup viewing/editing. Default is helsinki-tunnus and helsinkiad |

## Url parameters

Expand Down
2 changes: 2 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const moduleExports = {
publicRuntimeConfig: {
linkedEventsApiBaseUrl: process.env.NEXT_PUBLIC_LINKED_EVENTS_URL,
webStoreApiBaseUrl: process.env.NEXT_PUBLIC_WEB_STORE_API_BASE_URL,
attendanceListLoginMethods: process.env.NEXT_PUBLIC_ATTENDANCE_LIST_LOGIN_METHODS,
signupsLoginMethods: process.env.NEXT_PUBLIC_SIGNUPS_LOGIN_METHODS,
},
serverRuntimeConfig: {
env: process.env.NEXT_ENV,
Expand Down
12 changes: 12 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@ export enum SPLITTED_ROW_TYPE {
export const DEFAULT_SPLITTED_ROW_TYPE = SPLITTED_ROW_TYPE.MEDIUM_MEDIUM;

export type LoginMethod = "helsinki_tunnus" | "suomi_fi" | "helsinkiad";

export const parseLoginMethods = (value: string): LoginMethod[] => {
const validMethods: LoginMethod[] = ["helsinki_tunnus", "suomi_fi", "helsinkiad"];

return value.split(',').map(i => {
const trimmed = i.trim();
if (!validMethods.includes(trimmed as LoginMethod)) {
throw new Error(`Invalid login method: ${trimmed}`);
}
return trimmed as LoginMethod;
})
}
5 changes: 4 additions & 1 deletion src/domain/attendanceList/AttendanceListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useNotificationsContext } from '../../common/components/notificationsCo
import PageWrapper from '../../common/components/pageWrapper/PageWrapper';
import useLocale from '../../hooks/useLocale';
import { ExtendedSession } from '../../types';
import getPublicRuntimeConfig from "../../utils/getPublicRuntimeConfig";
import Container from '../app/layout/container/Container';
import MainContent from '../app/layout/mainContent/MainContent';
import TitleRow from '../app/layout/titleRow/TitleRow';
Expand Down Expand Up @@ -109,12 +110,14 @@ const AttendanceListPageWrapper: React.FC = () => {
},
});

const { attendanceListLoginMethods } = getPublicRuntimeConfig();

return (
<SignupsPagePermissions
event={event}
isLoadingData={isLoadingEventOrRegistration}
registration={registration}
loginMethods={["suomi_fi"]}
loginMethods={attendanceListLoginMethods}
>
<AttendanceListPage
event={event as Event}
Expand Down
5 changes: 4 additions & 1 deletion src/domain/singups/SignupsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useNotificationsContext } from '../../common/components/notificationsCo
import PageWrapper from '../../common/components/pageWrapper/PageWrapper';
import useLocale from '../../hooks/useLocale';
import { ExtendedSession } from '../../types';
import getPublicRuntimeConfig from "../../utils/getPublicRuntimeConfig";
import Container from '../app/layout/container/Container';
import MainContent from '../app/layout/mainContent/MainContent';
import TitleRow from '../app/layout/titleRow/TitleRow';
Expand Down Expand Up @@ -114,12 +115,14 @@ const SignupsPageWrapper: React.FC = () => {
registration,
} = useEventAndRegistrationData();

const { signupsLoginMethods } = getPublicRuntimeConfig();

return (
<SignupsPagePermissions
event={event}
isLoadingData={isLoadingEventOrRegistration}
registration={registration}
loginMethods={["helsinki_tunnus", "helsinkiad"]}
loginMethods={signupsLoginMethods}
>
<SignupsPage
event={event as Event}
Expand Down
11 changes: 10 additions & 1 deletion src/utils/__tests__/getPublicRuntimeConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ import { mockConfig } from '../mockNextJsConfig';
const publicRuntimeConfig = {
linkedEventsApiBaseUrl: 'https://linkedevents-backend:8000/v1',
webStoreApiBaseUrl: 'https://payment-test.com/v1',
attendanceListLoginMethods: 'suomi_fi',
signupsLoginMethods: 'helsinki_tunnus,helsinkiad',
};

const expectedPublicRuntimeConfig = {
linkedEventsApiBaseUrl: 'https://linkedevents-backend:8000/v1',
webStoreApiBaseUrl: 'https://payment-test.com/v1',
attendanceListLoginMethods: ['suomi_fi'],
signupsLoginMethods: ['helsinki_tunnus', 'helsinkiad'],
};

describe('getPublicRuntimeConfig function', () => {
it('should return public runtime config', () => {
mockConfig(publicRuntimeConfig, {});
expect(getPublicRuntimeConfig()).toEqual(publicRuntimeConfig);
expect(getPublicRuntimeConfig()).toEqual(expectedPublicRuntimeConfig);
});

const cases: [Record<string, string>][] = [
Expand Down
18 changes: 15 additions & 3 deletions src/utils/getPublicRuntimeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import getConfig from 'next/config';

import { parseLoginMethods } from "../constants";

const getPublicRuntimeConfig = () => {
const {
publicRuntimeConfig: { linkedEventsApiBaseUrl, webStoreApiBaseUrl },
publicRuntimeConfig: {
linkedEventsApiBaseUrl,
webStoreApiBaseUrl,
attendanceListLoginMethods,
signupsLoginMethods
},
} = getConfig();

if (!linkedEventsApiBaseUrl || !webStoreApiBaseUrl) {
if (!linkedEventsApiBaseUrl || !webStoreApiBaseUrl || !attendanceListLoginMethods || !signupsLoginMethods) {
throw new Error(
'Invalid configuration. Some required public runtime variable are missing'
);
}

return { linkedEventsApiBaseUrl, webStoreApiBaseUrl };
return {
linkedEventsApiBaseUrl,
webStoreApiBaseUrl,
attendanceListLoginMethods: parseLoginMethods(attendanceListLoginMethods),
signupsLoginMethods: parseLoginMethods(signupsLoginMethods),
};
};

export default getPublicRuntimeConfig;

0 comments on commit 1701fbe

Please sign in to comment.