Example app with SurrealDB as backed.
Features:
- Sign up, sign in
- Storing and fetching user's counter values
Before the app can use Surreal's sign up/in feature following preparation is necessary.
Connect to your instance as with surreal sql
, eg.
surreal sql --user root --pass root --db test --ns test --pretty --conn http://localhost:8000
Run following commands:
- Define a table for storing user data. Set permissions to disallow users creating and deleting records, and allow reading and updating user's record:
DEFINE TABLE user SCHEMALESS
PERMISSIONS
FOR select, update WHERE id = $auth.id,
FOR create, delete NONE;
- Define a scope to which users can sign up/in:
DEFINE SCOPE allusers
SESSION 7d
SIGNUP (CREATE user SET user=$user, pass=crypto::argon2::generate($pass))
SIGNIN (SELECT * FROM user WHERE user=$user AND crypto::argon2::compare(pass, $pass));
- Create a counter table for storing user's counter readings:
CREATE TABLE counter;
After finishing above steps your database should response to INFO FOR DB;
like so:
[
{
"result": {
"dl": {},
"dt": {},
"sc": {
"allusers": "DEFINE SCOPE allusers SESSION 1w SIGNUP (CREATE user SET user = $user, pass = crypto::argon2::generate($pass)) SIGNIN (SELECT * FROM user WHERE user = $user AND crypto::argon2::compare(pass, $pass))"
},
"tb": {
"counter": "DEFINE TABLE counter SCHEMALESS",
"user": "DEFINE TABLE user SCHEMALESS PERMISSIONS FOR select WHERE id = $auth.id, FOR create NONE, FOR update WHERE id = $auth.id, FOR delete NONE"
}
},
"status": "OK",
"time": "70.528µs"
}
]
The app is expecting the SURREAL_RPC_URL
key to be set via --dart-define
, eg:
CLI:
flutter run --dart-define SURREAL_RPC_URL=http://localhost:8000/rpc
VSCode config at .vscode/launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"name": "surreal_auth",
"request": "launch",
"type": "dart",
"args": [
"--dart-define",
"SURREAL_RPC_URL=http://localhost:8000/rpc"
]
},
{
"name": "surreal_auth (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile",
"args": [
"--dart-define",
"SURREAL_RPC_URL=http://localhost:8000/rpc"
]
},
{
"name": "surreal_auth (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release",
"args": [
"--dart-define",
"SURREAL_RPC_URL=http://localhost:8000/rpc"
]
}
]
}