first commit
This commit is contained in:
71
services/auth/auth.routes.go
Normal file
71
services/auth/auth.routes.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"sismedika.com/sas/westone/types"
|
||||
"sismedika.com/sas/westone/utils"
|
||||
)
|
||||
|
||||
// Login login using westone account
|
||||
//
|
||||
// @Summary login using westone account
|
||||
// @Description login using westone account
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Param parameter body types.SignInPayload true "parameter"
|
||||
// @Produce json
|
||||
// @Success 200 {object} map[string]any
|
||||
// @Failure 400 {object} map[string]any
|
||||
// @Failure 500 {object} map[string]any
|
||||
// @Router /westone/api/v1/auth/login [post]
|
||||
func (h *Handler) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
var payload types.SignInPayload
|
||||
fmt.Println("User Agent")
|
||||
fmt.Println(r.UserAgent())
|
||||
if err := utils.ParseJSON(r, &payload); err != nil {
|
||||
utils.WriteError(w, http.StatusBadRequest, err)
|
||||
h.store.LogSignIn(payload.Email, r.RemoteAddr, "FAILED", "LOGIN", "westone")
|
||||
return
|
||||
}
|
||||
|
||||
if err := utils.Validate.Struct(payload); err != nil {
|
||||
errorz := err.(validator.ValidationErrors)
|
||||
utils.WriteError(w, http.StatusBadRequest, fmt.Errorf("invalid payload: %v", errorz))
|
||||
h.store.LogSignIn(payload.Email, r.RemoteAddr, "FAILED", "LOGIN", "westone")
|
||||
return
|
||||
}
|
||||
|
||||
hashedPassword := HashWithMD5(payload.Password)
|
||||
response, err := h.store.SignInWestone(payload.Email, hashedPassword)
|
||||
if err != nil {
|
||||
var logError *utils.LogError
|
||||
if errors.As(err, &logError) {
|
||||
h.errorz.CreateErrorLog(*logError)
|
||||
utils.WriteErrorLog(w, http.StatusBadRequest, *logError)
|
||||
}
|
||||
h.store.LogSignIn(payload.Email, r.RemoteAddr, "FAILED", "LOGIN", "westone")
|
||||
return
|
||||
}
|
||||
|
||||
timenow := time.Now()
|
||||
token, err := CreateJWT(types.DataJWT{
|
||||
User: *response,
|
||||
Ip: r.RemoteAddr,
|
||||
Agent: r.UserAgent(),
|
||||
Version: "v1",
|
||||
LastLogin: timenow.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
utils.WriteError(w, http.StatusInternalServerError, err)
|
||||
h.store.LogSignIn(payload.Email, r.RemoteAddr, "FAILED", "LOGIN", "westone")
|
||||
return
|
||||
}
|
||||
|
||||
utils.WriteJSONLogin(w, http.StatusOK, response, token, "westone")
|
||||
}
|
||||
Reference in New Issue
Block a user