Feat: add login

This commit is contained in:
2025-12-07 12:15:48 +08:00
parent fb1c47b321
commit 2eb8a18c40
11 changed files with 265 additions and 7 deletions

View File

@@ -1,9 +1,12 @@
package auth
import (
"encoding/json"
"io"
"net/http"
"gitea.konchin.com/go2025/backend/utils"
"gitea.konchin.com/go2025/backend/middlewares"
"github.com/spf13/viper"
"github.com/uptrace/bunrouter"
)
@@ -24,5 +27,38 @@ type postGenLoginUrlOutput struct {
func (self *Handlers) PostGenLoginUrl(
w http.ResponseWriter, req bunrouter.Request,
) error {
return utils.Success(w)
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,
})
}