next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

Surrealdb adapter when using with google provider thows error at surrealId.split

Open Swado opened this issue 1 year ago • 4 comments

Adapter type

@auth/surrealdb-adapter

Environment

"next": "14.2.4",
"next-auth": "^5.0.0-beta.20",
"@auth/surrealdb-adapter": "^1.4.2",
"surrealdb.js": "^1.0.0-beta.9",

Reproduction URL

https://github.com/Swado/testmyst

Describe the issue

I was trying surrealdb adapter to login with google for my app. I followed the guide setup for oauth. When finally signed in with google i am being redirected to /api/auth/[...nextauth] error is being thrown server configuration not right.

Screenshot 2024-08-30 at 5 53 02 PM

image

How to reproduce

Try Surrealdb adapter setup for next js with google as provider.

auth.ts

import NextAuth, { type NextAuthConfig } from "next-auth";
import { SurrealDBAdapter } from "@auth/surrealdb-adapter";
import GoogleProvider from "next-auth/providers/google";
import { surrealConnection, surrealDatabase } from "./utils/surreal/surreal";
import Surreal from "surrealdb.js";

const namespace = process.env.NEXT_PUBLIC_NAMESPACE as string;
const database = process.env.NEXT_PUBLIC_DB_NAME as string;

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: SurrealDBAdapter<Surreal>(surrealConnection()),
  providers: [
    GoogleProvider({
      clientId: process.env.AUTH_GOOGLE_ID!,
      clientSecret: process.env.AUTH_GOOGLE_SECRET!,
    }),
  ],
  session: { strategy: "jwt" },
  callbacks: {
    async jwt({ token, account, profile }) {
      if (profile) {
        console.log({ profile });
        token.googleSub = profile.sub;

        try {
          await surrealConnection();

          await surrealDatabase.signin({
            namespace,
            database,
            scope: "users",
            sub: profile.sub,
            email: token.email,
          });
        } catch {
          // otherwise sign up
          await surrealDatabase.signup({
            namespace,
            database,
            scope: "users",
            sub: profile.sub,
            name: token.name,
            email: token.email,
            picture: token.picture,
          });
        }
      }

      return token;
    },
  },
});

surreal.ts

import { Surreal } from "surrealdb.js";

const connectionString = process.env.NEXT_PUBLIC_DB_CONNECTION_URL as string;
const username = process.env.NEXT_PUBLIC_DB_USER as string;
const password = process.env.NEXT_PUBLIC_DB_PASSWORD as string;
const namespace = process.env.NEXT_PUBLIC_NAMESPACE as string;
const database = process.env.NEXT_PUBLIC_DB_NAME as string;

export const surrealDatabase = new Surreal();

export const surrealConnection = () =>
  new Promise<Surreal>(async (resolve, reject) => {
    try {
      await surrealDatabase.connect(`${connectionString}/rpc`, {
        namespace,
        database,
        auth: { username, password },
      });
      resolve(surrealDatabase);
    } catch (e) {
      console.log("here1");
      console.log({ e });
      reject(e);
    }
  });

Expected behavior

Should signin without error message.

Swado avatar Aug 30 '24 12:08 Swado

I believe is because Auth.js is using surrealdb.js instead of it's successor, surrealdb. The adapter should be updated to use the latest package from surreal. Did you have any success working through this @Swado?

liamwh avatar Sep 25 '24 14:09 liamwh

The connection string needs to start with ws or wss and not use /rpc at the end in order to use this version of surrealdb.js package. Also the package should change from surrealdb.js to surrealdb@^1.0.0.

dvanmali avatar Sep 25 '24 16:09 dvanmali

I believe is because Auth.js is using surrealdb.js instead of it's successor, surrealdb. The adapter should be updated to use the latest package from surreal. Did you have any success working through this @Swado?

Actually i commented the adapter field in NextAuth. I studied the adapter code it just facilitates recurring methods liking getting user data and signin details so i just wrote my own logic.🤡 @liamwh

Swado avatar Sep 25 '24 16:09 Swado

The connection string needs to start with ws or wss and not use /rpc at the end in order to use this version of surrealdb.js package. Also the package should change from surrealdb.js to surrealdb@^1.0.0.

Will try it out. Thanks for suggesting. @dvanmali

Swado avatar Sep 25 '24 16:09 Swado