All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Signed-off-by: Asai Neko <sugar@sne.moe>
64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package auth
|
|
|
|
import (
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/service/service_auth"
|
|
"nixcn-cms/tracer"
|
|
"nixcn-cms/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Magic handles the "Magic Link" authentication request.
|
|
//
|
|
// @Summary Request Magic Link
|
|
// @Description Verifies Turnstile token and sends an authentication link via email. Returns the URI directly if debug mode is enabled.
|
|
// @Tags Authentication
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param payload body service_auth.MagicData true "Magic Link Request Data"
|
|
// @Success 200 {object} utils.RespStatus{data=service_auth.MagicResponse} "Successful request"
|
|
// @Failure 400 {object} utils.RespStatus{data=nil} "Invalid Input"
|
|
// @Failure 403 {object} utils.RespStatus{data=nil} "Turnstile Verification Failed"
|
|
// @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error"
|
|
// @Router /auth/magic [post]
|
|
func (self *AuthHandler) Magic(c *gin.Context) {
|
|
ctx, span := tracer.StartSpan(
|
|
c.Request.Context(),
|
|
"api_auth",
|
|
"magic",
|
|
)
|
|
defer span.End()
|
|
|
|
ctx = exception.ContextWithEndpoint(ctx, exception.EndpointAuthMagic)
|
|
ctx = exception.ContextWithService(ctx, exception.ServiceEndpoint)
|
|
|
|
var magicData service_auth.MagicData
|
|
|
|
if err := c.ShouldBindJSON(&magicData); err != nil {
|
|
errorCode := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorInvalidInput),
|
|
exception.WithError(err),
|
|
).Throw(ctx).String()
|
|
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
|
|
magicData.ClientIP = c.ClientIP()
|
|
|
|
result := self.svc.Magic(&service_auth.MagicPayload{
|
|
Context: ctx,
|
|
Data: &magicData,
|
|
})
|
|
|
|
if result.Common.Exception.Original != exception.CommonSuccess {
|
|
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String())
|
|
return
|
|
}
|
|
|
|
utils.HttpResponse(c, 200, result.Common.Exception.String(), result.Data)
|
|
}
|