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