Back

user_oauth2_unauthorized

  • 0
  • Self Hosted
  • Auth
  • Web
yankes
19 Oct, 2024, 09:08

I receive this errors when i try to login using Github

TypeScript
{
  "message": "Bad credentials: No description",
  "code": 401,
  "type": "user_oauth2_unauthorized",
  "version": "1.6.0"
}
TypeScript
{
  "message": "There was an error processing your request. Please check the inputs and try again.",
  "code": 400,
  "type": "general_bad_request",
  "version": "1.6.0"
}
TypeScript
// src/lib/server/oauth.js
"use server";

import { createAdminClient } from "./appwrite";
import { redirect } from "next/navigation";
import { headers } from "next/headers";
import { OAuthProvider } from "node-appwrite";

export async function signUpWithGithub() {
    const { account } = await createAdminClient();

  const origin = headers().get("origin");
  
    const redirectUrl = await account.createOAuth2Token(
        OAuthProvider.Github,
        `${origin}/oauth`,
        `${origin}/signup`,
    );

    return redirect(redirectUrl);
};
TypeScript
// src/app/oauth/route.js

import { AUTH_COOKIE } from "@/features/auth/constants";
import { createAdminClient } from "@/lib/appwrite";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";

export async function GET(request: NextRequest) {
  const userId = request.nextUrl.searchParams.get("userId");
  const secret = request.nextUrl.searchParams.get("secret");

    if (!userId || !secret) {
        return new NextResponse("Missing fields", { status: 400 });
    }

  const { account } = await createAdminClient();
  const session = await account.createSession(userId, secret);

  cookies().set(AUTH_COOKIE, session.secret, {
    path: "/",
    httpOnly: true,
    sameSite: "strict",
    secure: true,
  });

  return NextResponse.redirect(`${request.nextUrl.origin}/`);
}
TL;DR
Developers are experiencing user_oauth2_unauthorized errors when trying to log in using Github. The issue is likely due to bad credentials or input errors. Check the login process and inputs for mistakes. Verify that the OAuth configuration in `oauth.js` and user session creation in `route.js` are correctly set up. Make sure all necessary parameters are being passed correctly.
yankes
19 Oct, 2024, 09:08
yankes
19 Oct, 2024, 09:09
yankes
19 Oct, 2024, 09:09

user_oauth2_unauthorized

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