nidus-sync/api/signin.go

39 lines
1.1 KiB
Go
Raw Normal View History

package api
import (
2026-03-27 06:08:55 -07:00
"context"
"errors"
"net/http"
"github.com/Gleipnir-Technology/nidus-sync/auth"
2026-03-27 06:08:55 -07:00
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
"github.com/rs/zerolog/log"
)
2026-03-27 06:08:55 -07:00
type reqSignin struct {
Password string `json:"password"`
Username string `json:"username"`
}
2026-03-27 06:08:55 -07:00
func postSignin(ctx context.Context, r *http.Request, req reqSignin) (string, *nhttp.ErrorWithStatus) {
if req.Password == "" {
return "", nhttp.NewErrorStatus(http.StatusBadRequest, "Empty password")
}
2026-03-27 06:08:55 -07:00
if req.Username == "" {
return "", nhttp.NewErrorStatus(http.StatusBadRequest, "Empty username")
}
2026-03-27 06:08:55 -07:00
log.Info().Str("username", req.Username).Msg("API Signin")
_, err := auth.SigninUser(r, req.Username, req.Password)
if err != nil {
if errors.Is(err, auth.InvalidCredentials{}) {
2026-03-27 06:08:55 -07:00
return "", nhttp.NewErrorStatus(http.StatusUnauthorized, "invalid credentials")
}
if errors.Is(err, auth.InvalidUsername{}) {
2026-03-27 06:08:55 -07:00
return "", nhttp.NewErrorStatus(http.StatusUnauthorized, "invalid credentials")
}
2026-03-27 06:08:55 -07:00
log.Error().Err(err).Str("username", req.Username).Msg("Login server error")
return "", nhttp.NewError("login server error")
}
2026-03-27 06:08:55 -07:00
return "/", nil
}