Passport/pkg/server/accounts_api.go

47 lines
1023 B
Go
Raw Normal View History

2024-01-06 17:56:32 +00:00
package server
import (
"code.smartsheep.studio/hydrogen/passport/pkg/database"
"code.smartsheep.studio/hydrogen/passport/pkg/models"
"code.smartsheep.studio/hydrogen/passport/pkg/security"
2024-01-26 17:11:32 +00:00
"github.com/gofiber/fiber/v2"
2024-01-06 17:56:32 +00:00
)
2024-01-26 17:11:32 +00:00
func doRegister(c *fiber.Ctx) error {
var data struct {
2024-01-06 17:56:32 +00:00
Name string `json:"name"`
Nick string `json:"nick"`
Email string `json:"email"`
Password string `json:"password"`
2024-01-26 17:11:32 +00:00
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
2024-01-06 17:56:32 +00:00
user := models.Account{
Name: data.Name,
Nick: data.Nick,
State: models.PendingAccountState,
Factors: []models.AuthFactor{
{
Type: models.PasswordAuthFactor,
Secret: security.HashPassword(data.Password),
},
},
Contacts: []models.AccountContact{
{
Type: models.EmailAccountContact,
Content: data.Email,
VerifiedAt: nil,
},
},
}
if err := database.C.Create(&user).Error; err != nil {
2024-01-26 17:11:32 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
2024-01-06 17:56:32 +00:00
}
2024-01-26 17:11:32 +00:00
return c.JSON(user)
2024-01-06 17:56:32 +00:00
}