diff --git a/auth/jwt.go b/auth/jwt.go new file mode 100644 index 0000000..c1400d6 --- /dev/null +++ b/auth/jwt.go @@ -0,0 +1,20 @@ +package auth + +import ( + "net/http" + + echojwt "github.com/labstack/echo-jwt/v4" + "github.com/labstack/echo/v4" +) + +var IsLoggedIn = echojwt.WithConfig( + echojwt.Config{ + SigningKey: []byte("--one_api-secret-2019-04-01"), + TokenLookup: "cookie:token", + ErrorHandler: JWTErrorChecker, + }, +) + +func JWTErrorChecker(c echo.Context, err error) error { + return c.Redirect(http.StatusTemporaryRedirect, "/login") +} diff --git a/auth/middleware.go b/auth/middleware.go deleted file mode 100644 index 1c9ad35..0000000 --- a/auth/middleware.go +++ /dev/null @@ -1,7 +0,0 @@ -package auth - -import "github.com/labstack/echo/v4/middleware" - -var IsLoggedIn = middleware.JWTWithConfig(middleware.JWTConfig{ - SigningKey: []byte("secret"), -}) diff --git a/component/sidebar_user_profile/sidebar_user_profile.templ b/component/sidebar_user_profile/sidebar_user_profile.templ index 06b932f..a78df87 100644 --- a/component/sidebar_user_profile/sidebar_user_profile.templ +++ b/component/sidebar_user_profile/sidebar_user_profile.templ @@ -24,16 +24,16 @@ templ Navbaruserprofile(dataUser models.User) {
-
- -
- Recent Notifications -
- - - @NotificationCard() - -
+ //
+ // + //
+ // Recent Notifications + //
+ // + // + // @NotificationCard() + // + //
@@ -55,7 +55,12 @@ templ ProfileHeader(dataUser models.User) { { dataUser.UserPosition } diff --git a/component/sidebar_user_profile/sidebar_user_profile_templ.go b/component/sidebar_user_profile/sidebar_user_profile_templ.go index 2723c93..e9bcc9e 100644 --- a/component/sidebar_user_profile/sidebar_user_profile_templ.go +++ b/component/sidebar_user_profile/sidebar_user_profile_templ.go @@ -33,15 +33,7 @@ func Navbaruserprofile(dataUser models.User) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Recent Notifications\r
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = NotificationCard().Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -91,7 +83,7 @@ func ProfileHeader(dataUser models.User) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Sign Out
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Sign Out\r
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/go.mod b/go.mod index 42b746a..1a77b95 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/fsnotify/fsnotify v1.7.0 github.com/glebarez/go-sqlite v1.22.0 github.com/go-sql-driver/mysql v1.8.1 + github.com/golang-jwt/jwt/v5 v5.0.0 github.com/jmoiron/sqlx v1.4.0 github.com/labstack/echo/v4 v4.12.0 github.com/spf13/viper v1.18.2 @@ -36,6 +37,7 @@ require ( github.com/dustin/go-humanize v1.0.1 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/google/uuid v1.5.0 // indirect + github.com/labstack/echo-jwt/v4 v4.2.0 github.com/labstack/gommon v0.4.2 github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index 86a78ad..be1b4bb 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= +github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE= +github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= @@ -32,6 +34,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/labstack/echo-jwt/v4 v4.2.0 h1:odSISV9JgcSCuhgQSV/6Io3i7nUmfM/QkBeR5GVJj5c= +github.com/labstack/echo-jwt/v4 v4.2.0/go.mod h1:MA2RqdXdEn4/uEglx0HcUOgQSyBaTh5JcaHIan3biwU= github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0= github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= diff --git a/handlers/corporate/dashboardpic.handlers.go b/handlers/corporate/dashboardpic.handlers.go index af23f4b..9f69c4e 100644 --- a/handlers/corporate/dashboardpic.handlers.go +++ b/handlers/corporate/dashboardpic.handlers.go @@ -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) diff --git a/handlers/corporate/mcudetail.handlers.go b/handlers/corporate/mcudetail.handlers.go index 56d2d2f..5253937 100644 --- a/handlers/corporate/mcudetail.handlers.go +++ b/handlers/corporate/mcudetail.handlers.go @@ -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, diff --git a/handlers/corporate/patient.handlers.go b/handlers/corporate/patient.handlers.go index b479979..7af627e 100644 --- a/handlers/corporate/patient.handlers.go +++ b/handlers/corporate/patient.handlers.go @@ -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) diff --git a/handlers/public/login.handlers.go b/handlers/public/login.handlers.go index fadab93..f78fb3c 100644 --- a/handlers/public/login.handlers.go +++ b/handlers/public/login.handlers.go @@ -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 := ` ` 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 := ` + + ` + c.Response().Header().Set("HX-Trigger", "script") + return c.String(http.StatusOK, ret) +} diff --git a/handlers/routes.go b/handlers/routes.go index cab47b6..a3b0f4c 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -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) diff --git a/views/corporate/patient/patient.templ b/views/corporate/patient/patient.templ index 1afdb26..b1f9e55 100644 --- a/views/corporate/patient/patient.templ +++ b/views/corporate/patient/patient.templ @@ -151,14 +151,7 @@ templ JSDashboardPat() { document.getElementById('userEmail').value = parsedData.M_UserEmail; } - }); - - document.body.addEventListener('htmx:configRequest', function(evt) { - const token = localStorage.getItem("token"); - if (token) { - evt.detail.headers['Authentication'] = "Bearer " + token; - } - }); + }); } diff --git a/views/corporate/patient/patient_templ.go b/views/corporate/patient/patient_templ.go index da13271..da3acc4 100644 --- a/views/corporate/patient/patient_templ.go +++ b/views/corporate/patient/patient_templ.go @@ -267,7 +267,7 @@ func JSDashboardPat() templ.Component { templ_7745c5c3_Var7 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/views/public/login/login.templ b/views/public/login/login.templ index 8058e20..79a9572 100644 --- a/views/public/login/login.templ +++ b/views/public/login/login.templ @@ -94,14 +94,6 @@ templ CssLogin() { templ JsLogin() { - } templ ShowLogin(title string, cmp templ.Component, css templ.Component, js templ.Component) { diff --git a/views/public/login/login_templ.go b/views/public/login/login_templ.go index d244b06..1d9ea00 100644 --- a/views/public/login/login_templ.go +++ b/views/public/login/login_templ.go @@ -107,7 +107,7 @@ func JsLogin() templ.Component { templ_7745c5c3_Var3 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }