package auth import ( "nixcn-cms/internal/exception" "nixcn-cms/service/service_auth" "nixcn-cms/utils" "github.com/gin-gonic/gin" ) // Redirect handles the post-verification callback and redirects the user to the target application. // @Summary Handle Auth Callback and Redirect // @Description Verifies the temporary email code, ensures the user exists (or creates one), validates the client's redirect URI, and finally performs a 302 redirect with a new authorization code. // @Tags Authentication // @Accept x-www-form-urlencoded // @Produce json // @Produce html // @Param client_id query string true "Client Identifier" // @Param redirect_uri query string true "Target Redirect URI" // @Param code query string true "Temporary Verification Code" // @Param state query string false "Opaque state used to maintain state between the request and callback" // @Success 302 {string} string "Redirect to the provided RedirectUri with a new code" // @Failure 400 {object} utils.RespStatus{data=nil} "Invalid Input / Client Not Found / URI Mismatch" // @Failure 403 {object} utils.RespStatus{data=nil} "Invalid or Expired Verification Code" // @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error" // @Router /auth/redirect [get] func (self *AuthHandler) Redirect(c *gin.Context) { data := &service_auth.RedirectData{ ClientId: c.Query("client_id"), RedirectUri: c.Query("redirect_uri"), State: c.Query("state"), Code: c.Query("code"), } if data.ClientId == "" || data.RedirectUri == "" || data.Code == "" { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceAuth). SetEndpoint(exception.EndpointAuthServiceRedirect). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). SetError(nil). Throw(c). String() utils.HttpResponse(c, 400, errorCode) return } result := self.svc.Redirect(&service_auth.RedirectPayload{ Context: c, Data: data, }) if result.Common.Exception.Original != exception.CommonSuccess { utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String()) return } c.Redirect(302, result.Data) }