-
-
Notifications
You must be signed in to change notification settings - Fork 432
Support server side goto? #83
Comments
Hi @neonquill! Since app.get('/', function(req, res, next) {
const isNotLoggedIn = req.user === undefined;
if (isNotLoggedIn) {
res.redirect('/login');
} else {
next();
}
});
app.use(sapper()); |
@EmilTholin - The biggest challenge with that approach is that I basically want every single page to redirect the user to the login page, not just '/'. If I attempt to wildcard the route too broadly I end up catching lots of files I have no intention of redirecting. I guess sapper should have a list of all the valid routes somewhere that I could pull out and pass to express. The other issue with this approach is that it still requires implementing the redirect independently on the client and the server. Not really a big deal, just one more place for me to make mistakes. As an aside, I was paying closer attention to the other open issues, and it feels like a fix to #75 would also solve my problem. If I could return a redirect from Regardless, I'm happy with the answer that sapper wasn't designed to support calling |
I was just trying the exact same thing as @neonquill (great minds think alike). Handling it as another Express middleware would not have been an obvious idea for me. I think there's a mindset that needs to be painted here. The lines are a bit blurry. |
Maybe we could solve this by adding a context to preload with methods like import * as auth from './_auth.js';
export default {
preload: ({ params, session, query }) => {
// if `session` exists, we're on the server
const user = session ? session.user : auth.user;
// if we're not logged in, redirect
if (!user) {
this.redirect('/login');
}
else if (isInvalidForSomeReason(params.whatever)) {
this.error(404, `Could not find ${params.whatever}`);
}
else {
return fetch(`/api/things/${whatever}`).then(r => r.json()).then(result => ({ thing: result }));
}
}
}; |
Yeah that could work! |
That's how it should be done. |
implement this.redirect in preload
I'm running into problems trying to implement a login system where the user gets automatically redirected to a login page when first loading the site. For example:
This pattern works fine as long as
preload()
runs on the client. If this is the first load and this code gets run on the server then I get a "URL is not defined" error inside thegoto()
implementation.Looking at the code it seems like
goto()
was never designed to be used server side. I was wondering if I'm missing something fundamental about the sapper architecture and I should be using a completely different pattern to approach this, or if this is just a limitation of the current implementation.The text was updated successfully, but these errors were encountered: