phoenix/database/admins.go
Ivan R. 6d25c4e8af
feat!: migrate to net/http
With the release of Go 1.22, the standard library now has
all the necessary functions that allow us to abandon Gin.

I hope this rewrite will lower the entry barrier for new developers.
As a nice bonus, the size of the program has decreased from 20 to 15.4 MB.

To solve issue #81, request logging has been improved.
Now all errors are displayed in the logs.
2024-03-25 15:52:18 +05:00

54 lines
1,017 B
Go

package database
import (
"golang.org/x/crypto/bcrypt"
)
type Admin struct {
ID uint64 `gorm:"primaryKey"`
Username string `gorm:"unique;notNull"`
Bcrypt string `gorm:"notNull"`
}
func CountAdmins() int64 {
var admins []Admin
var count int64
DB.Model(&admins).Count(&count)
return count
}
func CreateAdmin(username string, password string) (Admin, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), 10)
if err != nil {
return Admin{}, err
}
admin := Admin{
Username: username,
Bcrypt: string(hash),
}
result := DB.Create(&admin)
if result.Error != nil {
return Admin{}, result.Error
}
return admin, nil
}
func AuthorizeAdmin(username string, password string) (Admin, error) {
var admin Admin
result := DB.Where("username = ?", username).First(&admin)
if result.Error != nil {
return Admin{}, result.Error
}
err := bcrypt.CompareHashAndPassword([]byte(admin.Bcrypt), []byte(password))
if err != nil {
return Admin{}, err
}
return admin, nil
}