feat: add homepage auto-redirect for logged-in users
- Add JavaScript to check for valid JWT token on homepage load - Auto-redirect to /bookshelf if user is already logged in - Shows login/register form if not authenticated - Improves UX by taking logged-in users directly to bookshelf Implementation: - Fetch /api/auth/profile with stored token - On success, redirect to /bookshelf - On failure, silently stay on homepage - Runs on DOMContentLoaded for fast execution
This commit is contained in:
@@ -240,6 +240,30 @@ templ Index(loggedIn bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
<script src="/static/theme.js"></script>
|
<script src="/static/theme.js"></script>
|
||||||
|
<script>
|
||||||
|
// Auto-redirect to bookshelf if user is logged in
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
|
if (token) {
|
||||||
|
// Validate token by checking profile
|
||||||
|
fetch('/api/auth/profile', {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
// Token is valid, redirect to bookshelf
|
||||||
|
window.location.href = '/bookshelf';
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
// Silently fail - user stays on homepage
|
||||||
|
console.log('Not logged in');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
}
|
}
|
||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user