Feat/multiple session (#64)

* fix: disable windows build

* feat: add ability to handle multiple sessions
This commit is contained in:
Lakhan Samani
2021-10-27 23:15:38 +05:30
committed by GitHub
parent 4649391169
commit b69d0b8e23
21 changed files with 301 additions and 53 deletions

View File

@@ -2,6 +2,7 @@ package session
import (
"context"
"fmt"
"log"
"github.com/go-redis/redis/v8"
@@ -12,20 +13,29 @@ type RedisStore struct {
store *redis.Client
}
func (c *RedisStore) AddToken(userId, token string) {
err := c.store.Set(c.ctx, "authorizer_"+userId, token, 0).Err()
func (c *RedisStore) AddToken(userId, accessToken, refreshToken string) {
err := c.store.HMSet(c.ctx, "authorizer_"+userId, map[string]string{
accessToken: refreshToken,
}).Err()
if err != nil {
log.Fatalln("Error saving redis token:", err)
}
}
func (c *RedisStore) DeleteToken(userId string) {
func (c *RedisStore) DeleteUserSession(userId string) {
err := c.store.Del(c.ctx, "authorizer_"+userId).Err()
if err != nil {
log.Fatalln("Error deleting redis token:", err)
}
}
func (c *RedisStore) DeleteToken(userId, accessToken string) {
err := c.store.HDel(c.ctx, "authorizer_"+userId, accessToken).Err()
if err != nil {
log.Fatalln("Error deleting redis token:", err)
}
}
func (c *RedisStore) ClearStore() {
err := c.store.Del(c.ctx, "authorizer_*").Err()
if err != nil {
@@ -33,11 +43,38 @@ func (c *RedisStore) ClearStore() {
}
}
func (c *RedisStore) GetToken(userId string) string {
func (c *RedisStore) GetToken(userId, accessToken string) string {
token := ""
token, err := c.store.Get(c.ctx, "authorizer_"+userId).Result()
res, err := c.store.HMGet(c.ctx, "authorizer_"+userId, accessToken).Result()
if err != nil {
log.Println("Error getting token from redis store:", err)
}
if len(res) > 0 && res[0] != nil {
token = fmt.Sprintf("%v", res[0])
}
return token
}
func (c *RedisStore) SetSocialLoginState(key, state string) {
err := c.store.Set(c.ctx, key, state, 0).Err()
if err != nil {
log.Fatalln("Error saving redis token:", err)
}
}
func (c *RedisStore) GetSocialLoginState(key string) string {
state := ""
state, err := c.store.Get(c.ctx, key).Result()
if err != nil {
log.Println("Error getting token from redis store:", err)
}
return state
}
func (c *RedisStore) RemoveSocialLoginState(key string) {
err := c.store.Del(c.ctx, key).Err()
if err != nil {
log.Fatalln("Error deleting redis token:", err)
}
}