Adjust variable naming
This commit is contained in:
parent
c3334faa9e
commit
78056d8b48
10 changed files with 45 additions and 41 deletions
|
@ -23,8 +23,8 @@ type SQLiteStorage struct {
|
|||
db *sql.DB
|
||||
}
|
||||
|
||||
func (s SQLiteStorage) AddUser(email, password string) error {
|
||||
user, err := storage.NewUser(email, password)
|
||||
func (s SQLiteStorage) Add(email, password string) error {
|
||||
user, err := storage.New(email, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -53,12 +53,12 @@ func (s SQLiteStorage) ByEmail(email string) (storage.User, bool) {
|
|||
return user, err == nil
|
||||
}
|
||||
|
||||
func (s SQLiteStorage) EmailExists(email string) bool {
|
||||
func (s SQLiteStorage) Taken(email string) bool {
|
||||
_, ok := s.ByEmail(email)
|
||||
return ok
|
||||
}
|
||||
|
||||
func MustLoadSQLite(dataFile string) SQLiteStorage {
|
||||
func MustLoad(dataFile string) SQLiteStorage {
|
||||
if _, err := os.Stat(dataFile); errors.Is(err, os.ErrNotExist) {
|
||||
os.Create(dataFile)
|
||||
slog.Debug("created sqlite3 database file", "file", dataFile)
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package storage
|
||||
|
||||
// Storage is an arbitrary storage interface
|
||||
// Storage is an interface for arbitrary storage
|
||||
type Storage interface {
|
||||
AddUser(email, password string) error
|
||||
ById(uuid string) (User, bool)
|
||||
ByEmail(uuid string) (User, bool)
|
||||
EmailExists(email string) bool
|
||||
Add(email, password string) error // Add inserts a user into data
|
||||
ById(uuid string) (User, bool) // ById retrieves a user by their UUID
|
||||
ByEmail(email string) (User, bool) // ByEmail retrieves a user by their email
|
||||
Taken(email string) bool // Taken checks whether an email is taken
|
||||
}
|
||||
|
||||
// Data stores active information for the app
|
||||
// It should be populated at app startup
|
||||
// Data stores active information for the app.
|
||||
// It should be populated on startup
|
||||
var Data Storage
|
||||
|
|
|
@ -13,12 +13,14 @@ type User struct {
|
|||
Hash []byte // bcrypt hash of password
|
||||
}
|
||||
|
||||
func (u User) PasswordFits(password string) bool {
|
||||
// Fits checks whether the password is correct
|
||||
func (u User) Fits(password string) bool {
|
||||
err := bcrypt.CompareHashAndPassword(u.Hash, []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func NewUser(email, password string) (User, error) {
|
||||
// New Creates a new User
|
||||
func New(email, password string) (User, error) {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
||||
if err != nil {
|
||||
slog.Error("error creating a new user", "error", err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue