Back

Trouble Authenticating users with OAuth in Next.JS

  • 0
  • Users
  • Accounts
  • Web
  • Cloud
falconZ
27 May, 2023, 03:59

I've been facing troubles trying to authenticate with OAuth (GitHub and Gitlab). I dont know what i am missing so any help would be appreciated and it would be really helpful if someone could provide an example

  • When trying to authenticate with Github I either get this error ''Error 400 Invalid success: URL host must be one of: localhost, cloud.appwrite.io, *.vercel.app" or when i leave out the success parameter to be empty "Missing Redirect URL"
  • With Gitlab iI get directed to appwrite cloud which throws a 404 and takes me to my appwrite dashboard instead of taking me to the Gitlab's OAuth page.
TypeScript
"use client";

import { account } from "@falcon-z/app/utils/appwrite";

export default function LoginButton({
  provider,
}: {
  provider: "github" | "gitlab";
}) {
  const handleAuth = (provider: "github" | "gitlab") => {
    account.createOAuth2Session(provider, "localhost:3000/");
  };

  return (
    <button
      type="button"
      onClick={() => handleAuth(provider)}
    >
      Login With <span className="first-letter:uppercase">{provider}</span>
    </button>
  );
}
TL;DR
The user is having trouble authenticating users with OAuth in Next.JS. They are experiencing JSON decoding issues and are getting errors when trying to authenticate with GitHub and Gitlab. They have checked their client ID and secret and made sure everything is configured correctly. The solution for the GitHub issue is to include a valid success URL host, such as localhost. For the Gitlab issue, they need to make sure they have the correct Gitlab endpoint URL and include "http://" in the URL.
Drake
27 May, 2023, 04:05

That url you have is not a url. It's missing http://

Drake
27 May, 2023, 04:09

What's the URL you put in gitlab?

falconZ
27 May, 2023, 04:09

Thanks, it works for Github

falconZ
27 May, 2023, 04:23

On Gitlab you could find my Appwrite setup on the image above i did copy the ID and secret properly from GItlab double checked them. I dont understand what the endpoints field is so i tried leaving it blank filled in with some GItlab APi endpoints no matter what when i click on login dashboards from my app i am being taken to Appwrite's cloud displaying a 404 page not found which immediately redirects to my Appwrite dashboard

falconZ
27 May, 2023, 04:24

its the same Localhost URL for gitlab on my code only the provider changes depending on the props passed in to the component

Drake
27 May, 2023, 06:39

No I mean the redirect URL that you put in gitlab

Drake
27 May, 2023, 06:39

The endpoint should be gitlabs endpoint. If youre using gitlab cloud, it should be left blank

falconZ
27 May, 2023, 06:40

I used the URI shown on appwrite console when setting up on the pic

Drake
27 May, 2023, 06:41

K the problem is probably the endpoint you put

falconZ
27 May, 2023, 06:42

i guess so , i actually dont use gitlab much and i just wanted to provide an option for users to login with gitlab . I tried leaving endpoints field empty and the same thing happened

Drake
27 May, 2023, 06:43

Try putting https://gitlab.com

Drake
27 May, 2023, 06:45

I actually don't know gitlab's url...do you?

Drake
27 May, 2023, 06:46

Actually that's probably right. For some reason, I thought it was gitlab.io

falconZ
27 May, 2023, 06:47

😀 that works thanks

falconZ
27 May, 2023, 06:49

i get this error though after authorizing on gitLab "Cannot assign null to property Appwrite\Auth\OAuth2\Gitlab::$user of type array"

falconZ
27 May, 2023, 06:50

you got it right its gitlab.com

Drake
27 May, 2023, 06:57

Unable to get the user for some reason. You sure everything is configured correctly between the two? Like the client Id and secret?

falconZ
27 May, 2023, 07:03

yeah i think so, ill stry configuring things again to be sure

falconZ
27 May, 2023, 07:08

i renewed client secret checked the ID and redirect URI everything seems to be set up right

Drake
27 May, 2023, 07:18
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