added adduser registeruser changepassword deluser changeuser

This commit is contained in:
wt committed 2025-05-27 22:07:50 +07:00
1 parent 55dbc36b92
commit c65d6d5be5
8 files changed
+211 -24

No files matched your search

+5 -5
View File
@@ -14,7 +14,7 @@ import (
const defaultsecretkey = "secret"
func createJWT(secretKey string, data map[string]interface{}, ttl time.Duration) (string, error) {
func createJWT(secretKey string, data map[string]any, ttl time.Duration) (string, error) {
claims := jwt.MapClaims{
"exp": time.Now().Add(ttl).Unix(),
"iat": time.Now().Unix(),
@@ -29,10 +29,10 @@ func createJWT(secretKey string, data map[string]interface{}, ttl time.Duration)
return signedToken, nil
}
func VerifyJWT(tokenString string, secretKey string) (map[string]interface{}, error) {
func VerifyJWT(tokenString string, secretKey string) (map[string]any, error) {
token, e := jwt.Parse(
tokenString,
func(token *jwt.Token) (interface{}, error) {
func(token *jwt.Token) (any, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpexted signing method: %v", token.Header["alg"])
}
@@ -43,7 +43,7 @@ func VerifyJWT(tokenString string, secretKey string) (map[string]interface{}, er
return nil, fmt.Errorf("token validation failed: %w", e)
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
result := make(map[string]interface{})
result := make(map[string]any)
for key, value := range claims {
if key == "exp" || key == "iat" || key == "nbf" {
continue
@@ -74,7 +74,7 @@ func Token(w http.ResponseWriter, req *http.Request) {
IsSuperuser: dbuser.IsSuperuser,
}
responseUserMarshal, _ := json.Marshal(responseUser)
responseUserUnmarshal := make(map[string]interface{})
responseUserUnmarshal := make(map[string]any)
json.Unmarshal(responseUserMarshal, &responseUserUnmarshal)
token, _ := createJWT(defaultsecretkey, responseUserUnmarshal, time.Duration(24*time.Hour))
fmt.Fprintf(w, "{\"token\":\"%s\"}", token)