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:
2026-01-29 15:52:34 -05:00
parent d9ca3d5a65
commit 703daeb32f
2 changed files with 26 additions and 2 deletions
+24
View File
@@ -240,6 +240,30 @@ templ Index(loggedIn bool) {
}
<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>
</html>
}