ciscoheat / sveltekit-flash-message

Send temporary data after redirect, usually from endpoints. Works with both SSR and client.
https://www.npmjs.com/package/sveltekit-flash-message
MIT License
268 stars 6 forks source link

Invalid URL in Non-Typescript Implementation #42

Open Blue-Davinci opened 5 months ago

Blue-Davinci commented 5 months ago

Hello Mr.Cisco, First of all thank you for the amazing library and I got pointed to it via a video from HuntaByte. Anyway the main issue is that am receiving an "Invalid URL" error which seems to stem from the redirect. Here is my current simple setup: "@sveltejs/adapter-auto": "^3.0.0", "@sveltejs/kit": "^2.0.0", "@sveltejs/vite-plugin-svelte": "^3.0.0", I am using a hook.server.js that reads a cookie and sets an event.locals My layout.server.js is also wrapped in the loadFlash as shown below:

`import { loadFlash } from 'sveltekit-flash-message/server';

export const load = loadFlash(async({locals}) =>{ let user = locals.user; console.log("[Layout.server.js ROOT] User:",user); return{ props: { user } } });`

After that I have setup a simple if and div in the main home layout.svelte, but the issue comes up in my login page.server.js, where am using the redirect to redirect and send a flash after a succesful login. Below is a snippet `import {fail} from '@sveltejs/kit'; import {z} from 'zod'; import { redirect } from 'sveltekit-flash-message/server';

login: async({fetch, request, url}) => {
              .....
          redirect('/', {type: 'success', message: 'Login Successful'});

} On the redirect it breaks and throws an invalid url, Not even using an absolute url like: http://localhost:5000/, still yields the same:TypeError: Invalid URL`

ciscoheat commented 5 months ago

Hi, thank you, glad you like the library! It looks like you're not supplying the RequestEvent or Cookies in your redirect. Try this:

login: async({fetch, request, url, cookies}) => {
    redirect('/', {type: 'success', message: 'Login Successful'}, cookies);
Blue-Davinci commented 5 months ago

Hi, thank you, glad you like the library! It looks like you're not supplying the RequestEvent or Cookies in your redirect. Try this:

login: async({fetch, request, url, cookies}) => {
    redirect('/', {type: 'success', message: 'Login Successful'}, cookies);

Hi, thank you, glad you like the library! It looks like you're not supplying the RequestEvent or Cookies in your redirect. Try this:

login: async({fetch, request, url, cookies}) => {
    redirect('/', {type: 'success', message: 'Login Successful'}, cookies);

Thank you sir, worked properly. Have a blessed day.

Blue-Davinci commented 5 months ago

I think my issue may now be similar to this: https://github.com/ciscoheat/sveltekit-flash-message/issues/38 as i am using the svelte cookie API without the append, as i cannot seem to get the flash message back after setting it.