Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Auth method parameter to more strict and informing struct para… #112

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ func main() {
panic(err)
}

if _, err = db.Signin(map[string]string{
"user": "root",
"pass": "root",
}); err != nil {
authData := &surrealdb.Auth{
Database: "test",
Namespace: "test",
Username: "root",
Password: "root",
}
if _, err = db.Signin(authData); err != nil {
panic(err)
}

Expand Down
21 changes: 15 additions & 6 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ import (
"github.com/surrealdb/surrealdb.go/pkg/constants"
)

// DB is a client for the SurrealDB database that holds are websocket connection.
// DB is a client for the SurrealDB database that holds the connection.
type DB struct {
conn conn.Connection
}

// New creates a new SurrealDB lient.
// Auth is a struct that holds surrealdb auth data for login.
type Auth struct {
Namespace string `json:"NS,omitempty"`
Database string `json:"DB,omitempty"`
Scope string `json:"SC,omitempty"`
Username string `json:"user,omitempty"`
Password string `json:"pass,omitempty"`
}

// New creates a new SurrealDB client.
func New(url string, connection conn.Connection) (*DB, error) {
connection, err := connection.Connect(url)
if err != nil {
Expand Down Expand Up @@ -44,13 +53,13 @@ func (db *DB) Info() (interface{}, error) {
}

// Signup is a helper method for signing up a new user.
func (db *DB) Signup(vars interface{}) (interface{}, error) {
return db.send("signup", vars)
func (db *DB) Signup(authData *Auth) (interface{}, error) {
return db.send("signup", authData)
}

// Signin is a helper method for signing in a user.
func (db *DB) Signin(vars interface{}) (interface{}, error) {
return db.send("signin", vars)
func (db *DB) Signin(authData *Auth) (interface{}, error) {
return db.send("signin", authData)
}

func (db *DB) Invalidate() (interface{}, error) {
Expand Down
11 changes: 7 additions & 4 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,13 @@ func (s *SurrealDBTestSuite) SetupSuite() {
// Sign with the root user
// Can be used with any user
func signin(s *SurrealDBTestSuite) interface{} {
signin, err := s.db.Signin(map[string]interface{}{
"user": "root",
"pass": "root",
})
authData := &surrealdb.Auth{
Database: "test",
Namespace: "test",
Username: "root",
Password: "root",
}
signin, err := s.db.Signin(authData)
s.Require().NoError(err)
return signin
}
Expand Down