feat (auth): rework logout

This commit is contained in:
dpurbosakti
2025-08-19 15:21:06 +07:00
parent a448f43b67
commit 4a6c6e28bc
4 changed files with 13 additions and 9 deletions
@@ -15,7 +15,7 @@ type authKey string
const akInfo authKey = "authInfo"
type Key struct{}
type AuthKey struct{}
// var Position m.Position
@@ -35,12 +35,12 @@ func Login(w http.ResponseWriter, r *http.Request) {
}
func Logout(w http.ResponseWriter, r *http.Request) {
authInfoContext := context.Context.Value(r.Context(), akInfo)
if authInfoContext == nil {
ctxVal := r.Context().Value(AuthKey{})
if ctxVal == nil {
rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": "logout skiped. the request is done wihtout authorization."}, nil)
return
}
authInfo := context.Context.Value(r.Context(), akInfo).(*s.AuthInfo)
authInfo := ctxVal.(*s.AuthInfo)
s.RevokeToken(authInfo.Uuid)
rw.WriteJSON(w, http.StatusOK, d.IS{"message": "logged out"}, nil)
}
@@ -52,7 +52,7 @@ func GuardMW(next http.Handler) http.Handler {
rw.WriteJSON(w, http.StatusUnauthorized, err.(d.FieldError), nil)
return
}
ctx := context.WithValue(r.Context(), Key{}, accessDetail)
ctx := context.WithValue(r.Context(), AuthKey{}, accessDetail)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
@@ -9,6 +9,7 @@ import (
/******************** external ********************/
a "github.com/karincake/apem"
hk "github.com/karincake/hongkue"
/******************** infra ********************/
gs "simrs-vx/internal/infra/gorm-setting"
@@ -17,6 +18,7 @@ import (
/******************** pkg ********************/
cmw "simrs-vx/pkg/cors-manager-mw"
hc "simrs-vx/pkg/handler-crud-helper"
lh "simrs-vx/pkg/lang-helper"
handlerlogger "simrs-vx/pkg/middleware/handler-logger"
///// Internal
@@ -28,6 +30,7 @@ func SetRoutes() http.Handler {
/////
a.RegisterExtCall(gs.Adjust)
a.RegisterExtCall(ssdb.Init)
a.RegisterExtCall(lh.Populate)
r := http.NewServeMux()
@@ -35,7 +38,8 @@ func SetRoutes() http.Handler {
r.HandleFunc("/", home.Home)
r.HandleFunc("POST /v1/authentication/login", auth.Login)
r.HandleFunc("POST /v1/authentication/logout", auth.Logout)
// r.HandleFunc("POST /v1/authentication/logout", auth.Logout)
hk.Route("POST /v1/authentication/logout", r, auth.GuardMW, auth.Logout)
hc.RegCrud(r, "/v1/user", user.O)
@@ -9,7 +9,7 @@ import (
"github.com/golang-jwt/jwt"
"github.com/google/uuid"
dg "github.com/karincake/apem/db-gorm-mysql"
dg "github.com/karincake/apem/db-gorm-pg"
d "github.com/karincake/dodol"
l "github.com/karincake/lepet"
@@ -1,12 +1,12 @@
package authentication
import (
dg "github.com/karincake/apem/db-gorm-mysql"
dg "github.com/karincake/apem/db-gorm-pg"
)
// just return the error code
func GetAndCheck(input, condition any) (eCode string) {
result := dg.I.Where(condition).Find(input)
result := dg.I.Where(condition).Find(&input)
if result.Error != nil {
return "fetch-fail"
} else if result.RowsAffected == 0 {