Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 78 additions & 4 deletions src/server/server-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ describe('Server Functions', () => {
});

describe('getSignInUrl', () => {
it('generates sign-in URL with return path', async () => {
it('generates sign-in URL with return path string', async () => {
const signInUrl = 'https://auth.workos.com/sign-in';
mockAuthkit.getSignInUrl.mockResolvedValue(signInUrl);

Expand All @@ -247,19 +247,56 @@ describe('Server Functions', () => {
expect(mockAuthkit.getSignInUrl).toHaveBeenCalledWith({ returnPathname: '/profile' });
});

it('works without return path', async () => {
it('works without options', async () => {
const signInUrl = 'https://auth.workos.com/sign-in';
mockAuthkit.getSignInUrl.mockResolvedValue(signInUrl);

const result = await serverFunctions.getSignInUrl({ data: undefined });

expect(result).toBe(signInUrl);
expect(mockAuthkit.getSignInUrl).toHaveBeenCalledWith({ returnPathname: undefined });
expect(mockAuthkit.getSignInUrl).toHaveBeenCalledWith(undefined);
});

it('passes state option through', async () => {
const signInUrl = 'https://auth.workos.com/sign-in';
mockAuthkit.getSignInUrl.mockResolvedValue(signInUrl);

const result = await serverFunctions.getSignInUrl({
data: { returnPathname: '/dashboard', state: 'custom-state' },
});

expect(result).toBe(signInUrl);
expect(mockAuthkit.getSignInUrl).toHaveBeenCalledWith({
returnPathname: '/dashboard',
state: 'custom-state',
});
});

it('passes all options through', async () => {
const signInUrl = 'https://auth.workos.com/sign-in';
mockAuthkit.getSignInUrl.mockResolvedValue(signInUrl);

const result = await serverFunctions.getSignInUrl({
data: {
returnPathname: '/dashboard',
state: 'my-state',
organizationId: 'org_123',
loginHint: 'user@example.com',
},
});

expect(result).toBe(signInUrl);
expect(mockAuthkit.getSignInUrl).toHaveBeenCalledWith({
returnPathname: '/dashboard',
state: 'my-state',
organizationId: 'org_123',
loginHint: 'user@example.com',
});
});
});

describe('getSignUpUrl', () => {
it('generates sign-up URL with return path', async () => {
it('generates sign-up URL with return path string', async () => {
const signUpUrl = 'https://auth.workos.com/sign-up';
mockAuthkit.getSignUpUrl.mockResolvedValue(signUpUrl);

Expand All @@ -278,6 +315,43 @@ describe('Server Functions', () => {
expect(result).toBe(signUpUrl);
expect(mockAuthkit.getSignUpUrl).toHaveBeenCalledWith({ returnPathname: '/onboarding' });
});

it('passes state option through', async () => {
const signUpUrl = 'https://auth.workos.com/sign-up';
mockAuthkit.getSignUpUrl.mockResolvedValue(signUpUrl);

const result = await serverFunctions.getSignUpUrl({
data: { returnPathname: '/welcome', state: 'signup-flow' },
});

expect(result).toBe(signUpUrl);
expect(mockAuthkit.getSignUpUrl).toHaveBeenCalledWith({
returnPathname: '/welcome',
state: 'signup-flow',
});
});

it('passes all options through', async () => {
const signUpUrl = 'https://auth.workos.com/sign-up';
mockAuthkit.getSignUpUrl.mockResolvedValue(signUpUrl);

const result = await serverFunctions.getSignUpUrl({
data: {
returnPathname: '/onboarding',
state: 'invite-123',
organizationId: 'org_456',
loginHint: 'newuser@example.com',
},
});

expect(result).toBe(signUpUrl);
expect(mockAuthkit.getSignUpUrl).toHaveBeenCalledWith({
returnPathname: '/onboarding',
state: 'invite-123',
organizationId: 'org_456',
loginHint: 'newuser@example.com',
});
});
});

describe('signOut headers handling', () => {
Expand Down
41 changes: 25 additions & 16 deletions src/server/server-functions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createServerFn } from '@tanstack/react-start';
import { redirect } from '@tanstack/react-router';
import { getAuthkit, getConfig } from './authkit-loader.js';
import { getRawAuthFromContext, getSessionWithRefreshToken, refreshSession } from './auth-helpers.js';
import type { User, Impersonator } from '../types.js';
import { createServerFn } from '@tanstack/react-start';
import type { Impersonator, User } from '../types.js';
import { getRawAuthFromContext, refreshSession } from './auth-helpers.js';
import { getAuthkit } from './authkit-loader.js';

// Type-only import - safe for bundling
import type { GetAuthorizationUrlOptions as GetAuthURLOptions } from '@workos/authkit-session';
Expand Down Expand Up @@ -162,25 +162,31 @@ export const getAuthorizationUrl = createServerFn({ method: 'GET' })
return authkit.getAuthorizationUrl(options);
});

/** Options for getSignInUrl/getSignUpUrl - all GetAuthURLOptions except screenHint */
type SignInUrlOptions = Omit<GetAuthURLOptions, 'screenHint'>;

/**
* Get the sign-in URL.
* Convenience wrapper around getAuthorizationUrl with sign-in screen hint.
*
* @example
* ```typescript
* // Without return path
* // Without options
* const url = await getSignInUrl();
*
* // With return path
* const url = await getSignInUrl({ data: { returnPathname: '/dashboard' } });
* // With return path (string shorthand)
* const url = await getSignInUrl({ data: '/dashboard' });
*
* // With options
* const url = await getSignInUrl({ data: { returnPathname: '/dashboard', state: 'custom-state' } });
* ```
*/
export const getSignInUrl = createServerFn({ method: 'GET' })
.inputValidator((data?: string | { returnPathname?: string }) => data)
.inputValidator((data?: string | SignInUrlOptions) => data)
.handler(async ({ data }) => {
const returnPathname = typeof data === 'string' ? data : data?.returnPathname;
const options = typeof data === 'string' ? { returnPathname: data } : data;
const authkit = await getAuthkit();
return authkit.getSignInUrl({ returnPathname });
return authkit.getSignInUrl(options);
});

/**
Expand All @@ -189,19 +195,22 @@ export const getSignInUrl = createServerFn({ method: 'GET' })
*
* @example
* ```typescript
* // Without return path
* // Without options
* const url = await getSignUpUrl();
*
* // With return path
* const url = await getSignUpUrl({ data: { returnPathname: '/dashboard' } });
* // With return path (string shorthand)
* const url = await getSignUpUrl({ data: '/dashboard' });
*
* // With options
* const url = await getSignUpUrl({ data: { returnPathname: '/dashboard', state: 'custom-state' } });
* ```
*/
export const getSignUpUrl = createServerFn({ method: 'GET' })
.inputValidator((data?: string | { returnPathname?: string }) => data)
.inputValidator((data?: string | SignInUrlOptions) => data)
.handler(async ({ data }) => {
const returnPathname = typeof data === 'string' ? data : data?.returnPathname;
const options = typeof data === 'string' ? { returnPathname: data } : data;
const authkit = await getAuthkit();
return authkit.getSignUpUrl({ returnPathname });
return authkit.getSignUpUrl(options);
});

/**
Expand Down
3 changes: 1 addition & 2 deletions src/server/server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';

// Setup mocks before imports
const mockHandleCallback = vi.fn();
Expand All @@ -22,7 +22,6 @@ vi.mock('@tanstack/react-router', () => ({
}));

import { handleCallbackRoute } from './server';
import { getAuthkit } from './authkit-loader';

describe('handleCallbackRoute', () => {
beforeEach(() => {
Expand Down