surrealdb / surrealdb.js

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

Authentication Error in NodeJS Integration #52

Closed JaiSh12345 closed 2 years ago

JaiSh12345 commented 2 years ago

I have recently come across SurrealDB, and installed it.

After adding to path, I started the server using surreal start --log trace --user root --pass root


 .d8888b.                                             888 8888888b.  888888b.
d88P  Y88b                                            888 888  'Y88b 888  '88b
Y88b.                                                 888 888    888 888  .88P
 'Y888b.   888  888 888d888 888d888  .d88b.   8888b.  888 888    888 8888888K.
    'Y88b. 888  888 888P'   888P'   d8P  Y8b     '88b 888 888    888 888  'Y88b
      '888 888  888 888     888     88888888 .d888888 888 888    888 888    888
Y88b  d88P Y88b 888 888     888     Y8b.     888  888 888 888  .d88P 888   d88P
 'Y8888P'   'Y88888 888     888      'Y8888  'Y888888 888 8888888P'  8888888P'

[2022-09-27 17:37:44] INFO  surrealdb::iam Root authentication is enabled
[2022-09-27 17:37:44] INFO  surrealdb::iam Root username is 'root'
[2022-09-27 17:37:44] INFO  surrealdb::dbs Database strict mode is disabled
[2022-09-27 17:37:44] INFO  surrealdb::kvs Starting kvs store in memory
[2022-09-27 17:37:44] INFO  surrealdb::kvs Started kvs store in memory
[2022-09-27 17:37:44] INFO  surrealdb::net Starting web server on 0.0.0.0:8000
[2022-09-27 17:37:44] INFO  surrealdb::net Started web server on 0.0.0.0:8000

In my NodeJS app, I have the following code (adapted from your docs):

import Surreal from 'surrealdb.js';
const db = new Surreal('http://localhost:8000/rpc');

async function main() {
    try {

        // Signin as a namespace, database, or root user
        await db.signin({
            user: 'root',
            pass: 'root',
                    NS: 'practice',
                    DB: 'buybig'
        });
            console.log('y');

        // Select a specific namespace / database
        // await db.use('practice', 'buybig');

        console.log(await db.select('users'));

    } catch (e) {
        console.error('ERROR', e);
    }
}

main();

I am getting this error:

ERROR AuthenticationError: There was a problem with authentication
    at Surreal._Surreal_signin (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/index.js:416:23)    at Surreal.<anonymous> (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/index.js:225:111)   
    at Surreal.f (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:28:18)     
    at file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:34:22
    at Array.forEach (<anonymous>)
    at Surreal.emit (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:33:67)  
    at Socket.<anonymous> (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/index.js:126:29)     
    at file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:34:22
    at Array.forEach (<anonymous>)
    at Socket.emit (file:///C:/Users/jaide/OneDrive/Documents/Projects/HTMLProject/buybig/node_modules/surrealdb.js/esm/classes/emitter.js:33:67)   
^C

And this in my SurrealDB logs:

[2022-09-27 18:06:04] INFO  surreal::web 127.0.0.1:64675 GET /rpc HTTP/1.1 101 "-" 68.7µs

Accessing database through VSCode's Thunder Client and SurrealDB cli tool works flawlessly.

Any help is appreciated.

Environment:

tobiemh commented 2 years ago

Hi @JaiSh12345 to signin as a 'root' user don't specify a NS or DB in the signin() method...

import Surreal from 'surrealdb.js';
const db = new Surreal('http://localhost:8000/rpc');

async function main() {
    try {

        // Signin as a root user
        await db.signin({
            user: 'root',
            pass: 'root',
        });
            console.log('y');

        // Select a specific namespace / database
        await db.use('practice', 'buybig');

        console.log(await db.select('users'));

    } catch (e) {
        console.error('ERROR', e);
    }
}

main();
JaiSh12345 commented 2 years ago

Okay! I don't know why I added NS and DB. I suppose I was playing around with it. Thanks!

JaiSh12345 commented 2 years ago

Hmmm, but it's not ending the program correctly. I get:

C:\Users ... > node app.js
y
[ { email: 'john@gmail.com', id: 'users:john', pass: 'pass' } ]
^C

And it just hangs after the console log.

mathe42 commented 2 years ago

You have to close the client! The DB connection is still alive!

JaiSh12345 commented 2 years ago

Ahh, okay, got it.

Just added a db.close(); at the end of the program.