So im new to nextjs i have login page setup the problem is after a while if i log out and then do login again, the request is stuck on pending like this picture
but if i restart my browser and do login again it works, and after a few hours this behavior will happen again. So I needed to restart my browser every few minutes/hours
i dont know what is wrong, is it my local machine or the API, or the nextjs itself.
BTW I use PocketBase for authentication.
this is the snippet code for login page
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;
}
}