add authgurad

This commit is contained in:
2024-06-21 18:54:06 +07:00
parent 5826d72988
commit c771187889
15 changed files with 127 additions and 94 deletions

View File

@@ -6,13 +6,13 @@ import (
"cpone/component/pagination"
sidebaruserprofile "cpone/component/sidebar_user_profile"
"cpone/models"
"cpone/services"
"cpone/utils"
corporate_dashboardpic "cpone/views/corporate/dashboardpic"
"net/http"
"strconv"
"github.com/a-h/templ"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
)
@@ -40,16 +40,17 @@ func (ea *DashboardPicHandler) HandleShowEmployeeAnalyticScreen(c echo.Context)
listID := utils.GenerateRandomID("listid")
paginationID := utils.GenerateRandomID("paginationid")
// usertoken := c.Get("user").(*jwt.Token)
// claims := usertoken.Claims.(jwt.MapClaims)
// name := claims["M_UserEmail"].(string)
// logger.Info("fromtoken", zap.Any("name", name))
userCok := c.Get("user").(*jwt.Token)
claims := userCok.Claims.(jwt.MapClaims)
name := claims["M_StaffName"].(string)
position := claims["M_UserGroupDashboard"].(string)
logger.Info("jwt", zap.Any("name", name))
user, err := services.GetUserLogin()
if err != nil {
defer logger.Sync()
logger.Info("Error get user dev", zap.Any("error", err))
return err
user := models.User{
UserID: 1,
Username: name,
UserFullName: name,
UserPosition: position,
}
dataBreadcrumb, err := ea.DashboardPicServices.GetEmployeeAnalyticBreadcrumb(title)

View File

@@ -5,11 +5,11 @@ import (
navbarmenu "cpone/component/navbar"
sidebaruserprofile "cpone/component/sidebar_user_profile"
"cpone/models"
"cpone/services"
"cpone/utils"
corporate_mcudetail "cpone/views/corporate/mcu"
corporate_tabmcudetail "cpone/views/corporate/mcu/mcutab"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
)
@@ -30,16 +30,21 @@ func NewMcuDetailHandler(mcud McuDetailServices) *McuDetailHandler {
func (mcud *McuDetailHandler) HandlerShowMcuDetailScreen(c echo.Context) error {
logger, _ := zap.NewProduction()
title := "PT. ABC"
id := c.Param("id")
logger.Info("params", zap.Any("id", id))
user, err := services.GetUserLogin()
if err != nil {
defer logger.Sync()
logger.Info("Error get user corp", zap.Any("error", err))
return err
userCok := c.Get("user").(*jwt.Token)
claims := userCok.Claims.(jwt.MapClaims)
name := claims["M_StaffName"].(string)
position := claims["M_UserGroupDashboard"].(string)
logger.Info("jwt", zap.Any("name", name))
user := models.User{
UserID: 1,
Username: name,
UserFullName: name,
UserPosition: position,
}
dataBreadcrumb, err := mcud.McuDetailServices.GetBreadcrumb(id)
@@ -63,7 +68,7 @@ func (mcud *McuDetailHandler) HandlerShowMcuDetailScreen(c echo.Context) error {
js := corporate_mcudetail.JSMcuDetail()
view := corporate_mcudetail.ShowMcuDetail(
title,
dataBreadcrumb.Title,
content,
css,
js,

View File

@@ -6,11 +6,11 @@ import (
"cpone/component/pagination"
sidebaruserprofile "cpone/component/sidebar_user_profile"
"cpone/models"
"cpone/services"
"cpone/utils"
corporate_patient "cpone/views/corporate/patient"
"strconv"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
)
@@ -125,19 +125,23 @@ func (ph *PatientHandler) LoadLocalData(c echo.Context) error {
func (ph *PatientHandler) HandleShowPatient(c echo.Context) error {
logger, _ := zap.NewProduction()
// userEmail := c.QueryParam("email")
// logger.Info("params", zap.Any("email", userEmail))
listID := utils.GenerateRandomID("listid")
paginationID := utils.GenerateRandomID("paginationid")
dialogID := utils.GenerateRandomID("dialogid")
dialogBodyID := utils.GenerateRandomID("dialogbodyid")
user, err := services.GetUserLogin()
if err != nil {
defer logger.Sync()
logger.Info("Error get user", zap.Any("error", err))
return err
userCok := c.Get("user").(*jwt.Token)
claims := userCok.Claims.(jwt.MapClaims)
name := claims["M_StaffName"].(string)
position := claims["M_UserGroupDashboard"].(string)
logger.Info("jwt", zap.Any("name", name))
user := models.User{
UserID: 1,
Username: name,
UserFullName: name,
UserPosition: position,
}
navbaruser := navbarmenu.NavbarWithLogo(user)

View File

@@ -6,6 +6,7 @@ import (
public_login "cpone/views/public/login"
"encoding/json"
"net/http"
"time"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
@@ -79,6 +80,15 @@ func (lh *LoginHandler) HandleSignIn(c echo.Context) error {
si := public_login.ShowLogin("Login ", public_login.MainLogin(), public_login.CssLogin(), public_login.JsLogin())
return utils.View(c, si)
}
cookie := new(http.Cookie)
cookie.Name = "token"
cookie.Value = resp.Data.Token
cookie.Path = "/"
cookie.HttpOnly = true
c.SetCookie(cookie)
userString := string(user)
store := `
<script>
@@ -94,25 +104,7 @@ func (lh *LoginHandler) HandleSignIn(c echo.Context) error {
setTimeout(() => {
htmx.ajax('GET', '/login/redirect?url=` + url + `')
}, 500)
// document.body.addEventListener('htmx:configRequest', function(evt) {
// evt.detail.headers['Authentication'] = "Bearer: " + getJWT();
// console.log("running");
// });
// setTimeout(() => {
// window.location.href = "` + url + `";
// }, 100);
// document.body.addEventListener('htmx:configRequest', function(evt) {
// const token = localStorage.getItem("token");
// if (token) {
// evt.detail.headers['Authorization'] = "Bearer " + token;
// }
// });
// document.body.hxTrigger({ "hx-redirect": "` + url + `" });
}, 200)
</script>
`
c.Response().Header().Set("HX-Trigger", "script")
@@ -125,7 +117,34 @@ func (lh *LoginHandler) HandleRedirect(c echo.Context) error {
token := c.Request().Header.Get("Authentication")
logger.Info("params", zap.Any("url", url), zap.Any("token", token))
c.Response().Header().Set("Authentication", token)
c.Response().Header().Set("HX-Redirect", url)
return c.String(http.StatusOK, url)
}
func (lh *LoginHandler) HandleSignOut(c echo.Context) error {
logger, _ := zap.NewProduction()
logger.Info("Logout")
expire := time.Now().Add(-7 * 24 * time.Hour)
cookie := new(http.Cookie)
cookie.Name = "token"
cookie.Value = ""
cookie.Path = "/"
cookie.Expires = expire
cookie.HttpOnly = true
c.SetCookie(cookie)
ret := `
<script>
console.log("signout")
localStorage.removeItem("token")
localStorage.removeItem("user")
setTimeout(() => {
window.location.replace("/login");
}, 200)
</script>
`
c.Response().Header().Set("HX-Trigger", "script")
return c.String(http.StatusOK, ret)
}

View File

@@ -1,6 +1,7 @@
package handlers
import (
"cpone/auth"
client_handlers "cpone/handlers/client"
corporate_handlers "cpone/handlers/corporate"
mcu_corporate_handlers "cpone/handlers/corporate/mcudetail"
@@ -78,6 +79,7 @@ func SetupRoutesPublic(app *echo.Echo, appStore db.AppStore) {
public.GET("login", loginHadlr.HandlerShowLogin)
public.POST("login/signin", loginHadlr.HandleSignIn)
public.GET("login/redirect", loginHadlr.HandleRedirect)
public.GET("login/signout", loginHadlr.HandleSignOut)
// kartu kontrol
publicKartuKontrolServices := public_services.NewServicesKartuKontrol(appStore)
@@ -100,7 +102,8 @@ func SetupRoutesPublic(app *echo.Echo, appStore db.AppStore) {
public.GET("redirectsurveymcu", publicKartuKontrolhandlers.HandlerRedirectToSurveyMcu)
}
func SetupRoutesCorporate(app *echo.Echo, appStore db.AppStore) {
corp := app.Group("/corp")
corp := app.Group("/corp", auth.IsLoggedIn)
// corp := app.Group("/corp")
l := corporate_services.NewServicesCompany(appStore)
lh := corporate_handlers.NewCompanyHandler(l)
corp.GET("/company", lh.ShowCompany)
@@ -137,7 +140,7 @@ func SetupRoutesCorporate(app *echo.Echo, appStore db.AppStore) {
}
func SetupRoutesClient(app *echo.Echo, appStore db.AppStore) {
public := app.Group("/client")
public := app.Group("/client", auth.IsLoggedIn)
lpanel := client_services.NewServicesMdPanel(appStore)
lhpanel := client_handlers.NewMdPanelHandler(lpanel)