surrealdb / surrealdb.js

SurrealDB SDK for JavaScript
https://surrealdb.com
Apache License 2.0
272 stars 45 forks source link

Bug: undefined when i use db.info #209

Closed SrWither closed 5 months ago

SrWither commented 5 months ago

Describe the bug

i'm trying to use db.info in the js sdk, but i'm getting undefined as a response

Steps to reproduce

my surrealql:

DEFINE TABLE Posts SCHEMAFULL
    PERMISSIONS
        FOR create, update, delete WHERE user = $auth.id;

DEFINE FIELD title ON TABLE Posts TYPE string;
DEFINE FIELD content ON TABLE Posts TYPE string;
DEFINE FIELD description ON TABLE Posts TYPE string;
DEFINE FIELD user ON TABLE Posts TYPE record(Users) DEFAULT $auth.id;

DEFINE TABLE Users SCHEMAFULL
    PERMISSIONS
        FOR select, update, delete WHERE id = $auth.id;

DEFINE FIELD username ON TABLE Users TYPE string;
DEFINE FIELD email ON TABLE Users TYPE string
    ASSERT string::is::email($value);
DEFINE FIELD password ON TABLE Users TYPE string;

DEFINE INDEX emailUnique ON TABLE Users COLUMNS email UNIQUE;

DEFINE SCOPE Auth
    SESSION 3d

    SIGNUP (
      INSERT INTO User
      (username, email, password)
      VALUES
      ($username, $email, crypto::argon2::generate($password))
    )

    SIGNIN (
      SELECT * FROM User WHERE
      email = $email
      AND crypto::argon2::compare(password, $password)
    )
;

my typescript code: connect.ts

import { Surreal } from 'surrealdb.js';

export const db = new Surreal();

export const connectDb = async () => {
  try {
    await db.connect('http://127.0.0.1:8000/rpc', {
      namespace: 'blog',
      database: 'blog',
    });
  } catch (e) {
    console.error(e);
  }
};

user.ts

import { db } from './connect';

export type User = {
  id: string;
  username: string;
  email: string;
  password: string;
};

export const getMyUser = async (token: string): Promise<User | null> => {
  try {
    const isAuthenticated = await db.authenticate(token);
    if (!isAuthenticated) {
      return null;
    }

    const user = await db.info<User>();
    console.log(user);

    if (!user) {
      return null;
    }

    return user;
  } catch (e) {
    console.error(e);
    return null;
  }
};

db.authenticate returns true but still db.info returns undefined If I use db.signin or db.signup in the Auth scope the same thing happens

Expected behaviour

get the authenticated user data instead of an undefined

SurrealDB version

surreal 1.1.1 for linux on amd64

SurrealDB.js version

0.11.0

Contact Details

fjachuf@gmail.com

Is there an existing issue for this?

Code of Conduct

oskar-gmerek commented 5 months ago

Check your Select access permission on the User table. I guess there is a problem.

SrWither commented 5 months ago

It seems that the error was in the scope, it was pointing to a table that did not exist