Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Swado opened this issue Aug 30, 2024 · 4 comments
Open
Labels
adapters Changes related to the core code concerning database adapters bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@Swado
Copy link

Swado commented Aug 30, 2024

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 Swado added adapters Changes related to the core code concerning database adapters bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Aug 30, 2024
@liamwh
Copy link
Contributor

liamwh commented Sep 25, 2024

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?

@dvanmali
Copy link

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.

@Swado
Copy link
Author

Swado commented Sep 25, 2024

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
Copy link
Author

Swado commented Sep 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters Changes related to the core code concerning database adapters bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

3 participants