【发布时间】:2023-01-04 22:13:12
【问题描述】:
我有一个 NextJS 应用程序,它有时无法按预期工作。
当我的连接速度较慢且网站的首次加载时间比正常情况下长时,当我尝试登录应用程序时,会执行 HTML 表单的默认行为,并且我插入的凭据会显示在 URL 上,甚至虽然我在提交函数中有一个event.preventDefault(),但我没有使用 GET。
我已经尝试提高应用程序的性能并减少页面的首次加载,但是,如果用户可以让加载时间变慢,它就可以被利用。
我只想防止凭据显示在 URL 上。 它可以替换为任何类型的其他错误。
这是我的代码:
async function handleLogin(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setIsLoadingLogin(true);
setError('');
const captchaValue = await captchaRef.current?.executeAsync();
if (!captchaValue) {
setError('Erro de captcha. Tente novamente mais tarde.');
return setIsLoadingLogin(false);
}
try {
const { access, refresh } = await loginService({
email,
password,
captcha_value: captchaValue,
});
setCookie(undefined, cookieNames.userAccessToken, access);
setCookie(undefined, cookieNames.userRefreshToken, refresh);
await router.push('/home');
} catch (error: any) {
if (error.response.status === 500) return setError('Erro no servidor.');
if (error.response.data.detail) return setError(error.response.data.detail);
} finally {
setIsLoadingLogin(false);
setPassword('');
captchaRef.current?.reset();
}
}
<form onSubmit={handleLogin}>
...
</form>
【问题讨论】:
-
使用 POST 方法而不是 GET
-
我已经在使用 POST,这里的问题是函数没有执行。
-
你能分享你的代码吗?也许
event.preventDefault()有时没有执行,因为在到达语句之前发生异常?
标签: javascript performance authentication security next.js