Skip to content

Commit

Permalink
fix: sveltekit-flash-message 2 args with cookies does not work
Browse files Browse the repository at this point in the history
sooo using event after all, ah well
  • Loading branch information
V-ed committed Feb 27, 2024
1 parent cbdc413 commit de63393
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 36 deletions.
13 changes: 6 additions & 7 deletions client/src/lib/utils/assertions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createLayoutAlert, type LayoutAlertData } from '$lib/components/LayoutAlert/helper';
import { createToasts, type ToastData, type ToastManagerData } from '$lib/components/ToastManager/helper';
import type { PageMessages } from '$lib/types';
import { error, fail, type Cookies, type RequestEvent } from '@sveltejs/kit';
import { error, fail, type RequestEvent } from '@sveltejs/kit';
import { StatusCodes } from 'http-status-codes';
import { redirect } from 'sveltekit-flash-message/server';
import type { Infer, SuperValidated } from 'sveltekit-superforms';
Expand Down Expand Up @@ -37,6 +37,7 @@ export type ActionNotValidData = {

export type AssertTsRestActionResultOKArgs<T extends { status: number }> = {
form?: SuperValidated<Infer<AnyZodObject>>;
event: RequestEvent;
result: () => Awaitable<T>;
onValid?: (result: ValidResult<T>) => Awaitable<
PageMessages & {
Expand All @@ -45,8 +46,7 @@ export type AssertTsRestActionResultOKArgs<T extends { status: number }> = {
}
>;
onNotOk?: (result: InvalidResult<T>, data: ActionNotValidData) => Awaitable<unknown>;
} & ({ toast?: Partial<ToastManagerData> } | { layoutAlert: Partial<LayoutAlertData> }) &
({ cookies: Cookies } | { event: RequestEvent });
} & ({ toast?: Partial<ToastManagerData> } | { layoutAlert: Partial<LayoutAlertData> });

export function assertTsRestActionResultOK<T extends { status: number; body: unknown }>(args: AssertTsRestActionResultOKArgs<T>) {
// Define checking result function
Expand Down Expand Up @@ -86,15 +86,14 @@ export function assertTsRestActionResultOK<T extends { status: number; body: unk

const { redirectTo, ...expectedResult } = (await args.onValid?.(result)) ?? {};

const redirectEvent = 'cookies' in args ? args.cookies : args.event;

const pageData: PageMessages = { form: args.form, ...expectedResult };

if (redirectTo) {
return redirect(redirectTo, pageData, redirectEvent);
console.log({ redirectTo });
return redirect(redirectTo, pageData, args.event);
}

return redirect(pageData, redirectEvent);
return redirect(pageData, args.event);
};

if (args.form) {
Expand Down
18 changes: 14 additions & 4 deletions client/src/routes/(auth)/forgot_password/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ export const load = (async ({ url, locals: { tsrest }, setHeaders }) => {
}) satisfies PageServerLoad;

export const actions = {
requestPasswordReset: async ({ request, locals: { tsrest }, cookies }) => {
requestPasswordReset: async (event) => {
const {
request,
locals: { tsrest },
} = event;

const form = await superValidate(request, zod(requestPasswordSchema));

return assertTsRestActionResultOK({
form,
cookies,
event,
result: () => tsrest.auth.forgotPasswordRequest({ query: form.data }),
onValid: () => ({
layoutAlert: createLayoutAlert({
Expand All @@ -64,12 +69,17 @@ export const actions = {
}),
});
},
resetPassword: async ({ request, locals: { tsrest }, cookies }) => {
resetPassword: async (event) => {
const {
request,
locals: { tsrest },
} = event;

const form = await superValidate(request, zod(resetPasswordSchema));

return assertTsRestActionResultOK({
form,
cookies,
event,
result: () => tsrest.auth.resetPassword({ body: form.data }),
onValid: () => ({
redirectTo: '/login',
Expand Down
10 changes: 8 additions & 2 deletions client/src/routes/(auth)/login/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ export const load = (async ({ url, locals: { sessionUser } }) => {
}) satisfies PageServerLoad;

export const actions = {
default: async ({ request, url, locals: { tsrest }, cookies }) => {
default: async (event) => {
const {
request,
url,
locals: { tsrest },
} = event;

const form = await superValidate(request, zod(schema));

return assertTsRestActionResultOK({
form,
cookies,
event,
result: () => tsrest.auth.login({ body: form.data }),
onValid: () => ({ redirectTo: getRedirectTo(url) ?? '/' }),
layoutAlert: {},
Expand Down
9 changes: 7 additions & 2 deletions client/src/routes/(auth)/register/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ export const load = (async ({ url, locals: { tsrest } }) => {
}) satisfies PageServerLoad;

export const actions = {
default: async ({ request, locals: { tsrest }, cookies }) => {
default: async (event) => {
const {
request,
locals: { tsrest },
} = event;

const form = await superValidate(request, zod(registerSchema));

const { registerToken, password, ...user } = form.data;

return assertTsRestActionResultOK({
form,
cookies,
event,
result: () => tsrest.auth.register({ body: { registerToken, password, user } }),
onValid: () => ({ redirectTo: '/' }),
});
Expand Down
9 changes: 7 additions & 2 deletions client/src/routes/(user)/settings/experience/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ export const load = (async ({ locals: { sessionUser, browserLang } }) => {
}) satisfies PageServerLoad;

export const actions = {
default: async ({ request, locals: { tsrest }, cookies }) => {
default: async (event) => {
const {
request,
locals: { tsrest },
} = event;

const form = await superValidate(request, zod(langSchema));

const lang = locales.includes(form.data.lang as string) ? form.data.lang : null;

return assertTsRestActionResultOK({
form,
cookies,
event,
result: () => {
return tsrest.user.settings.profile.update({
body: { lang },
Expand Down
26 changes: 20 additions & 6 deletions client/src/routes/(user)/settings/profile/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,26 @@ export const load = (async ({ locals: { sessionUser } }) => {
}) satisfies PageServerLoad;

export const actions = {
basicUserInfo: async ({ request, locals: { tsrest }, cookies }) => {
basicUserInfo: async (event) => {
const {
request,
locals: { tsrest },
} = event;

const form = await superValidate(request, zod(userFormSchema));

return assertTsRestActionResultOK({
form,
cookies,
event,
result: () => tsrest.user.settings.profile.update({ body: form.data }),
});
},
profilePicture: async ({ request, locals: { tsrest }, cookies }) => {
profilePicture: async (event) => {
const {
request,
locals: { tsrest },
} = event;

const formData = await request.formData();

const profilePictureFile = formData.get('profile-picture');
Expand All @@ -38,13 +48,17 @@ export const actions = {
}

return assertTsRestActionResultOK({
cookies,
event,
result: () => tsrest.user.settings.profile.uploadPicture({ body: formData }),
});
},
deleteProfilePicture: async ({ locals: { tsrest }, cookies }) => {
deleteProfilePicture: async (event) => {
const {
locals: { tsrest },
} = event;

return assertTsRestActionResultOK({
cookies,
event,
result: () => tsrest.user.settings.profile.deletePicture(),
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ export const load = (async (event) => {
}) satisfies PageServerLoad;

export const actions = {
default: async ({ request, locals: { tsrest }, cookies }) => {
default: async (event) => {
const {
request,
locals: { tsrest },
} = event;

const form = await superValidate(request, zod(emailChangeSchema));

return assertTsRestActionResultOK({
form,
cookies,
event,
result: () => tsrest.user.settings.profile.requestUpdateEmail({ body: form.data }),
onNotOk: (result, { errorMessage }) => {
if (result.status == StatusCodes.FORBIDDEN) {
Expand Down
9 changes: 7 additions & 2 deletions client/src/routes/(user)/settings/security/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ export const load = (async () => {
}) satisfies PageServerLoad;

export const actions = {
default: async ({ request, locals: { tsrest }, cookies }) => {
default: async (event) => {
const {
request,
locals: { tsrest },
} = event;

const form = await superValidate(request, zod(newPasswordFormSchema));

return assertTsRestActionResultOK({
form,
cookies,
event,
result: () => tsrest.user.settings.security.changePassword({ body: form.data }),
onValid: () => ({
form,
Expand Down
8 changes: 6 additions & 2 deletions client/src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import { StatusCodes } from 'http-status-codes';
import type { Actions } from './$types';

export const actions = {
async logout({ locals: { tsrest }, cookies }) {
async logout(event) {
const {
locals: { tsrest },
} = event;

return assertTsRestActionResultOK({
cookies,
event,
layoutAlert: {},
result: () => tsrest.auth.logout(),
onValid: () => ({ redirectTo: '/login' }),
Expand Down
11 changes: 8 additions & 3 deletions client/src/routes/admin/users/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ export const load = (async ({ locals: { tsrest } }) => {
}) satisfies PageServerLoad;

export const actions = {
delete: async ({ request, locals: { tsrest }, cookies }) => {
delete: async (event) => {
const {
request,
locals: { tsrest },
} = event;

const formdata = await request.formData();

const email = await emailSchema.parseAsync(formdata.get('email')).catch(() => {
Expand All @@ -47,12 +52,12 @@ export const actions = {
extraData: 'admin.users.actions.delete.errors.missing-email.details' satisfies I18nKey,
}),
},
cookies,
event,
);
});

return assertTsRestActionResultOK({
cookies,
event,
result: () => tsrest.users.admin.deleteUser({ body: { email } }),
onValid: () => ({
toasts: createToasts({
Expand Down
10 changes: 8 additions & 2 deletions client/src/routes/admin/users/[email]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ export const load = (async ({ params: { email }, locals: { tsrest } }) => {
}) satisfies PageServerLoad;

export const actions = {
default: async ({ request, locals: { tsrest }, params: { email: editableUserEmail }, cookies }) => {
default: async (event) => {
const {
request,
locals: { tsrest },
params: { email: editableUserEmail },
} = event;

const form = await superValidate(request, zod(adminUserFormSchema));

return assertTsRestActionResultOK({
form,
cookies,
event,
result: () => {
return tsrest.users.admin.editUser({
body: {
Expand Down
9 changes: 7 additions & 2 deletions client/src/routes/admin/users/new/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ export const load = (async ({ locals: { tsrest } }) => {
}) satisfies PageServerLoad;

export const actions = {
default: async ({ request, locals: { tsrest }, cookies }) => {
default: async (event) => {
const {
request,
locals: { tsrest },
} = event;

const form = await superValidate(request, zod(adminNewUserFormSchema));

return assertTsRestActionResultOK({
form,
cookies,
event,
result: () => {
const { email, firstName, lastName, roles, emailLang } = form.data;

Expand Down

3 comments on commit de63393

@ved-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 This commit is now deployed to Vercel!

fullstacked

https://fullstacked-rd3bdt9ya-v-ed.vercel.app

@ved-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 This commit is now deployed to Vercel!

fullstacked

https://fullstacked-8c8cdfph9-v-ed.vercel.app

@ved-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 This commit is now deployed to Vercel!

fullstacked

https://fullstacked-754o9na0f-v-ed.vercel.app

Please sign in to comment.