(sessionData: string)
| 432 | ); |
| 433 | |
| 434 | const authenticateSealedSession = (sessionData: string) => |
| 435 | Effect.gen(function* () { |
| 436 | if (!sessionData) return null; |
| 437 | |
| 438 | const session = workos.userManagement.loadSealedSession({ |
| 439 | sessionData, |
| 440 | cookiePassword, |
| 441 | }); |
| 442 | |
| 443 | const local = yield* withServiceLogging( |
| 444 | "workos.session.local_verify", |
| 445 | workosErrorFromFailure, |
| 446 | verifySealedSessionLocally(sessionData, cookiePassword, sessionJwks), |
| 447 | ); |
| 448 | |
| 449 | if (isLocalSessionValid(local)) { |
| 450 | return { |
| 451 | userId: local.session.user.id, |
| 452 | email: local.session.user.email, |
| 453 | firstName: local.session.user.firstName, |
| 454 | lastName: local.session.user.lastName, |
| 455 | avatarUrl: local.session.user.profilePictureUrl, |
| 456 | organizationId: local.organizationId, |
| 457 | sessionId: local.sessionId, |
| 458 | refreshedSession: undefined as string | undefined, |
| 459 | }; |
| 460 | } |
| 461 | |
| 462 | if (isLocalSessionInvalidCookie(local)) return null; |
| 463 | |
| 464 | // Try refreshing |
| 465 | const refreshed = yield* use("session.refresh", () => session.refresh()).pipe( |
| 466 | Effect.orElseSucceed(() => ({ authenticated: false as const })), |
| 467 | ); |
| 468 | |
| 469 | if (!refreshed.authenticated || !("sealedSession" in refreshed) || !refreshed.sealedSession) |
| 470 | return null; |
| 471 | |
| 472 | return { |
| 473 | userId: refreshed.user.id, |
| 474 | email: refreshed.user.email, |
| 475 | firstName: refreshed.user.firstName, |
| 476 | lastName: refreshed.user.lastName, |
| 477 | avatarUrl: refreshed.user.profilePictureUrl, |
| 478 | organizationId: refreshed.organizationId, |
| 479 | sessionId: refreshed.sessionId, |
| 480 | refreshedSession: refreshed.sealedSession, |
| 481 | }; |
| 482 | }); |
| 483 | |
| 484 | return { |
| 485 | getAuthorizationUrl: (redirectUri: string, state?: string) => |
no test coverage detected