How can I log into a scope with SurrealDB?

Viewed 132

I'm using the example code from the SurrealDB Features page for a user/pass system

DEFINE SCOPE admin SESSION 1d
        SIGNUP ( CREATE user SET user = $user, pass = crypto::argon2::generate($pass) )
        SIGNIN ( SELECT * FROM user WHERE user = $user AND crypto::argon2::compare(pass, $pass));

Unfortunately, the docs don't say how to log into the scope. Can I do this with SurrealQL or an API endpoint?

1 Answers

High level answer: you call signin from whichever client library you are using, at the moment most of this is still in development, e.g. see https://github.com/surrealdb/surrealdb.js


Low level answer, there are multiple types of logins, the regular root login requires user and pass. For a scope login, you specify NS, DB, and SC values, for namespace, database and scope respectively, then you add any values needed for the scope.

You can connect to a websocket at ws://<host>:<port>/rpc, then send commands as json in the format

{
  "id": <an id so you can identify responses later on>,
  "method": <one of the available commands>,
  "params": <an array of parameters>
}

As a minimal example, lets create some commands to signin to the scope you defined:

method     params
------     ------
signin     {
              "NS": <ns>,
              "DB": <DB>,
              "SC": "admin",
              // these parameters can be anything you request in the scope
              "user": <user>,
              "pass": <pass>
           }
use        <namespace>, <database>
// use your session ...
Related