nextjs 承诺不会触发后端,但在重新启动浏览器后可以工作

问题描述 投票:0回答:1

所以我是nextjs新手,我有登录页面设置,问题是一段时间后,如果我注销然后再次登录,请求会停留在待处理状态,就像这张图片
promise stuck on pending

但是如果我重新启动浏览器并再次登录,它就会起作用,几个小时后,这种行为将再次发生。所以我需要每隔几分钟/小时重新启动一次浏览器 我不知道出了什么问题,是我的本地机器还是 API,还是 nextjs 本身。
顺便说一句,我使用 PocketBase 进行身份验证。
这是登录页面的片段代码

login/page.tsx

export default function Login() {
  const [isLoading, setIsLoading] = useState(false);
  const [isAuthenticating, setIsAuthenticating] = useState(false);
  const form = useForm<LoginFormInputs>({
    resolver: zodResolver(loginSchema),
    defaultValues: {
      email: "",
      password: "",
    },
  });
  const route = useRouter();
  
  const onSubmit: SubmitHandler<LoginFormInputs> = async (data) => {
    setIsLoading(true);
    setIsAuthenticating(true); // Set authenticating state
    try {
      const goLogin = await login(data.email, data.password);
      if(isErrorResponse(goLogin)) {
        if(goLogin.code === 400) {
          toast({title: "Uh Oh!", description: "Invalid login credentials", variant: "destructive"}); 
        }
        if(goLogin.token) {
          toast({title: "Nice!", description: "Login successfully, redirecting.", variant: "success"}); 
          route.push('/dashboard');
        }
      }
    } catch (error) {
      console.error(error);
      return;
    } finally {
      setIsLoading(false);
      setIsAuthenticating(false);
    }
  };

  const handleLoginWithGoogle = async () => {
    setIsAuthenticating(true);
    try {
      await loginWithGoogle();
      toast({title: "Nice!", description: "Login successfully, redirecting.", variant: "success"}); 
      route.push('/dashboard');
    } catch (error) {
      return toast({title: "Nice!", description: "Login successfully, redirecting.", variant: "success"}); 
    } finally {
      setIsAuthenticating(false);
    }
  };
return (<div>....my_code<div/>)

// auth.ts

import pb from "./pocketbase";
import { userlog } from "./userlog";

export const login = async (email: string, password: string) => {
    try {
        pb.authStore.clear();
        const res = await pb.collection('users').authWithPassword(
            email,
            password,
        );
        if(!res.token) {
            return {"code": 400, "message": "Invalid login credentials","data": {}}
        }
        await userlog("login_success");
        return {"code": 200, "message": "Login successfully","token": res.token}
    } catch (error) {
        return {"code": 400, "message": "Invalid login credentials","data": {}}
    }
}

export const loginWithGoogle = async () => {
    try {
        pb.authStore.clear();
        const res = await pb.collection('users').authWithOAuth2({ provider: 'google' });
        await userlog("login_success");
        return res;
    } catch (error) {
        return error;
    }
}
javascript next.js13 pocketbase
1个回答
0
投票

好吧,问题显然在于如何处理此处的身份验证。首先,浏览器重新启动会有所帮助,这表明存储(浏览器缓存、本地存储、会话存储等)会被清除,您可以再次使用系统。

现在,有几个选项可供考虑:

  1. 尝试以编程方式清除浏览器缓存和本地/会话存储。
  2. PocketBase 可能会保留陈旧的令牌,从而导致身份验证问题。也尝试以编程方式清除它。
  3. 如果正在使用 cookie,请检查网络请求,看看您是否没有使用 cookie 发送错误信息。

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.