Search code examples
surrealdbsurrealdb-rpc

how to sign in and get authorization for multiple scopes


I'm just starting out trying to get familiar with SurrealDB. I am really looking forward to the application user auth features, but wondering how someone belonging to multiple scopes would login.

Would they need to re-login for each scope - or can they all be managed in one single login?


Solution

  • If the sessions being simultaneous is not a requirement for your application, you can simply call "signin" again using the same connection with a different scope to switch the scope that is used within the WebSocket session. To avoid asking the users for credentials again or keeping the user credentials for longer than strictly necessary, you can use the JWT that is returned by SurrealDB upon a successful "signin" to call "authenticate", which will have the same effect on the session as calling "signin" again but will use the token that was issued for that scope instead of the user credentials. You (or the client) will need to keep as many tokens as scopes the user requires and call "authenticate" with the relevant one whenever you need to switch scopes.

    If you application has users that need access to multiple SurrealDB scopes concurrently, you will probably need to establish a separate WebSocket session (i.e. connection) for each scope that the user needs.

    Alternatively, for use cases where latency is not critical and where only a single or a few operations are performed for each scope, you can also use the SurrealDB HTTP REST API. Using simple HTTP requests, you will be able to set the scope for each individual request by providing a token issued for that scope.

    I hope this was helpful! Let me know if you need any other help. If so, feel free to share any details you are able about you use case so that I can be more specific.

    Disclaimer: I work in SurrealDB.