✨ Auth config to limit auth steps
This commit is contained in:
@ -26,6 +26,8 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
|
||||
preferences := api.Group("/preferences").Name("Preferences API")
|
||||
{
|
||||
preferences.Get("/auth", getAuthConfig)
|
||||
preferences.Put("/auth", updateAuthConfig)
|
||||
preferences.Get("/notifications", getNotificationPreference)
|
||||
preferences.Put("/notifications", updateNotificationPreference)
|
||||
}
|
||||
|
@ -1,12 +1,45 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
func getAuthConfig(c *fiber.Ctx) error {
|
||||
if err := exts.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
return c.JSON(user.AuthConfig)
|
||||
}
|
||||
|
||||
func updateAuthConfig(c *fiber.Ctx) error {
|
||||
if err := exts.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data models.AuthConfig
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
user.AuthConfig = datatypes.NewJSONType(data)
|
||||
|
||||
if err := database.C.Save(&user).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
} else {
|
||||
services.InvalidAuthCacheWithUser(user.ID)
|
||||
}
|
||||
|
||||
return c.JSON(user.AuthConfig)
|
||||
}
|
||||
|
||||
func getNotificationPreference(c *fiber.Ctx) error {
|
||||
if err := exts.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
|
Reference in New Issue
Block a user