Passport/pkg/services/accounts.go
2024-01-28 16:17:38 +08:00

72 lines
1.7 KiB
Go

package services
import (
"code.smartsheep.studio/hydrogen/passport/pkg/database"
"code.smartsheep.studio/hydrogen/passport/pkg/models"
"code.smartsheep.studio/hydrogen/passport/pkg/security"
"fmt"
"gorm.io/datatypes"
)
func GetAccount(id uint) (models.Account, error) {
var account models.Account
if err := database.C.Where(models.Account{
BaseModel: models.BaseModel{ID: id},
}).First(&account).Error; err != nil {
return account, err
}
return account, nil
}
func LookupAccount(id string) (models.Account, error) {
var account models.Account
if err := database.C.Where(models.Account{Name: id}).First(&account).Error; err == nil {
return account, nil
}
var contact models.AccountContact
if err := database.C.Where(models.AccountContact{Content: id}).First(&contact).Error; err == nil {
if err := database.C.
Where(models.Account{
BaseModel: models.BaseModel{ID: contact.AccountID},
}).First(&account).Error; err == nil {
return account, err
}
}
return account, fmt.Errorf("account was not found")
}
func CreateAccount(name, nick, email, password string) (models.Account, error) {
user := models.Account{
Name: name,
Nick: nick,
State: models.PendingAccountState,
Profile: models.AccountProfile{
Experience: 100,
},
Factors: []models.AuthFactor{
{
Type: models.PasswordAuthFactor,
Secret: security.HashPassword(password),
},
},
Contacts: []models.AccountContact{
{
Type: models.EmailAccountContact,
Content: email,
VerifiedAt: nil,
},
},
Permissions: datatypes.NewJSONType(make([]string, 0)),
ConfirmedAt: nil,
}
if err := database.C.Create(&user).Error; err != nil {
return user, err
} else {
return user, nil
}
}