Skip to content

Commit 5baeaea

Browse files
committed
♻️ Add types and update console.warn (#2446)
1 parent e44819b commit 5baeaea

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/lib/utils/auth_forms.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { superValidate } from 'sveltekit-superforms/server';
44
import { zod } from 'sveltekit-superforms/adapters';
55

66
import type {
7+
AuthForm,
78
AuthFormCreationStrategies,
89
AuthFormValidationStrategies,
910
} from '$lib/types/auth_forms';
@@ -16,8 +17,10 @@ import { HOME_PAGE } from '$lib/constants/navbar-links';
1617
* Initialize authentication form pages (login/signup)
1718
* Redirects to home page if already logged in,
1819
* otherwise initializes the authentication form for unauthenticated users
20+
* @param locals - The application locals containing authentication state
21+
* @returns { form: AuthForm } - The initialized authentication form
1922
*/
20-
export const initializeAuthForm = async (locals: App.Locals) => {
23+
export const initializeAuthForm = async (locals: App.Locals): Promise<{ form: AuthForm }> => {
2124
const session = await locals.auth.validate();
2225

2326
if (session) {
@@ -31,19 +34,16 @@ export const initializeAuthForm = async (locals: App.Locals) => {
3134
* Create authentication form with comprehensive fallback handling
3235
* Tries multiple strategies until one succeeds
3336
*/
34-
export const createAuthFormWithFallback = async () => {
37+
export const createAuthFormWithFallback = async (): Promise<{ form: AuthForm }> => {
3538
for (const strategy of formCreationStrategies) {
3639
try {
3740
const result = await strategy.run();
3841

3942
return result;
4043
} catch (error) {
4144
if (isDevelopmentMode()) {
42-
console.warn(`Failed to ${strategy.name}`);
43-
44-
if (error instanceof Error) {
45-
console.warn('Error:', error.message);
46-
}
45+
console.warn(`Create authForm strategy: Failed to ${strategy.name}`);
46+
console.warn(error instanceof Error ? (error.stack ?? error.message) : error);
4747
}
4848
}
4949
}
@@ -73,7 +73,7 @@ const formCreationStrategies: AuthFormCreationStrategies = [
7373
name: 'Create form by manually defining structure',
7474
async run() {
7575
const defaultForm = {
76-
valid: true,
76+
valid: false,
7777
posted: false,
7878
errors: {},
7979
message: '',
@@ -92,19 +92,16 @@ const formCreationStrategies: AuthFormCreationStrategies = [
9292
* @param request - The incoming request containing form data
9393
* @returns The validated form object (bare form, suitable for actions: fail(..., { form }))
9494
*/
95-
export const validateAuthFormWithFallback = async (request: Request) => {
95+
export const validateAuthFormWithFallback = async (request: Request): Promise<AuthForm> => {
9696
for (const strategy of formValidationStrategies) {
9797
try {
9898
const result = await strategy.run(request);
9999

100100
return result.form;
101101
} catch (error) {
102102
if (isDevelopmentMode()) {
103-
console.warn(`Failed to ${strategy.name}`);
104-
105-
if (error instanceof Error) {
106-
console.warn('Error:', error.message);
107-
}
103+
console.warn(`Validate authForm strategy: Failed to ${strategy.name}`);
104+
console.warn(error instanceof Error ? (error.stack ?? error.message) : error);
108105
}
109106
}
110107
}

0 commit comments

Comments
 (0)