S
SolidJS10mo ago
ndom91

Passing data from `event.locals` to Frontend

Hey folks, I'm super new to solid/solid start, but I'm trying to implement some authentication and I have my user's session in event.locals and its available in Middleware / BE. But how do I access that event.locals data in the frontend? Having trouble finding anything online.
8 Replies
ndom91
ndom91OP10mo ago
To be clear, using beta.2
Birk Skyum
Birk Skyum10mo ago
Maybe this is how
import { getRequestEvent } from "solid-js/web";

const event = getRequestEvent();
import { getRequestEvent } from "solid-js/web";

const event = getRequestEvent();
ndom91
ndom91OP10mo ago
Ah that did it! Thanks ❤️
Atila
Atila10mo ago
It's important to note that SolidStart's middleware won't intercept client-side nav.
Birk Skyum
Birk Skyum10mo ago
@Atila , what is the implication of that? Does getRequestEvent only give the first event in a potential series?
Atila
Atila10mo ago
if you try to protect a route via a middleware, it won't work for soft-navigation (SPA)
Does getRequestEvent only give the first event in a potential series?
that's a second case where, at the moment, the won't match assets - so it doesn't fire a million times, no need to filter routes out. But it intercepts every flight between client/server - so if you fire a redirect or something, it will fire multiple times. I think for protecting a route, the best case would be to add the logic in a parent route preload function.
protected/
├── private
└── etc...
public/
├── login
└── etc...
protected/
├── private
└── etc...
public/
├── login
└── etc...
so /protected/* can redirect if user is not authorized
Birk Skyum
Birk Skyum10mo ago
Can't really wrap my head around it. I'll have to think about that.
Atila
Atila10mo ago
Particularly, I like Nuxt's approach, where the developer can define route-specific middleware and then do the protection. https://nuxt.com/docs/getting-started/routing#route-middleware I think there's a chance we get there, but I'm not sure how much work is required under the hood to going through middleware on every navigation.
Want results from more Discord servers?
Add your server