5

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?

LeoDog896
  • 3,472
  • 1
  • 15
  • 40

1 Answers1

6

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 ...
ed__
  • 125
  • 5
  • 1
    Thanks for your detailed response, **@ed_**! For a list of supported WebSocket methods (as at the time of this comment), check the source here: https://github.com/surrealdb/surrealdb/blob/1dd08bedaa997855c4c28dfc03ca23c650f6b2c6/src/net/rpc.rs#L128 Some clue as to what parameters are accepted by each method is available in the methods' definitions (also in the same source file). – ADEBISI Foluso A. Oct 08 '22 at 17:56