feat: use nanoid instead of uuid
This commit is contained in:
parent
635bdad28a
commit
7bcf560e61
@ -6,7 +6,7 @@ import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
gonanoid "github.com/matoous/go-nanoid"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
@ -47,18 +47,24 @@ func (db *DeviceDatabase) Close() error {
|
||||
return db.Connection.Close()
|
||||
}
|
||||
func (db *DeviceDatabase) CreateDevice(device_name string, description string) (string, error) {
|
||||
deviceID := uuid.New()
|
||||
_, err := db.Connection.Exec("INSERT INTO devices (id, device_name, description) VALUES (?, ?, ?)", deviceID.String(), device_name, description)
|
||||
return deviceID.String(), err
|
||||
}
|
||||
|
||||
func (db *DeviceDatabase) CreateSession(deviceID string, expiryDate time.Time) (string, error) {
|
||||
sessionToken := uuid.New()
|
||||
_, err := db.Connection.Exec("INSERT INTO sessions (device_id, token, expiry_date) VALUES (?, ?, ?)", deviceID, sessionToken.String(), expiryDate)
|
||||
deviceID, err := gonanoid.Nanoid(8)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return sessionToken.String(), nil
|
||||
_, err = db.Connection.Exec("INSERT INTO devices (id, device_name, description) VALUES (?, ?, ?)", deviceID, device_name, description)
|
||||
return deviceID, err
|
||||
}
|
||||
|
||||
func (db *DeviceDatabase) CreateSession(deviceID string, expiryDate time.Time) (string, error) {
|
||||
sessionToken, err := gonanoid.Nanoid(16)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, err = db.Connection.Exec("INSERT INTO sessions (device_id, token, expiry_date) VALUES (?, ?, ?)", deviceID, sessionToken, expiryDate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return sessionToken, nil
|
||||
}
|
||||
|
||||
func (db *DeviceDatabase) GetDeviceById(id string) (*PlaybackDevice, error) {
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
gonanoid "github.com/matoous/go-nanoid"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
@ -49,23 +49,31 @@ func (db *UserDatabase) Close() error {
|
||||
return db.Connection.Close()
|
||||
}
|
||||
func (db *UserDatabase) CreateUser(username, password string, isAdmin bool) (string, error) {
|
||||
userID := uuid.New()
|
||||
userID, err := gonanoid.Nanoid(8)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
hashedPassword, err := hashPassword(password)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = db.Connection.Exec("INSERT INTO users (id, username, password, is_admin) VALUES (?, ?, ?, ?)", userID.String(), username, hashedPassword, isAdmin)
|
||||
return userID.String(), err
|
||||
_, err = db.Connection.Exec("INSERT INTO users (id, username, password, is_admin) VALUES (?, ?, ?, ?)", userID, username, hashedPassword, isAdmin)
|
||||
return userID, err
|
||||
}
|
||||
|
||||
func (db *UserDatabase) CreateSession(userID string, expiryDate time.Time) (string, error) {
|
||||
sessionToken := uuid.New()
|
||||
_, err := db.Connection.Exec("INSERT INTO sessions (user_id, token, expiry_date) VALUES (?, ?, ?)", userID, sessionToken.String(), expiryDate)
|
||||
sessionToken, err := gonanoid.Nanoid(16)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return sessionToken.String(), nil
|
||||
|
||||
_, err = db.Connection.Exec("INSERT INTO sessions (user_id, token, expiry_date) VALUES (?, ?, ?)", userID, sessionToken, expiryDate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return sessionToken, nil
|
||||
}
|
||||
|
||||
func (db *UserDatabase) GetUserByUsername(username string) (*User, error) {
|
||||
|
||||
1
go.mod
1
go.mod
@ -6,6 +6,7 @@ require (
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/labstack/echo/v4 v4.13.3 // indirect
|
||||
github.com/labstack/gommon v0.4.2 // indirect
|
||||
github.com/matoous/go-nanoid v1.5.1 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.24 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@ -6,6 +6,8 @@ github.com/labstack/echo/v4 v4.13.3 h1:pwhpCPrTl5qry5HRdM5FwdXnhXSLSY+WE+YQSeCaa
|
||||
github.com/labstack/echo/v4 v4.13.3/go.mod h1:o90YNEeQWjDozo584l7AwhJMHN0bOC4tAfg+Xox9q5g=
|
||||
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
|
||||
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
|
||||
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
|
||||
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
|
||||
Loading…
Reference in New Issue
Block a user