Passport/pkg/server/accounts_api.go

63 lines
1.4 KiB
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"
2024-01-28 08:17:38 +00:00
"code.smartsheep.studio/hydrogen/passport/pkg/services"
2024-01-26 17:11:32 +00:00
"github.com/gofiber/fiber/v2"
2024-01-28 08:17:38 +00:00
"gorm.io/gorm/clause"
2024-01-06 17:56:32 +00:00
)
2024-01-28 08:17:38 +00:00
func getPrincipal(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
var data models.Account
if err := database.C.Where(&models.Account{
BaseModel: models.BaseModel{ID: user.ID},
}).Preload(clause.Associations).First(&data).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(data)
}
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
2024-01-28 08:17:38 +00:00
if user, err := services.CreateAccount(
data.Name,
data.Nick,
data.Email,
data.Password,
); err != nil {
2024-01-26 17:11:32 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
2024-01-28 08:17:38 +00:00
} else {
return c.JSON(user)
2024-01-06 17:56:32 +00:00
}
}
2024-01-28 16:32:39 +00:00
func doRegisterConfirm(c *fiber.Ctx) error {
var data struct {
Code string `json:"code"`
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
if err := services.ConfirmAccount(data.Code); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}