you can ask chatgpt kkkkChào mọi người,
Tôi có một nút Đăng nhập sẽ xuất hiện trên mỗi trang của trang web.
Tuy nhiên, sau khi khách truy cập cung cấp thông tin đăng nhập chính xác, nút Đăng nhập sẽ biến mất. Tôi phải làm thế nào để thực hiện điều đó?
js
// Express example
app.get('/some-page', (req, res) => {
const isLoggedIn = checkSession(req); // reads cookie, validates session
res.render('page', { isLoggedIn });
});
<!-- template -->
{{#unless isLoggedIn}}
<button id="login-btn">Log In</button>
{{/unless}}
jsx
function Header() {
const [isLoggedIn, setIsLoggedIn] = useState(false);
useEffect(() => {
// check with the server whether the current token/cookie is valid
fetch('/api/me', { credentials: 'include' })
.then(res => setIsLoggedIn(res.ok));
}, []);
return (
<header>
{!isLoggedIn && <button onClick={handleLogin}>Log In</button>}
</header>
);
}
async function updateHeader() {
const res = await fetch('/api/me', { credentials: 'include' });
document.getElementById('login-btn').style.display = res.ok ? 'none' : 'inline-block';
}
updateHeader();
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.