-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth-endpoints.zod.ts
More file actions
218 lines (189 loc) · 7.15 KB
/
auth-endpoints.zod.ts
File metadata and controls
218 lines (189 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
/**
* Authentication Endpoint Specification
*
* Defines the canonical HTTP endpoints for the authentication service.
* Based on better-auth v1.4.18 endpoint conventions.
*
* NOTE: ObjectStack's auth implementation uses better-auth library which has
* established endpoint conventions. This spec documents those conventions as
* the canonical API contract.
*/
// ==========================================
// Endpoint Path Definitions
// ==========================================
/**
* Authentication Endpoint Paths
*
* These are the paths relative to the auth base route (e.g., /api/v1/auth).
* Based on better-auth's endpoint structure.
*/
export const AuthEndpointPaths = {
// Email/Password Authentication
signInEmail: '/sign-in/email',
signUpEmail: '/sign-up/email',
signOut: '/sign-out',
// Session Management
getSession: '/get-session',
// Password Management
forgetPassword: '/forget-password',
resetPassword: '/reset-password',
// Email Verification
sendVerificationEmail: '/send-verification-email',
verifyEmail: '/verify-email',
// OAuth (dynamic based on provider)
// authorize: '/authorize/:provider'
// callback: '/callback/:provider'
// 2FA (when enabled)
twoFactorEnable: '/two-factor/enable',
twoFactorVerify: '/two-factor/verify',
// Passkeys (when enabled)
passkeyRegister: '/passkey/register',
passkeyAuthenticate: '/passkey/authenticate',
// Magic Links (when enabled)
magicLinkSend: '/magic-link/send',
magicLinkVerify: '/magic-link/verify',
} as const;
/**
* HTTP Method + Path Specification
*
* Defines the complete HTTP contract for each endpoint.
*/
export const AuthEndpointSchema = z.object({
/** Sign in with email and password */
signInEmail: z.object({
method: z.literal('POST'),
path: z.literal(AuthEndpointPaths.signInEmail),
description: z.literal('Sign in with email and password'),
}),
/** Register new user with email and password */
signUpEmail: z.object({
method: z.literal('POST'),
path: z.literal(AuthEndpointPaths.signUpEmail),
description: z.literal('Register new user with email and password'),
}),
/** Sign out current user */
signOut: z.object({
method: z.literal('POST'),
path: z.literal(AuthEndpointPaths.signOut),
description: z.literal('Sign out current user'),
}),
/** Get current user session */
getSession: z.object({
method: z.literal('GET'),
path: z.literal(AuthEndpointPaths.getSession),
description: z.literal('Get current user session'),
}),
/** Request password reset email */
forgetPassword: z.object({
method: z.literal('POST'),
path: z.literal(AuthEndpointPaths.forgetPassword),
description: z.literal('Request password reset email'),
}),
/** Reset password with token */
resetPassword: z.object({
method: z.literal('POST'),
path: z.literal(AuthEndpointPaths.resetPassword),
description: z.literal('Reset password with token'),
}),
/** Send email verification */
sendVerificationEmail: z.object({
method: z.literal('POST'),
path: z.literal(AuthEndpointPaths.sendVerificationEmail),
description: z.literal('Send email verification link'),
}),
/** Verify email with token */
verifyEmail: z.object({
method: z.literal('GET'),
path: z.literal(AuthEndpointPaths.verifyEmail),
description: z.literal('Verify email with token'),
}),
});
/**
* Endpoint Aliases
*
* Common aliases for better developer experience.
* These map to the canonical better-auth endpoints.
*/
export const AuthEndpointAliases = {
login: AuthEndpointPaths.signInEmail,
register: AuthEndpointPaths.signUpEmail,
logout: AuthEndpointPaths.signOut,
me: AuthEndpointPaths.getSession,
} as const;
/**
* Full Endpoint URLs
*
* Helper to construct full endpoint URLs given a base path.
*/
export function getAuthEndpointUrl(basePath: string, endpoint: keyof typeof AuthEndpointPaths): string {
const cleanBase = basePath.replace(/\/$/, '');
return `${cleanBase}${AuthEndpointPaths[endpoint]}`;
}
/**
* Endpoint Mapping
*
* Maps common/legacy endpoint names to canonical better-auth paths.
* This allows clients to use simpler names while maintaining compatibility.
*/
export const EndpointMapping = {
'/login': AuthEndpointPaths.signInEmail,
'/register': AuthEndpointPaths.signUpEmail,
'/logout': AuthEndpointPaths.signOut,
'/me': AuthEndpointPaths.getSession,
'/refresh': AuthEndpointPaths.getSession, // Session refresh handled by better-auth automatically
} as const;
// ==========================================
// Auth Configuration Discovery
// ==========================================
/**
* Auth Provider Information (Public)
*
* Public-facing information about an OAuth/social provider.
* Does NOT include sensitive configuration (clientSecret, etc.)
*/
export const AuthProviderInfoSchema = z.object({
id: z.string().describe('Provider ID (e.g., google, github, microsoft)'),
name: z.string().describe('Display name (e.g., Google, GitHub)'),
enabled: z.boolean().describe('Whether this provider is enabled'),
});
/**
* Email/Password Configuration (Public)
*/
export const EmailPasswordConfigPublicSchema = z.object({
enabled: z.boolean().describe('Whether email/password auth is enabled'),
disableSignUp: z.boolean().optional().describe('Whether new user registration is disabled'),
requireEmailVerification: z.boolean().optional().describe('Whether email verification is required'),
});
/**
* Auth Features Configuration (Public)
*/
export const AuthFeaturesConfigSchema = z.object({
twoFactor: z.boolean().default(false).describe('Two-factor authentication enabled'),
passkeys: z.boolean().default(false).describe('Passkey/WebAuthn support enabled'),
magicLink: z.boolean().default(false).describe('Magic link login enabled'),
organization: z.boolean().default(false).describe('Multi-tenant organization support enabled'),
});
/**
* Get Auth Config Response
*
* Returns the public authentication configuration that the frontend
* can use to render appropriate login UI (social provider buttons, etc.)
*/
export const GetAuthConfigResponseSchema = z.object({
emailPassword: EmailPasswordConfigPublicSchema.describe('Email/password authentication config'),
socialProviders: z.array(AuthProviderInfoSchema).describe('Available social/OAuth providers'),
features: AuthFeaturesConfigSchema.describe('Enabled authentication features'),
});
// ==========================================
// Type Exports
// ==========================================
export type AuthEndpoint = z.infer<typeof AuthEndpointSchema>;
export type AuthEndpointPath = typeof AuthEndpointPaths[keyof typeof AuthEndpointPaths];
export type AuthEndpointAlias = keyof typeof AuthEndpointAliases;
export type EndpointMappingKey = keyof typeof EndpointMapping;
export type AuthProviderInfo = z.infer<typeof AuthProviderInfoSchema>;
export type EmailPasswordConfigPublic = z.infer<typeof EmailPasswordConfigPublicSchema>;
export type AuthFeaturesConfig = z.infer<typeof AuthFeaturesConfigSchema>;
export type GetAuthConfigResponse = z.infer<typeof GetAuthConfigResponseSchema>;