Skip to content

Commit 3fe673a

Browse files
committed
Update env for frontend auth info
1 parent bd054ac commit 3fe673a

4 files changed

Lines changed: 33 additions & 22 deletions

File tree

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ You will require env variables:
2222
OIDC_EXPECTED_AZP=<expected client ID - defaults to "cupcake-client">
2323
JWT_ENABLED=true/false
2424

25-
Note - the frontend OIDC authority and client ID are currently hardcoded in
26-
`frontend/app/composables/useAuth.ts`. If you need to use a different OIDC provider locally,
27-
update `AUTHORITY` and `CLIENT_ID` in that file.
25+
The frontend OIDC authority and client ID can be overridden via `NUXT_PUBLIC_OIDC_AUTHORITY` and
26+
`NUXT_PUBLIC_OIDC_CLIENT_ID` (see [frontend README](./frontend/README.md)). They default to the
27+
development Keycloak realm and client, so no change is needed for local dev against that environment.
2828

2929
If you are not running with auth (`JWT_ENABLED=false`) then localhost is fine.
3030

@@ -83,7 +83,9 @@ in the OIDC provider under the client specified by `OIDC_EXPECTED_AZP`.
8383

8484
The backend fetches the JWKS from the discovery document and validates incoming tokens against it.
8585

86-
Note - the frontend has the OIDC authority and client ID hardcoded in
87-
`frontend/app/composables/useAuth.ts` (`AUTHORITY` and `CLIENT_ID` constants). These must be
88-
kept in sync with the backend `OIDC_WELL_KNOWN_URL` and `OIDC_EXPECTED_AZP` settings.
86+
The frontend OIDC settings are configured via environment variables and must be kept in sync with
87+
the backend `OIDC_WELL_KNOWN_URL` and `OIDC_EXPECTED_AZP` settings:
88+
89+
NUXT_PUBLIC_OIDC_AUTHORITY - the OIDC authority URL (e.g. https://auth.example.com/realms/myrealm)
90+
NUXT_PUBLIC_OIDC_CLIENT_ID - the OIDC client ID (defaults to "cupcake-client")
8991

frontend/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,27 @@ Always lots to do - but - before we can release this:
1010

1111
## Build
1212

13-
Nuxt application using npm
13+
Nuxt application using pnpm
1414

15-
npm install
15+
pnpm install
1616

1717
## Local running
1818

19-
npm run dev
19+
pnpm dev
2020

2121
## Preview build
2222

23-
npm build
24-
npm preview
23+
pnpm build
24+
pnpm preview
2525

2626
## Deploy
2727

2828
Assuming we will build a docker container - add to [frontend action](../.github/workflows/frontend.yaml) when decided.
2929

3030
### Configuration
3131

32-
Set the environment variable `CUPCAKE_BACKEND` to the backend URL prefix (protocol, host and port, no trailing `/`). It
33-
expects to find `/api`, `/login` and `/slackCallback` at this location.
34-
35-
If not set it will use `http://localhost:8080` for development.
32+
| Environment Variable | Description | Default |
33+
|----------------------------|----------------------------------------------------------|------------------------------------------------|
34+
| `CUPCAKE_FRONTEND` | Hostname the dev server allows (Vite `allowedHosts`) | `localhost` |
35+
| `NUXT_PUBLIC_OIDC_AUTHORITY` | OIDC authority URL (e.g. Keycloak realm URL) | `https://auth.home.chrissearle.org/realms/HA12` |
36+
| `NUXT_PUBLIC_OIDC_CLIENT_ID` | OIDC client ID | `cupcake-client` |

frontend/app/composables/useAuth.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,17 @@ type TokenResponse = {
2323
token_type: string
2424
}
2525

26-
const AUTHORITY = 'https://auth.home.chrissearle.org/realms/HA12'
27-
const CLIENT_ID = 'cupcake-client'
28-
2926
Log.setLevel(Log.WARN)
3027

3128
let _userManager: UserManager | null = null
3229

3330
function getUserManager(): UserManager {
3431
if (!_userManager) {
32+
const { oidcAuthority, oidcClientId } = useRuntimeConfig().public
3533
const origin = window.location.origin
3634
_userManager = new UserManager({
37-
authority: AUTHORITY,
38-
client_id: CLIENT_ID,
35+
authority: oidcAuthority,
36+
client_id: oidcClientId,
3937
redirect_uri: `${origin}/`,
4038
response_type: 'code',
4139
scope: 'openid profile email offline_access',
@@ -85,7 +83,8 @@ let tokenEndpointPromise: Promise<string> | null = null
8583

8684
async function getTokenEndpoint(): Promise<string> {
8785
if (!tokenEndpointPromise) {
88-
tokenEndpointPromise = fetch(`${AUTHORITY}/.well-known/openid-configuration`)
86+
const { oidcAuthority } = useRuntimeConfig().public
87+
tokenEndpointPromise = fetch(`${oidcAuthority}/.well-known/openid-configuration`)
8988
.then(async (r) => {
9089
if (!r.ok) throw new Error('Failed to fetch OIDC discovery')
9190
return (await r.json()) as Discovery
@@ -132,9 +131,11 @@ export async function ensureAccessToken(skewSeconds = 30): Promise<string | null
132131
return null
133132
}
134133

134+
const { oidcClientId } = useRuntimeConfig().public
135+
135136
const body = new URLSearchParams()
136137
body.set('grant_type', 'refresh_token')
137-
body.set('client_id', CLIENT_ID)
138+
body.set('client_id', oidcClientId)
138139
body.set('refresh_token', refreshToken)
139140

140141
const resp = await fetch(tokenEndpoint, {

frontend/nuxt.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ if (
1111
export default defineNuxtConfig({
1212
compatibilityDate: "2025-08-06",
1313

14+
runtimeConfig: {
15+
public: {
16+
oidcAuthority: "https://auth.home.chrissearle.org/realms/HA12",
17+
oidcClientId: "cupcake-client",
18+
},
19+
},
20+
1421
ssr: false,
1522
devtools: { enabled: true },
1623
modules: [

0 commit comments

Comments
 (0)