Now.js Framework Documentation
RedirectManager
RedirectManager
Overview
RedirectManager is the redirect management system in Now.js Framework. It supports auth redirects, intended URL storage, and configurable redirect types.
When to use:
- Need redirect after login
- Need redirect for unauthorized access
- Need to store intended URL
- Need guest-only routes
Why use it:
- ✅ Pre-defined redirect types
- ✅ Intended route storage
- ✅ Query/hash preservation
- ✅ Notification integration
- ✅ SPA and full-page redirect support
Basic Usage
Redirect Types
// Auth required redirect
RedirectManager.requireAuth();
// After login
RedirectManager.afterLogin();
// After logout
RedirectManager.afterLogout();
// Forbidden (403)
RedirectManager.forbidden();
// Guest only (redirect logged-in users)
RedirectManager.guestOnly();Custom Redirect
RedirectManager.redirect('auth_required', {
target: '/custom-login',
message: 'Please login to continue'
});Redirect Types
| Type | Default Target | Description |
|---|---|---|
auth_required |
/login |
User must login |
forbidden |
/403 |
No permission |
after_login |
/ or intended |
After successful login |
after_logout |
/ |
After logout |
guest_only |
/dashboard |
Guest only |
maintenance |
/maintenance |
System maintenance |
not_found |
/404 |
Page not found |
Default Configurations
RedirectManager.defaults = {
auth_required: {
target: '/login',
preserveQuery: true, // Keep query string
preserveHash: true, // Keep hash
storeIntended: true, // Store current URL
message: 'Please login to continue',
showNotification: true
},
after_login: {
target: '/',
useIntended: true, // Go to stored URL
clearIntended: true, // Clear after use
showNotification: false
},
// ... other types
};API Reference
RedirectManager.redirect(type, options)
Perform redirect
| Parameter | Type | Description |
|---|---|---|
type |
string | Redirect type |
options.target |
string | Override target URL |
options.message |
string | Override message |
options.params |
object | URL parameters |
RedirectManager.redirect('auth_required', {
target: '/login',
params: { returnTo: '/checkout' },
message: 'Please login to complete your order'
});RedirectManager.requireAuth(options?)
Redirect to login
// Route guard
if (!isAuthenticated) {
RedirectManager.requireAuth();
return;
}RedirectManager.afterLogin(options?)
Redirect after successful login
// After successful login
async function handleLogin(credentials) {
await AuthManager.login(credentials);
RedirectManager.afterLogin();
}RedirectManager.afterLogout(options?)
Redirect after logout
async function handleLogout() {
await AuthManager.logout();
RedirectManager.afterLogout();
}RedirectManager.forbidden(options?)
Redirect to 403
RedirectManager.guestOnly(options?)
Redirect logged-in users away
// Login page guard
if (isAuthenticated) {
RedirectManager.guestOnly();
return;
}Intended Route
Store the route user intended to visit:
storeIntendedRoute()
Save current URL
RedirectManager.storeIntendedRoute();getIntendedRoute()
Get stored URL
Returns: string|null
const intended = RedirectManager.getIntendedRoute();
// '/dashboard/settings'clearIntendedRoute()
Clear stored URL
RedirectManager.clearIntendedRoute();Events
// Before redirect
EventManager.on('redirect:before', (data) => {
console.log('Redirecting to:', data.targetUrl);
});
// After redirect (SPA mode)
EventManager.on('redirect:after', (data) => {
console.log('Redirected:', data.type);
});Integration Examples
Route Guard
// middleware/auth.js
function requireAuth(to, from, next) {
if (!AuthManager.isAuthenticated()) {
RedirectManager.redirect('auth_required', {
storeIntended: true
});
return;
}
next();
}Login Page
// pages/login.js
async function handleLoginSubmit(data) {
try {
await AuthManager.login(data);
// Redirect to intended or dashboard
RedirectManager.afterLogin({
fallbackTarget: '/dashboard'
});
} catch (error) {
showError(error.message);
}
}Protected API Call
async function fetchUserData() {
try {
return await ApiService.get('/api/user');
} catch (error) {
if (error.status === 401) {
RedirectManager.requireAuth({
message: 'Session expired, please login again'
});
}
throw error;
}
}Multi-Role Redirect
function redirectByRole(user) {
const roleTargets = {
admin: '/admin/dashboard',
manager: '/manager/reports',
user: '/user/home'
};
RedirectManager.redirect('after_login', {
target: roleTargets[user.role] || '/home'
});
}Common Pitfalls
⚠️ 1. Intended Route in SPA
// ❌ Intended route lost after refresh
// (uses sessionStorage instead of localStorage)
// ✅ Framework handles automatically
RedirectManager.requireAuth(); // stores in sessionStorage⚠️ 2. Query String Handling
// ❌ Query lost
redirect('/login');
// ✅ Preserve query
RedirectManager.redirect('auth_required', {
preserveQuery: true
});
// /login?returnTo=/current/pageRelated Documentation
- AuthManager - Authentication
- RouterManager - Routing