Files
backend/handlers/auth/postGenLoginUrl.go
2025-12-07 12:15:48 +08:00

65 lines
1.4 KiB
Go

package auth
import (
"encoding/json"
"io"
"net/http"
"gitea.konchin.com/go2025/backend/middlewares"
"github.com/spf13/viper"
"github.com/uptrace/bunrouter"
)
type postGenLoginUrlInput struct {
UserId string `json:"userId"`
}
type postGenLoginUrlOutput struct {
LoginUrl string `json:"loginUrl"`
}
// PostGenLoginUrl
//
// @param payload body postGenLoginUrlInput true "Payload"
// @success 200 {object} postGenLoginUrlOutput "Payload"
// @failure 400
// @router /auth/gen-login-url [post]
func (self *Handlers) PostGenLoginUrl(
w http.ResponseWriter, req bunrouter.Request,
) error {
ctx := req.Context()
b, err := io.ReadAll(req.Body)
if err != nil {
return middlewares.HTTPError{
StatusCode: http.StatusBadRequest,
Message: "failed to read payload",
OriginError: err,
}
}
var input postGenLoginUrlInput
if err := json.Unmarshal(b, &input); err != nil {
return middlewares.HTTPError{
StatusCode: http.StatusBadRequest,
Message: "failed to unmarshal json",
OriginError: err,
}
}
token, err := self.db.UpdateLoginToken(ctx, input.UserId)
if err != nil {
return middlewares.HTTPError{
StatusCode: http.StatusInternalServerError,
Message: "failed to update token",
OriginError: err,
}
}
return bunrouter.JSON(w, postGenLoginUrlOutput{
LoginUrl: viper.GetString("extern-url") +
"/auth/login?" +
"token=" + token,
})
}