diff --git a/assets/asset-corporate-portal/js/login-general.js b/assets/asset-corporate-portal/js/login-general.js index 5911cec..cc4edab 100644 --- a/assets/asset-corporate-portal/js/login-general.js +++ b/assets/asset-corporate-portal/js/login-general.js @@ -54,17 +54,17 @@ var KTLogin = function () { validation.validate().then(function (status) { if (status == 'Valid') { - swal.fire({ - text: "All is cool! Now you submit this form", - icon: "success", - buttonsStyling: false, - confirmButtonText: "Ok, got it!", - customClass: { - confirmButton: "btn font-weight-bold btn-light-primary" - } - }).then(function () { - KTUtil.scrollTop(); - }); + // swal.fire({ + // text: "All is cool! Now you submit this form", + // icon: "success", + // buttonsStyling: false, + // confirmButtonText: "Ok, got it!", + // customClass: { + // confirmButton: "btn font-weight-bold btn-light-primary" + // } + // }).then(function () { + // KTUtil.scrollTop(); + // }); } else { swal.fire({ text: "Sorry, looks like there are some errors detected, please try again.", diff --git a/handlers/dev/employeeanalytic.handlers.go b/handlers/dev/employeeanalytic.handlers.go index 34a3f1a..9c67c77 100644 --- a/handlers/dev/employeeanalytic.handlers.go +++ b/handlers/dev/employeeanalytic.handlers.go @@ -69,7 +69,7 @@ func (ea *EmployeeAnalyticHandler) HandleShowEmployeeAnalyticScreen(c echo.Conte paginationcomponent := pagination.PaginationV3( totalPage, 1, - "/dev/employeeanalytic/changepage", + "/dev/dashboard_pic/changepage", paginationID, "#listID, #paginationID, #searchID, #startdateID, #enddateID", "#"+paginationID, @@ -79,7 +79,7 @@ func (ea *EmployeeAnalyticHandler) HandleShowEmployeeAnalyticScreen(c echo.Conte ) filtercomponent := dev_employeeanalytic.FilterListing( - "/dev/employeeanalytic/filter", + "/dev/dashboard_pic/filter", "input changed delay:500ms", "#searchID, #startdateID, #enddateID, #listID, #paginationID", "outerHTML", @@ -132,7 +132,7 @@ func (ea *EmployeeAnalyticHandler) HandlePagination(c echo.Context) error { paginationcomponent := pagination.PaginationV3( totalPage, page, - "/dev/employeeanalytic/changepage", + "/dev/dashboard_pic/changepage", paginationID, "#listID, #paginationID, #searchID, #startdateID, #enddateID", "#"+paginationID, @@ -165,7 +165,7 @@ func (ea *EmployeeAnalyticHandler) HandleFilter(c echo.Context) error { listcomponent := dev_employeeanalytic.ListingData(dataList, listID) paginationcomponent := pagination.PaginationV3( totalPage, 1, - "/dev/employeeanalytic/changepage", + "/dev/dashboard_pic/changepage", paginationID, "#listID, #paginationID, #searchID, #startdateID, #enddateID", "#"+paginationID, "outerHTML", "", "", diff --git a/handlers/public/login.handlers.go b/handlers/public/login.handlers.go index f703162..27f9e40 100644 --- a/handlers/public/login.handlers.go +++ b/handlers/public/login.handlers.go @@ -1,13 +1,16 @@ package public_handlers import ( + "cpone/models" "cpone/utils" public_login "cpone/views/public/login" "github.com/labstack/echo/v4" + "go.uber.org/zap" ) type LoginService interface { + Login(username string, password string) (models.ResponseApi, error) } func NewLoginHandler(us LoginService) *LoginHandler { @@ -20,14 +23,39 @@ type LoginHandler struct { LoginService LoginService } -// func (uh *LoginHandler) ShowLogin(c echo.Context) error { -// helo := client_Login.Show() -// return utils.View(c, helo) -// } - func (lh *LoginHandler) HandlerShowLogin(c echo.Context) error { - si := public_login.ShowLogin("Login ", public_login.MainLogin(), public_login.CssLogin(), public_login.JsLogin()) + si := public_login.ShowLogin("Login ", public_login.MainLogin(""), public_login.CssLogin(), public_login.JsLogin()) return utils.View(c, si) } + +func (lh *LoginHandler) HandlerSignIn(c echo.Context) error { + logger, _ := zap.NewProduction() + + username := c.FormValue("username") + password := c.FormValue("password") + + defer logger.Sync() + logger.Info("PArams", zap.Any("username", username), zap.Any("pass", password)) + + resp, err := lh.LoginService.Login(username, password) + if err != nil { + defer logger.Sync() + logger.Info("Error", zap.Error(err)) + + si := public_login.ShowLogin("Login ", public_login.MainLogin(""), public_login.CssLogin(), public_login.JsLogin()) + return utils.View(c, si) + } + defer logger.Sync() + logger.Info("resp", zap.Any("token", resp.Data.Token)) + + // url := "/one-ui" + url := "/home" + + if resp.Data.Type == "pic" { + url = "/dashboard_pic" + } + + return utils.Redirect(c, url) +} diff --git a/handlers/routes.go b/handlers/routes.go index 9327160..7878a99 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -67,8 +67,13 @@ func SetupRoutesPublic(app *echo.Echo, appStore db.AppStore) { lh := public_handlers.NewLandingPageHandler(l) public.GET("landingpage", lh.ShowLandingPage) public.GET("home", lh.ShowLandingPage) - loginHdr := public_handlers.NewLoginHandler(l) - public.GET("login", loginHdr.HandlerShowLogin) + // loginHdr := public_handlers.NewLoginHandler(l) + // public.GET("login", loginHdr.HandlerShowLogin) + + loginServ := public_services.NewServicesLogin(appStore) + loginHadlr := public_handlers.NewLoginHandler(loginServ) + public.GET("login", loginHadlr.HandlerShowLogin) + public.POST("login/signin", loginHadlr.HandlerSignIn) // kartu kontrol publicKartuKontrolServices := public_services.NewServicesKartuKontrol(appStore) @@ -236,9 +241,9 @@ func SetupRoutesDev(app *echo.Echo, appStore db.AppStore) { // employee analytic devEmplAnaServices := dev_services.NewEmployeeAnalyticServices(appStore) devEmplAnaHandler := dev_handlers.NewEmployeeAnalyticHandler(devEmplAnaServices) - dev.GET("/employeeanalytic", devEmplAnaHandler.HandleShowEmployeeAnalyticScreen) - dev.GET("/employeeanalytic/changepage", devEmplAnaHandler.HandlePagination) - dev.GET("/employeeanalytic/filter", devEmplAnaHandler.HandleFilter) + dev.GET("/dashboard_pic", devEmplAnaHandler.HandleShowEmployeeAnalyticScreen) + dev.GET("/dashboard_pic/changepage", devEmplAnaHandler.HandlePagination) + dev.GET("/dashboard_pic/filter", devEmplAnaHandler.HandleFilter) // group result devGRServices := dev_services.NewServicesGroupResult(appStore) diff --git a/models/login.models.go b/models/login.models.go new file mode 100644 index 0000000..272256e --- /dev/null +++ b/models/login.models.go @@ -0,0 +1,29 @@ +package models + +type ResponseApi struct { + Status string `json:"status"` + Data struct { + User struct { + MUserID string `json:"M_UserID"` + MUserEmail string `json:"M_UserEmail"` + MUserUsername string `json:"M_UserUsername"` + MUserGroupDashboard string `json:"M_UserGroupDashboard"` + MUserDefaultTSampleStationID string `json:"M_UserDefaultT_SampleStationID"` + MStaffName string `json:"M_StaffName"` + IsCourier string `json:"is_courier"` + TimeAutologout string `json:"time_autologout"` + IP string `json:"ip"` + Agent string `json:"agent"` + Version string `json:"version"` + LastLogin string `json:"last-login"` + } `json:"user"` + Token string `json:"token"` + Type string `json:"type"` + } `json:"data"` + Message string `json:"message"` + Query string `json:"query"` + DbError struct { + Code int `json:"code"` + Message string `json:"message"` + } `json:"db_error"` +} diff --git a/services/public/login.services.go b/services/public/login.services.go index 5ef470b..0a7f6b7 100644 --- a/services/public/login.services.go +++ b/services/public/login.services.go @@ -1,6 +1,14 @@ package public_services -import "cpone/db" +import ( + "bytes" + "cpone/db" + "cpone/models" + "encoding/json" + "fmt" + "io" + "net/http" +) type ServicesLogin struct { CompanyStore db.AppStore @@ -12,3 +20,39 @@ func NewServicesLogin(uStore db.AppStore) *ServicesLogin { CompanyStore: uStore, } } + +func (ls *ServicesLogin) Login(username string, password string) (models.ResponseApi, error) { + url := "https://devcpone.aplikasi.web.id/one-api/v1/system/auth/login" + + var resp models.ResponseApi + + // Create a JSON payload with the username and password + payload := []byte(fmt.Sprintf(`{"username": "%s", "password": "%s"}`, username, password)) + + // Make the POST request + request, err := http.Post(url, "application/json", bytes.NewBuffer(payload)) + if err != nil { + return resp, err + } + if request.StatusCode != http.StatusOK { + return resp, fmt.Errorf("server returned non-OK status: %d", request.StatusCode) + } + defer request.Body.Close() + + // Read the response body + body, err := io.ReadAll(request.Body) + if err != nil { + return resp, err + } + + if err := json.Unmarshal(body, &resp); err != nil { + return resp, err + } + + // Check if status is "ERR" + if resp.Status == "ERR" { + return resp, fmt.Errorf("login failed: %s", resp.Message) + } + + return resp, nil +} diff --git a/utils/view.utils.go b/utils/view.utils.go index d9213cf..4b65a81 100644 --- a/utils/view.utils.go +++ b/utils/view.utils.go @@ -32,3 +32,9 @@ func View(c echo.Context, cmp templ.Component) error { return cmp.Render(c.Request().Context(), c.Response().Writer) } + +func Redirect(c echo.Context, url string) error { + c.Response().Header().Set("HX-Redirect", url) + + return c.String(http.StatusOK, url) +} diff --git a/views/public/login/login.templ b/views/public/login/login.templ index 79a9572..2820085 100644 --- a/views/public/login/login.templ +++ b/views/public/login/login.templ @@ -4,7 +4,9 @@ import ( "cpone/layout" ) -templ MainLogin() { +templ MainLogin( + responseBody string, +) {
diff --git a/views/public/login/login_templ.go b/views/public/login/login_templ.go index 1d9ea00..a32e3ba 100644 --- a/views/public/login/login_templ.go +++ b/views/public/login/login_templ.go @@ -14,7 +14,9 @@ import ( "cpone/layout" ) -func MainLogin() templ.Component { +func MainLogin( + responseBody string, +) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) if !templ_7745c5c3_IsBuffer { @@ -39,7 +41,7 @@ func MainLogin() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = FormSignin().Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = FormSignin(responseBody).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/views/public/login/logincomponent.templ b/views/public/login/logincomponent.templ index 4fdb036..6b4f711 100644 --- a/views/public/login/logincomponent.templ +++ b/views/public/login/logincomponent.templ @@ -31,7 +31,9 @@ templ AsideLeft() {
} -templ FormSignin() { +templ FormSignin( + responseBody string, +) {
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
\"\"

Welcome to Corporate Portal\r

Sign in to access your account\r

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, StoreLocalStorage(responseBody)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -66,9 +85,9 @@ func FormSignup() templ.Component { defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var3 := templ.GetChildren(ctx) - if templ_7745c5c3_Var3 == nil { - templ_7745c5c3_Var3 = templ.NopComponent + templ_7745c5c3_Var4 := templ.GetChildren(ctx) + if templ_7745c5c3_Var4 == nil { + templ_7745c5c3_Var4 = templ.NopComponent } ctx = templ.ClearChildren(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Sign Up\r

Enter your details to create your account\r

") @@ -90,9 +109,9 @@ func ForgotPassword() templ.Component { defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var4 := templ.GetChildren(ctx) - if templ_7745c5c3_Var4 == nil { - templ_7745c5c3_Var4 = templ.NopComponent + templ_7745c5c3_Var5 := templ.GetChildren(ctx) + if templ_7745c5c3_Var5 == nil { + templ_7745c5c3_Var5 = templ.NopComponent } ctx = templ.ClearChildren(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
\"\"

Forgotten Password ?\r

Enter your email to reset your password\r

") @@ -105,3 +124,14 @@ func ForgotPassword() templ.Component { return templ_7745c5c3_Err }) } + +func StoreLocalStorage(responseBody string) templ.ComponentScript { + return templ.ComponentScript{ + Name: `__templ_StoreLocalStorage_ef0d`, + Function: `function __templ_StoreLocalStorage_ef0d(responseBody){console.log("here"); + console.log(responseBody); +}`, + Call: templ.SafeScript(`__templ_StoreLocalStorage_ef0d`, responseBody), + CallInline: templ.SafeScriptInline(`__templ_StoreLocalStorage_ef0d`, responseBody), + } +}