|
1 | 1 | import { redirect } from '@sveltejs/kit'; |
2 | 2 |
|
3 | | -import { TEMPORARY_REDIRECT } from '$lib/constants/http-response-status-codes'; |
| 3 | +import { superValidate } from 'sveltekit-superforms/server'; |
| 4 | +import { zod } from 'sveltekit-superforms/adapters'; |
| 5 | + |
| 6 | +import { TEMPORARY_REDIRECT, SEE_OTHER } from '$lib/constants/http-response-status-codes'; |
| 7 | +import { HOME_PAGE } from '$lib/constants/navbar-links'; |
| 8 | +import { authSchema } from '$lib/zod/schema'; |
4 | 9 | import { Roles } from '$lib/types/user'; |
5 | 10 |
|
| 11 | +/** |
| 12 | + * Initialize authentication form pages (login/signup) |
| 13 | + * Redirects to home page if already logged in, |
| 14 | + * otherwise initializes the authentication form for unauthenticated users |
| 15 | + */ |
| 16 | +export const initializeAuthForm = async (locals: App.Locals) => { |
| 17 | + const session = await locals.auth.validate(); |
| 18 | + |
| 19 | + if (session) { |
| 20 | + redirect(SEE_OTHER, HOME_PAGE); |
| 21 | + } |
| 22 | + |
| 23 | + return await createAuthFormWithFallback(); |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * Create authentication form with comprehensive fallback handling |
| 28 | + * Tries multiple strategies until one succeeds |
| 29 | + */ |
| 30 | +const createAuthFormWithFallback = async () => { |
| 31 | + for (const strategy of formCreationStrategies) { |
| 32 | + try { |
| 33 | + const result = await strategy.run(); |
| 34 | + console.log(`Success: ${strategy.name}`); |
| 35 | + |
| 36 | + return result; |
| 37 | + } catch (error) { |
| 38 | + console.warn(`Failed to ${strategy.name}`); |
| 39 | + |
| 40 | + if (error instanceof Error) { |
| 41 | + console.warn('Error:', error.message); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // This should never be reached due to manual creation strategy |
| 47 | + throw new Error('Failed to create form for authentication.'); |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * Form creation strategies in order of preference |
| 52 | + * Each strategy attempts a different approach to create a valid form |
| 53 | + * |
| 54 | + * See: |
| 55 | + * https://superforms.rocks/concepts/client-validation |
| 56 | + * https://superforms.rocks/api#supervalidate-options |
| 57 | + */ |
| 58 | +const formCreationStrategies = [ |
| 59 | + { |
| 60 | + name: '(Basic case) Use standard superValidate', |
| 61 | + async run() { |
| 62 | + const form = await superValidate(null, zod(authSchema)); |
| 63 | + return { form: { ...form, message: '' } }; |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: 'Use zod adapter explicitly', |
| 68 | + async run() { |
| 69 | + const zodAdapter = zod(authSchema); |
| 70 | + const form = await superValidate(null, zodAdapter); |
| 71 | + return { form: { ...form, message: '' } }; |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: 'Create form by manually defining structure', |
| 76 | + async run() { |
| 77 | + const defaultForm = { |
| 78 | + id: 'fallback-form-' + Date.now(), // Note: Use only client-side validation |
| 79 | + valid: true, |
| 80 | + posted: false, |
| 81 | + data: { username: '', password: '' }, |
| 82 | + errors: {}, |
| 83 | + constraints: { |
| 84 | + username: { minlength: 3, maxlength: 24, required: true, pattern: '[\\w]*' }, |
| 85 | + password: { |
| 86 | + minlength: 8, |
| 87 | + maxlength: 128, |
| 88 | + required: true, |
| 89 | + pattern: '(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\\d)[a-zA-Z\\d]{8,128}', |
| 90 | + }, |
| 91 | + }, |
| 92 | + // [Workaround] Critical fix for production environment schema shape error |
| 93 | + // SuperForms requires a 'shape' property for internal form structure validation |
| 94 | + // In production builds, Zod schema internals may be optimized away, causing |
| 95 | + // "SchemaError: No shape could be created for schema" errors |
| 96 | + // |
| 97 | + // See: |
| 98 | + // SuperForms source - schemaShape() function in adapters/zod.ts |
| 99 | + // https://github.com/ciscoheat/sveltekit-superforms/issues/594 |
| 100 | + shape: { |
| 101 | + username: { type: 'string' }, |
| 102 | + password: { type: 'string' }, |
| 103 | + }, |
| 104 | + }; |
| 105 | + return { form: { ...defaultForm, message: '' } }; |
| 106 | + }, |
| 107 | + }, |
| 108 | +]; |
| 109 | + |
6 | 110 | export const ensureSessionOrRedirect = async (locals: App.Locals): Promise<void> => { |
7 | 111 | const session = await locals.auth.validate(); |
8 | 112 |
|
|
0 commit comments