package main
import (
"context"
"fmt"
surrealdb "github.com/surrealdb/surrealdb.go"
"github.com/surrealdb/surrealdb.go/contrib/testenv"
)
func ExampleDB_record_user_auth_struct() {
ns := "surrealdbexamples"
db := testenv.MustNew(ns, "record_auth_demo", "user")
setupQuery := `
-- Define the user table with schema
DEFINE TABLE user SCHEMAFULL
PERMISSIONS
FOR select, update, delete WHERE id = $auth.id;
-- Define fields
DEFINE FIELD name ON user TYPE string;
DEFINE FIELD password ON user TYPE string;
-- Define unique index on email
REMOVE INDEX IF EXISTS name ON user;
DEFINE INDEX name ON user FIELDS name UNIQUE;
-- Define access method for record authentication
REMOVE ACCESS IF EXISTS user ON DATABASE;
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM user WHERE name = $user AND crypto::argon2::compare(password, $pass)
)
SIGNUP (
CREATE user CONTENT {
name: $user,
password: crypto::argon2::generate($pass)
}
);
`
if _, err := surrealdb.Query[any](context.Background(), db, setupQuery, nil); err != nil {
panic(err)
}
fmt.Println("Database schema setup complete")
_, err := db.SignUp(context.Background(), &surrealdb.Auth{
Namespace: ns,
Database: "record_auth_demo",
Access: "user",
Username: "yusuke",
Password: "VerySecurePassword123!",
})
if err != nil {
panic(err)
}
fmt.Println("User signed up successfully")
_, err = db.SignIn(context.Background(), &surrealdb.Auth{
Namespace: ns,
Database: "record_auth_demo",
Access: "user",
Username: "yusuke",
Password: "VerySecurePassword123!",
})
if err != nil {
panic(err)
}
fmt.Println("User signed in successfully")
info, err := db.Info(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("Authenticated user name: %v\n", info["name"])
}
func ExampleDB_record_user_custom_struct() {
ns := "surrealdbexamples"
db := testenv.MustNew(ns, "record_user_custom", "user")
setupQuery := `
-- Define the user table with schema
DEFINE TABLE user SCHEMAFULL
PERMISSIONS
FOR select, update, delete WHERE id = $auth.id;
-- Define fields
DEFINE FIELD name ON user TYPE string;
DEFINE FIELD email ON user TYPE string;
DEFINE FIELD password ON user TYPE string;
-- Define unique index on email
REMOVE INDEX IF EXISTS email ON user;
DEFINE INDEX email ON user FIELDS email UNIQUE;
-- Define access method for record authentication
REMOVE ACCESS IF EXISTS user ON DATABASE;
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(password, $password)
)
SIGNUP (
CREATE user CONTENT {
name: $name,
email: $email,
password: crypto::argon2::generate($password)
}
);
`
if _, err := surrealdb.Query[any](context.Background(), db, setupQuery, nil); err != nil {
panic(err)
}
fmt.Println("Database schema setup complete")
type User struct {
Namespace string `json:"NS"`
Database string `json:"DB"`
Access string `json:"AC"`
Name string `json:"name"`
Password string `json:"password"`
Email string `json:"email"`
}
type LoginRequest struct {
Namespace string `json:"NS"`
Database string `json:"DB"`
Access string `json:"AC"`
Email string `json:"email"`
Password string `json:"password"`
}
_, err := db.SignUp(context.Background(), &User{
Namespace: ns,
Database: "record_user_custom",
Access: "user",
Name: "yusuke",
Password: "VerySecurePassword123!",
Email: "yusuke@example.com",
})
if err != nil {
panic(err)
}
fmt.Println("User signed up successfully")
_, err = db.SignIn(context.Background(), &LoginRequest{
Namespace: ns,
Database: "record_user_custom",
Access: "user",
Email: "yusuke@example.com",
Password: "VerySecurePassword123!",
})
if err != nil {
panic(err)
}
fmt.Println("User signed in successfully")
info, err := db.Info(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("Authenticated user name: %v\n", info["name"])
}