add header filter
This commit is contained in:
69
services/users/users.go
Normal file
69
services/users/users.go
Normal file
@@ -0,0 +1,69 @@
|
||||
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, c.Request().Header)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func GetCurrentUser(c echo.Context) error {
|
||||
u := new(model.User)
|
||||
|
||||
data, err := api.GetRequest(config.Env.MSHosts.UsersHost+"/me", c.Request().Header)
|
||||
|
||||
err = json.Unmarshal(data, &u)
|
||||
|
||||
if err != nil {
|
||||
return c.NoContent(http.StatusBadGateway)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, u)
|
||||
}
|
||||
Reference in New Issue
Block a user