add login multi api & redirect

This commit is contained in:
2024-06-12 09:22:42 +07:00
parent 2058220370
commit 17c70e13fc
11 changed files with 197 additions and 40 deletions

View File

@@ -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.",

View File

@@ -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", "", "",

View File

@@ -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)
}

View File

@@ -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)

29
models/login.models.go Normal file
View File

@@ -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"`
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -4,7 +4,9 @@ import (
"cpone/layout"
)
templ MainLogin() {
templ MainLogin(
responseBody string,
) {
<div class="d-flex flex-column flex-root">
<div
class="login login-1 login-signin-on d-flex flex-column flex-lg-row flex-column-fluid bg-white"
@@ -22,7 +24,7 @@ templ MainLogin() {
<!--begin::Signin-->
<div class="login-form login-signin">
<!--begin::Form-->
@FormSignin()
@FormSignin(responseBody)
<!--end::Form-->
</div>
<!--end::Signin-->

View File

@@ -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
}

View File

@@ -31,7 +31,9 @@ templ AsideLeft() {
</div>
}
templ FormSignin() {
templ FormSignin(
responseBody string,
) {
<form
class="form"
novalidate="novalidate"
@@ -113,6 +115,10 @@ templ FormSignin() {
<!--begin::Action-->
<div class="pb-lg-0 pb-5 pt-5">
<button
hx-post="/login/signin"
hx-swap="outerHTML"
hx-target="#kt_login"
hx-on::after-request={ StoreLocalStorage(responseBody) }
type="button"
id="kt_login_signin_submit"
class="btn btn-block font-weight-bolder text-white font-size-h6 px-8 py-4 my-3 mr-3 mt-5 btn-primary"
@@ -272,3 +278,8 @@ templ ForgotPassword() {
<!--end::Form group-->
</form>
}
script StoreLocalStorage(responseBody string) {
console.log("here");
console.log(responseBody);
}

View File

@@ -34,7 +34,9 @@ func AsideLeft() templ.Component {
})
}
func FormSignin() templ.Component {
func FormSignin(
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 {
@@ -47,7 +49,24 @@ func FormSignin() templ.Component {
templ_7745c5c3_Var2 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<form class=\"form\" novalidate=\"novalidate\" id=\"kt_login_signin_form\"><!--begin::Title--><div class=\"d-flex justify-content-center mb-5\"><img src=\"/asset-corporate-portal/media/logo/logo.png\" alt=\"\" style=\"height: 103px\"></div><div class=\"pt-2\"><h3 class=\"d-flex justify-content-center title-company text-dark\">Welcome to Corporate Portal\r</h3></div><div class=\"pb-10 pt-0\"><h3 class=\"subtitle-company\" style=\"color: #a7a8bb\">Sign in to access your account\r</h3></div><!--begin::Title--><!--begin::Form group--><div class=\"form-group\"><label class=\"font-size-h6 font-weight-bolder text-dark\">Email</label> <input class=\"form-control form-control-solid h-auto py-7 px-6 rounded-lg\" type=\"text\" name=\"username\" autocomplete=\"off\" placeholder=\"Enter your email\"></div><!--end::Form group--><!--begin::Form group--><div class=\"form-group\"><div class=\"d-flex justify-content-between mt-n5\"><label class=\"font-size-h6 font-weight-bolder text-dark pt-5\">Password</label></div><div class=\"input-group\"><!-- form-control form-control-solid --><input class=\"h-auto form-control form-control-solid py-7 px-6 rounded-lg rounded-right-0\" type=\"password\" name=\"password\" id=\"user-password\" autocomplete=\"off\" placeholder=\"Enter your password\"><div class=\"input-group-append\"><span class=\"input-group-text rounded-lg rounded-left-0\" style=\"border: 0\"><!-- far fa-eye-slash --><a class=\"btn\" role=\"button\" id=\"btn-show\"><i class=\"far fa-eye-slash\" id=\"password-icon\"></i></a></span></div></div></div><div class=\"d-flex justify-content-end\"><a href=\"javascript:;\" class=\"font-size-h6 font-weight-bolder text-hover-primary mb-5 text-primary\" id=\"kt_login_forgot\">Forgot Password ?\r</a></div><!--end::Form group--><!--begin::Action--><div class=\"pb-lg-0 pb-5 pt-5\"><button type=\"button\" id=\"kt_login_signin_submit\" class=\"btn btn-block font-weight-bolder text-white font-size-h6 px-8 py-4 my-3 mr-3 mt-5 btn-primary\">Sign In\r</button></div><!--end::Action--></form>")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<form class=\"form\" novalidate=\"novalidate\" id=\"kt_login_signin_form\"><!--begin::Title--><div class=\"d-flex justify-content-center mb-5\"><img src=\"/asset-corporate-portal/media/logo/logo.png\" alt=\"\" style=\"height: 103px\"></div><div class=\"pt-2\"><h3 class=\"d-flex justify-content-center title-company text-dark\">Welcome to Corporate Portal\r</h3></div><div class=\"pb-10 pt-0\"><h3 class=\"subtitle-company\" style=\"color: #a7a8bb\">Sign in to access your account\r</h3></div><!--begin::Title--><!--begin::Form group--><div class=\"form-group\"><label class=\"font-size-h6 font-weight-bolder text-dark\">Email</label> <input class=\"form-control form-control-solid h-auto py-7 px-6 rounded-lg\" type=\"text\" name=\"username\" autocomplete=\"off\" placeholder=\"Enter your email\"></div><!--end::Form group--><!--begin::Form group--><div class=\"form-group\"><div class=\"d-flex justify-content-between mt-n5\"><label class=\"font-size-h6 font-weight-bolder text-dark pt-5\">Password</label></div><div class=\"input-group\"><!-- form-control form-control-solid --><input class=\"h-auto form-control form-control-solid py-7 px-6 rounded-lg rounded-right-0\" type=\"password\" name=\"password\" id=\"user-password\" autocomplete=\"off\" placeholder=\"Enter your password\"><div class=\"input-group-append\"><span class=\"input-group-text rounded-lg rounded-left-0\" style=\"border: 0\"><!-- far fa-eye-slash --><a class=\"btn\" role=\"button\" id=\"btn-show\"><i class=\"far fa-eye-slash\" id=\"password-icon\"></i></a></span></div></div></div><div class=\"d-flex justify-content-end\"><a href=\"javascript:;\" class=\"font-size-h6 font-weight-bolder text-hover-primary mb-5 text-primary\" id=\"kt_login_forgot\">Forgot Password ?\r</a></div><!--end::Form group--><!--begin::Action--><div class=\"pb-lg-0 pb-5 pt-5\">")
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("<button hx-post=\"/login/signin\" hx-swap=\"outerHTML\" hx-target=\"#kt_login\" hx-on::after-request=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 templ.ComponentScript = StoreLocalStorage(responseBody)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var3.Call)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" type=\"button\" id=\"kt_login_signin_submit\" class=\"btn btn-block font-weight-bolder text-white font-size-h6 px-8 py-4 my-3 mr-3 mt-5 btn-primary\">Sign In\r</button></div><!--end::Action--></form>")
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("<form class=\"form\" novalidate=\"novalidate\" id=\"kt_login_signup_form\"><!--begin::Title --><div class=\"pb-13 pt-lg-0 pt-5\"><h3 class=\"font-weight-bolder text-dark font-size-h4 font-size-h1-lg\">Sign Up\r</h3><p class=\"text-muted font-weight-bold font-size-h4\">Enter your details to create your account\r</p></div><!--end::Title --><!--begin::Form group--><div class=\"form-group\"><input class=\"form-control form-control-solid h-auto py-7 px-6 rounded-lg font-size-h6\" type=\"text\" placeholder=\"Fullname\" name=\"fullname\" autocomplete=\"off\"></div><!--end::Form group--><!--begin::Form group--><div class=\"form-group\"><input class=\"form-control form-control-solid h-auto py-7 px-6 rounded-lg font-size-h6\" type=\"email\" placeholder=\"Email\" name=\"email\" autocomplete=\"off\"></div><!--end::Form group--><!--begin::Form group--><div class=\"form-group\"><input class=\"form-control form-control-solid h-auto py-7 px-6 rounded-lg font-size-h6\" type=\"password\" placeholder=\"Password\" name=\"password\" autocomplete=\"off\"></div><!--end::Form group--><!--begin::Form group--><div class=\"form-group\"><input class=\"form-control form-control-solid h-auto py-7 px-6 rounded-lg font-size-h6\" type=\"password\" placeholder=\"Confirm password\" name=\"cpassword\" autocomplete=\"off\"></div><!--end::Form group--><!--begin::Form group--><div class=\"form-group\"><label class=\"checkbox mb-0\"><input type=\"checkbox\" name=\"agree\">I Agree the\r <a href=\"#\">terms and conditions</a>.\r <span></span></label></div><!--end::Form group--><!--begin::Form group--><div class=\"form-group d-flex flex-wrap pb-lg-0 pb-3\"><button type=\"button\" id=\"kt_login_signup_submit\" class=\"btn btn-primary font-weight-bolder font-size-h6 px-8 py-4 my-3 mr-4\">Submit\r</button> <button type=\"button\" id=\"kt_login_signup_cancel\" class=\"btn btn-light-primary font-weight-bolder font-size-h6 px-8 py-4 my-3\">Cancel\r</button></div><!--end::Form group--></form>")
@@ -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("<form class=\"form\" novalidate=\"novalidate\" id=\"kt_login_forgot_form\"><!--begin::Title--><div class=\"d-flex justify-content-center mb-5\"><img src=\"/asset-corporate-portal/media/logo/logo.png\" alt=\"\" style=\"height: 103px\"></div><div class=\"pb-13 pt-lg-0 pt-5\"><h3 class=\"font-weight-bolder text-dark font-size-h4 font-size-h1-lg\">Forgotten Password ?\r</h3><p class=\"text-muted font-weight-bold font-size-h4\">Enter your email to reset your password\r</p></div><!--end::Title--><!--begin::Form group--><div class=\"form-group\"><input class=\"form-control form-control-solid h-auto py-7 px-6 rounded-lg font-size-h6\" type=\"email\" placeholder=\"Email\" name=\"email\" autocomplete=\"off\"></div><!--end::Form group--><!--begin::Form group--><div class=\"form-group d-flex flex-wrap pb-lg-0\"><button type=\"button\" id=\"kt_login_forgot_submit\" class=\"btn btn-primary font-weight-bolder font-size-h6 px-8 py-4 my-3 btn-block btn-submit\">Submit\r</button> <button type=\"button\" id=\"kt_login_forgot_cancel\" class=\"btn btn-light-primary font-weight-bolder font-size-h6 px-8 py-4 my-3 btn-block btn-cancel\">Cancel\r</button></div><!--end::Form group--></form>")
@@ -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),
}
}