codeforlife 2.7.0 → 2.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [2.7.2](https://github.com/ocadotechnology/codeforlife-package-javascript/compare/v2.7.1...v2.7.2) (2025-08-15)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * comment ([1364251](https://github.com/ocadotechnology/codeforlife-package-javascript/commit/13642516aea7a89d7c3e24e7813391686587447f))
7
+
8
+ ## [2.7.1](https://github.com/ocadotechnology/codeforlife-package-javascript/compare/v2.7.0...v2.7.1) (2025-08-15)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * optionally set useSessionMetadata ([22c5253](https://github.com/ocadotechnology/codeforlife-package-javascript/commit/22c525341badef6f13fe9adc1489d033792d2340))
14
+
1
15
  # [2.7.0](https://github.com/ocadotechnology/codeforlife-package-javascript/compare/v2.6.17...v2.7.0) (2025-08-15)
2
16
 
3
17
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "codeforlife",
3
3
  "description": "Common frontend code",
4
4
  "private": false,
5
- "version": "2.7.0",
5
+ "version": "2.7.2",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "cli": "VITE_CONFIG=./vite.config.ts ../scripts/frontend $@"
@@ -2,7 +2,6 @@ import { type EndpointBuilder, type Api } from "@reduxjs/toolkit/query/react"
2
2
 
3
3
  import { login, logout } from "../../slices/session"
4
4
 
5
- export type ExchangeOAuth2CodeResult = null
6
5
  export type ExchangeOAuth2CodeArg = {
7
6
  code: string
8
7
  code_verifier: string
@@ -15,10 +15,7 @@ import {
15
15
  type OAuth2RequestCodeUrlSearchParams,
16
16
  type OAuth2ReceiveCodeUrlSearchParams,
17
17
  } from "../utils/auth"
18
- import {
19
- type ExchangeOAuth2CodeResult,
20
- type ExchangeOAuth2CodeArg,
21
- } from "../api/endpoints/session"
18
+ import { type ExchangeOAuth2CodeArg } from "../api/endpoints/session"
22
19
  import { useSearchParams, useLocation, useNavigate } from "./router"
23
20
  import { SESSION_METADATA_COOKIE_NAME } from "../settings"
24
21
  import { selectIsLoggedIn } from "../slices/session"
@@ -34,14 +31,25 @@ export interface SessionMetadata {
34
31
  otp_bypass_token_exists: boolean
35
32
  }
36
33
 
37
- export function useSessionMetadata(): SessionMetadata | undefined {
34
+ export function useSessionMetadata<T = SessionMetadata>(
35
+ cookieName = SESSION_METADATA_COOKIE_NAME,
36
+ ): T | undefined {
38
37
  return useSelector(selectIsLoggedIn)
39
- ? (JSON.parse(
40
- Cookies.get(SESSION_METADATA_COOKIE_NAME)!,
41
- ) as SessionMetadata)
38
+ ? (JSON.parse(Cookies.get(cookieName)!) as T)
42
39
  : undefined
43
40
  }
44
41
 
42
+ /**
43
+ * A utility function to predefine a useSessionMetadata hook.
44
+ * @param cookieName The name of the session metadata cookie.
45
+ * @returns An object containing the session metadata.
46
+ */
47
+ useSessionMetadata.predefine = <SessionMetadata,>(
48
+ cookieName = SESSION_METADATA_COOKIE_NAME,
49
+ ) => {
50
+ return () => useSessionMetadata<SessionMetadata>(cookieName)
51
+ }
52
+
45
53
  export type UseSessionChildrenFunction<Required extends boolean> = (
46
54
  metadata: Required extends true
47
55
  ? SessionMetadata
@@ -198,7 +206,7 @@ export function useOAuth2CodeChallenge(
198
206
  return [_codeChallenge, resetCodeChallenge]
199
207
  }
200
208
 
201
- export interface UseOAuth2KwArgs<ResultType = ExchangeOAuth2CodeResult> {
209
+ interface BaseUseOAuth2KwArgs<SessionMetadata> {
202
210
  provider: string
203
211
  authUri: string
204
212
  clientId: string
@@ -207,15 +215,24 @@ export interface UseOAuth2KwArgs<ResultType = ExchangeOAuth2CodeResult> {
207
215
  responseType?: "code"
208
216
  accessType?: "offline"
209
217
  prompt?: string
210
- useLoginMutation: TypedUseMutation<ResultType, ExchangeOAuth2CodeArg, any>
211
- onCreateSession: (result: ResultType) => void
218
+ useLoginMutation: TypedUseMutation<
219
+ SessionMetadata,
220
+ ExchangeOAuth2CodeArg,
221
+ any
222
+ >
223
+ onCreateSession: (result: SessionMetadata) => void
212
224
  onRetrieveSession: (metadata: SessionMetadata) => void
213
225
  }
214
226
 
227
+ interface UseOAuth2KwArgs<SessionMetadata>
228
+ extends BaseUseOAuth2KwArgs<SessionMetadata> {
229
+ useSessionMetadata: () => SessionMetadata | undefined
230
+ }
231
+
215
232
  export type OAuth2 = [string, OAuth2RequestCodeUrlSearchParams] | []
216
233
 
217
234
  // https://datatracker.ietf.org/doc/html/rfc7636
218
- export function useOAuth2<ResultType = ExchangeOAuth2CodeResult>({
235
+ function _useOAuth2<SessionMetadata>({
219
236
  provider,
220
237
  authUri,
221
238
  clientId,
@@ -224,10 +241,11 @@ export function useOAuth2<ResultType = ExchangeOAuth2CodeResult>({
224
241
  responseType = "code",
225
242
  accessType = "offline",
226
243
  prompt,
244
+ useSessionMetadata,
227
245
  useLoginMutation,
228
246
  onCreateSession,
229
247
  onRetrieveSession,
230
- }: UseOAuth2KwArgs<ResultType>): OAuth2 {
248
+ }: UseOAuth2KwArgs<SessionMetadata>): OAuth2 {
231
249
  const [state, resetState] = useOAuth2State(provider)
232
250
  const [
233
251
  {
@@ -280,7 +298,7 @@ export function useOAuth2<ResultType = ExchangeOAuth2CodeResult>({
280
298
  // ...and the page's state contains the stored state...
281
299
  locationState.state === state &&
282
300
  // ...and the login endpoint was not called with the current values or has
283
- // not returned and error...
301
+ // not returned an error...
284
302
  (loginArgs.code !== locationState.code ||
285
303
  loginArgs.code_verifier !== codeVerifier ||
286
304
  loginArgs.redirect_uri !== redirectUri ||
@@ -362,3 +380,16 @@ export function useOAuth2<ResultType = ExchangeOAuth2CodeResult>({
362
380
 
363
381
  return []
364
382
  }
383
+
384
+ export const useOAuth2: {
385
+ <SessionMetadata>(kwargs: UseOAuth2KwArgs<SessionMetadata>): OAuth2
386
+ (kwargs: BaseUseOAuth2KwArgs<SessionMetadata>): OAuth2
387
+ } = <_SessionMetadata,>(
388
+ kwargs:
389
+ | UseOAuth2KwArgs<_SessionMetadata>
390
+ | BaseUseOAuth2KwArgs<SessionMetadata>,
391
+ ): OAuth2 => {
392
+ return "useSessionMetadata" in kwargs
393
+ ? _useOAuth2(kwargs)
394
+ : _useOAuth2({ ...kwargs, useSessionMetadata })
395
+ }