38 lines
958 B
Go
38 lines
958 B
Go
package utils
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"legalgo-BE-go/config"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func GetTokenRedis(ctx context.Context, rdb *redis.Client, id string) (string, error) {
|
|
val, err := rdb.Get(ctx, "token-"+id).Result()
|
|
|
|
switch {
|
|
case err == redis.Nil:
|
|
return "", fmt.Errorf("token does not exist")
|
|
case err != nil:
|
|
return "", err
|
|
case val == "":
|
|
return "", fmt.Errorf("token does not exist")
|
|
}
|
|
|
|
return val, nil
|
|
}
|
|
|
|
func StoreTokenRedis(ctx context.Context, rdb *redis.Client, token, id string) error {
|
|
_, err := rdb.Get(ctx, "token-"+id).Result()
|
|
if err != redis.Nil {
|
|
// If there's an existing token, delete it (expire the previous session)
|
|
if err := rdb.Del(ctx, "token-"+id).Err(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// return rdb.Set(ctx context.Context, key string, value interface{}, expiration time.Duration)
|
|
return rdb.Set(ctx, "token-"+id, token, time.Minute*time.Duration(config.REDIS_TIMEOUT)).Err()
|
|
}
|