Appearance
Public Portal Login Redirect Middleware ​
File Information ​
- Path:
app/middleware/public-portal-login-redirect.ts - Purpose: Auto-login public portals when accessing login page
- Type: Per-Route Middleware (Client-Only)
Overview ​
The public-portal-login-redirect.ts middleware automatically logs in public portals when users access the login page. This removes the "redirect to login then immediately to home" flash by auto-logging in and redirecting to home before the login page renders.
Features ​
- Client-Only Execution: Runs only on client-side (not SSR)
- Public Portal Detection: Checks if workspace is a public portal
- Automatic Login: Auto-logs in public portal users
- Login Page Bypass: Redirects to home before login page renders
- Seamless UX: Eliminates redirect flash
Key Characteristics ​
Client-Only Middleware ​
This middleware is intentionally client-only because:
- Session is stored in HTTP-only cookies
- Cookies must be set via server API response
- Browser context is required for cookie handling
typescript
if (import.meta.server) return // Skip on serverLogin Page Specific ​
Only runs on login pages:
typescript
// Only runs when accessing login page
// Checks brand name from route paramsImplementation Details ​
Public Portal Check and Auto-Login ​
typescript
const { checkAndLoginPublicPortal } = usePublicPortal()
const result = await checkAndLoginPublicPortal(brandUrlSlug)
if (result.isPublicPortal && result.loggedIn) {
// Public portal and logged in successfully
// Redirect to home to skip login page
return navigateTo(`/${brandUrlSlug}`, { replace: true })
}Error Handling ​
Handles errors silently:
typescript
try {
const result = await checkAndLoginPublicPortal(brandUrlSlug)
// Handle result
} catch (error) {
console.error('[public-portal-login-redirect] failed:', error)
// Allow login page to render normally
}Usage ​
In Login Page ​
vue
<!-- app/pages/[brand_name]/login.vue -->
<script setup lang="ts">
definePageMeta({
middleware: 'public-portal-login-redirect'
})
</script>
<template>
<div>
<!-- Login form (only renders if not public portal) -->
</div>
</template>Execution Flow ​
1. Check if client-side → Skip if server
2. Check if authenticated → Skip if authenticated
3. Check if public portal → Continue if public portal
4. Auto-login public portal → Redirect to home if successful
5. Allow login page to render if not public portalBenefits ​
- Better UX: Eliminates redirect flash for public portals
- Seamless: Users don't see login page for public portals
- Automatic: No manual configuration needed
- Error Tolerant: Falls back gracefully on errors
Comparison with Public Portal Home Middleware ​
| Feature | public-portal-home | public-portal-login-redirect |
|---|---|---|
| Route | Brand home (/{brand_name}) | Login page (/{brand_name}/login) |
| Purpose | Auto-login on home | Auto-login on login page |
| Redirect | To login if not public | To home if public |
| Use Case | Home page access | Login page access |
Dependencies ​
usePublicPortal()- Public portal composableuseAuthStore()- Authentication storenavigateTo()- Nuxt navigation helper
Related Middleware ​
- Public Portal Home Middleware - Auto-login on home page
- Global Auth Middleware - Global authentication
- Redirect If Logged In Middleware - Redirect authenticated users
Related Documentation ​
- Auth Composables - usePublicPortal - Public portal composable
- Authentication System - Authentication feature
- Pages - Login - Login page
- Middleware Overview - All middleware