mirror of
https://github.com/ordinary-dev/phoenix
synced 2024-09-20 03:40:29 +05:00
Ivan R.
6d25c4e8af
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.
54 lines
1,017 B
Go
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
|
|
}
|