Appearance
public-portal-search Middleware ​
File Information ​
- Path:
app/middleware/public-portal-search.ts - Purpose: Guard for unauthenticated visitors on
/:brand_name/search; if the workspace is a public portal, auto-login so search results can load; otherwise redirect to login - Type: Per-route middleware (client-only behavior)
Overview ​
When a user lands on /:brand_name/search without being authenticated, the middleware checks whether the workspace (identified by brand_name) is a public portal. If it is, it attempts to auto-login via usePublicPortal().checkAndLoginPublicPortal(brandUrlSlug) so that search results can load. If the workspace is not a public portal, or auto-login fails, the user is redirected to /:brand_name/login with a redirect query parameter so they can log in and return to the search page.
Behavior ​
- Client-only: The middleware returns immediately on the server (
import.meta.server); session and login checks rely on HTTP-only cookies and client-side API calls. - Route: Runs only when the path is exactly
/${brand_name}/search(andbrand_nameparam is present). - Authenticated users: If the auth store indicates the user is authenticated, or
/api/auth/sessionreturnsauthenticated: true, the middleware does nothing and allows the request to proceed. - Public portal: Calls
checkAndLoginPublicPortal(brandUrlSlug)from usePublicPortal. IfisPublicPortalis false orloggedInis false after the call, redirects to login withredirectset to the current full path. - Errors: On failure of the public portal check, redirects to login.
Usage ​
Apply this middleware to the search page route (e.g. via definePageMeta) so that unauthenticated access to the search page is either allowed (after public portal auto-login) or redirected to login.
Related ​
- usePublicPortal - checkAndLoginPublicPortal
- Middleware - public-portal-home - Public portal home page handling
- Server API - session - Session check used by middleware