Skip to content

Commit

Permalink
Change Auth method parameter to more strict and informing struct para… (
Browse files Browse the repository at this point in the history
#112)

Co-authored-by: Przemyslaw Hugh Kaznowski <[email protected]>
  • Loading branch information
ElecTwix and phughk authored Nov 27, 2023
1 parent 57a4a91 commit b90883b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
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

0 comments on commit b90883b

Please sign in to comment.