Skip to content

Commit

Permalink
narrow withAuth type to User when ensureSignedIn is true (#185)
Browse files Browse the repository at this point in the history
When ensureEignedIn is true, the user will either be returned or next
will throw a `NEXT_REDIRECT` to redirect to log in.
  • Loading branch information
nicknisi authored Jan 21, 2025
1 parent b059722 commit a21db78
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,13 @@ async function redirectToSignIn() {
redirect(await getAuthorizationUrl({ returnPathname, screenHint }));
}

async function withAuth(options?: { ensureSignedIn?: boolean }): Promise<UserInfo | NoUserInfo>;
async function withAuth({ ensureSignedIn = false }: { ensureSignedIn?: boolean } = {}): Promise<UserInfo | NoUserInfo> {
async function withAuth(options: { ensureSignedIn: true }): Promise<UserInfo>;
async function withAuth(options?: { ensureSignedIn?: true | false }): Promise<UserInfo | NoUserInfo>;
async function withAuth(options?: { ensureSignedIn?: boolean }): Promise<UserInfo | NoUserInfo> {
const session = await getSessionFromHeader();

if (!session) {
if (ensureSignedIn) {
if (options?.ensureSignedIn) {
await redirectToSignIn();
}
return { user: null };
Expand Down

0 comments on commit a21db78

Please sign in to comment.