56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package users
|
|
|
|
import (
|
|
"astra-api-gateway/config"
|
|
"astra-api-gateway/model"
|
|
"astra-api-gateway/pkg/api"
|
|
"encoding/json"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/labstack/echo/v4"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func Login(c echo.Context) (err error) {
|
|
// Bind
|
|
u := new(model.User)
|
|
if err = c.Bind(u); err != nil {
|
|
return
|
|
}
|
|
|
|
data, err := api.PostRequest(config.Env.MSHosts.UsersHost+"/login", u)
|
|
|
|
u.Password = ""
|
|
|
|
if err != nil {
|
|
return c.NoContent(http.StatusBadGateway)
|
|
}
|
|
|
|
err = json.Unmarshal(data, &u)
|
|
|
|
if err != nil {
|
|
return c.NoContent(http.StatusUnauthorized)
|
|
}
|
|
|
|
token := jwt.New(jwt.SigningMethodHS256)
|
|
|
|
claims := token.Claims.(jwt.MapClaims)
|
|
claims["id"] = strconv.Itoa(int(u.ID))
|
|
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
|
|
|
|
var _token, _ = token.SignedString([]byte(config.Env.Server.JWTSecret))
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
c.SetCookie(&http.Cookie{
|
|
Name: "accessToken",
|
|
Value: _token,
|
|
HttpOnly: true,
|
|
Path: "/",
|
|
})
|
|
|
|
return c.JSON(http.StatusOK, u)
|
|
}
|