Passport/pkg/server/accounts_api.go

175 lines
4.7 KiB
Go
Raw Normal View History

2024-01-06 17:56:32 +00:00
package server
import (
2024-03-16 04:28:50 +00:00
"fmt"
"strconv"
"time"
2024-03-20 12:56:43 +00:00
"git.solsynth.dev/hydrogen/identity/pkg/database"
"git.solsynth.dev/hydrogen/identity/pkg/models"
"git.solsynth.dev/hydrogen/identity/pkg/services"
2024-01-26 17:11:32 +00:00
"github.com/gofiber/fiber/v2"
jsoniter "github.com/json-iterator/go"
2024-01-29 08:11:59 +00:00
"github.com/spf13/viper"
2024-01-06 17:56:32 +00:00
)
2024-01-31 13:16:54 +00:00
func getUserinfo(c *fiber.Ctx) error {
2024-01-28 08:17:38 +00:00
user := c.Locals("principal").(models.Account)
var data models.Account
2024-01-30 07:57:49 +00:00
if err := database.C.
Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
Preload("Profile").
Preload("Contacts").
First(&data).Error; err != nil {
2024-01-28 08:17:38 +00:00
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
var resp fiber.Map
raw, _ := jsoniter.Marshal(data)
jsoniter.Unmarshal(raw, &resp)
resp["sub"] = strconv.Itoa(int(data.ID))
resp["family_name"] = data.Profile.FirstName
resp["given_name"] = data.Profile.LastName
resp["name"] = data.Name
resp["email"] = data.GetPrimaryEmail().Content
resp["preferred_username"] = data.Nick
2024-01-31 16:33:01 +00:00
if len(data.Avatar) > 0 {
resp["picture"] = fmt.Sprintf("https://%s/api/avatar/%s", viper.GetString("domain"), data.Avatar)
}
return c.JSON(resp)
2024-01-28 08:17:38 +00:00
}
2024-01-30 09:57:23 +00:00
func getEvents(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
var count int64
var events []models.ActionEvent
if err := database.C.
Where(&models.ActionEvent{AccountID: user.ID}).
Model(&models.ActionEvent{}).
Count(&count).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
if err := database.C.
2024-02-12 04:31:18 +00:00
Order("created_at desc").
2024-01-30 09:57:23 +00:00
Where(&models.ActionEvent{AccountID: user.ID}).
Limit(take).
Offset(offset).
Find(&events).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(fiber.Map{
"count": count,
"data": events,
})
}
2024-01-31 13:16:54 +00:00
func editUserinfo(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
var data struct {
2024-02-28 15:30:29 +00:00
Nick string `json:"nick" validate:"required,min=4,max=24"`
Description string `json:"description"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Birthday time.Time `json:"birthday"`
2024-01-31 13:16:54 +00:00
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
var account models.Account
if err := database.C.
Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
Preload("Profile").
First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
account.Nick = data.Nick
2024-02-28 15:30:29 +00:00
account.Description = data.Description
2024-01-31 13:16:54 +00:00
account.Profile.FirstName = data.FirstName
account.Profile.LastName = data.LastName
account.Profile.Birthday = &data.Birthday
if err := database.C.Save(&account).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else if err := database.C.Save(&account.Profile).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}
2024-01-30 10:18:25 +00:00
func killSession(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
id, _ := c.ParamsInt("sessionId", 0)
if err := database.C.Delete(&models.AuthSession{}, &models.AuthSession{
BaseModel: models.BaseModel{ID: uint(id)},
AccountID: user.ID,
}).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}
2024-01-26 17:11:32 +00:00
func doRegister(c *fiber.Ctx) error {
var data struct {
2024-01-31 13:16:54 +00:00
Name string `json:"name" validate:"required,lowercase,alphanum,min=4,max=16"`
Nick string `json:"nick" validate:"required,min=4,max=24"`
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required,min=4,max=32"`
2024-01-29 08:11:59 +00:00
MagicToken string `json:"magic_token"`
2024-01-26 17:11:32 +00:00
}
if err := BindAndValidate(c, &data); err != nil {
return err
2024-01-29 08:11:59 +00:00
} else if viper.GetBool("use_registration_magic_token") && len(data.MagicToken) <= 0 {
return fmt.Errorf("missing magic token in request")
} else if viper.GetBool("use_registration_magic_token") {
if tk, err := services.ValidateMagicToken(data.MagicToken, models.RegistrationMagicToken); err != nil {
return err
} else {
database.C.Delete(&tk)
}
2024-01-26 17:11:32 +00:00
}
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 {
2024-01-31 13:16:54 +00:00
Code string `json:"code" validate:"required"`
2024-01-28 16:32:39 +00:00
}
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)
}