Skip to content
Back

oAuth Problem with session (Google)

  • 0
  • Web
TrxsTer
29 Oct, 2024, 10:08

I'm currently working on a project using Next.js and have set up Google OAuth authentication. The consent screen flow is functioning correctly, and it redirects users to the target redirect URI, /success. However, I'm not receiving the expected session data, and I'm unsure what's going wrong. I've looked through similar issues in the threads and saw a suggestion to enable the allow third-party cookie setting, which I have done, but the issue persists. Although user data is available in the Appwrite console, I'm not getting the results I expected.

TypeScript
await account.createOAuth2Session(
  OAuthProvider.Google,
  redirectUri,
  failureUri,
  ["email", "profile", "openid"]
);
} catch (error) {
  console.error("OAuth session creation failed:", error);
  toast({
    title: "Oh no!",
    description: "Something went wrong! Please try again later.",
    variant: "destructive",
  });
}
TL;DR
Problem with Google OAuth authentication in Next.js project. Redirect to /success but not receiving expected session data. Tried enabling 3rd party cookies. Suggested solution: for production, use custom domains. Follow SSR guide if applicable. If still facing issues, check for errors in OAuth session creation.
Sen
30 Oct, 2024, 04:53

am facing the same issue

Kenny
30 Oct, 2024, 13:41

What information are you not getting that you were expecting to get?

Steven
30 Oct, 2024, 18:21

If you're using SSR, you should probably follow this: https://appwrite.io/docs/products/auth/server-side-rendering#oauth2

TrxsTer
5 Nov, 2024, 05:09

It is not SSR and this is the message I am getting in the console log in the /success page

TrxsTer
5 Nov, 2024, 05:16
TypeScript
/success.tsx

  useEffect(() => {
    const handleSuccess = async () => {
      try {
        const session = await account.getSession("current");
        if (session) {
          const user = await account.get();
          dispatch(login());
          toast({
            title: "Login successful!",
            description: `Welcome, ${user.name}!`,
            variant: "success",
            duration: 2000,
          });
          router.push("/user-profile");
        } else {
          throw new Error("No session found");
        }
      } catch (error) {
        console.error("Auth success error:", error);
        toast({
          title: "Login failed!",
          description: "Something went wrong. Please try again.",
          variant: "destructive",
          duration: 2000,
        });
        router.push("/sign-in");
      } finally {
        setIsLoading(false);
      }
    };
KINGKabir👑
13 Nov, 2024, 22:01

did it solve?

Steven
13 Nov, 2024, 22:57

Then, it's probably a 3rd party cookie problem.

For local development, maybe you can change your browser settings to enable 3rd party cookies.

In production, you'll need to use custom domains

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more