@@ -28,18 +28,18 @@ export class IoRedisSessionStore {
28
28
private readonly cookieOptions : CookieSerializeOptions ;
29
29
30
30
constructor ( {
31
- redisClient,
32
- secret,
33
- cookieName = 'session' ,
34
- sessionPrefix = 'sk_ioredis_session' ,
35
- userSessionsPrefix = 'sk_ioredis_user_sessions' ,
36
- signed = true ,
37
- useTTL = true ,
38
- renewSessionBeforeExpire = false ,
39
- renewBeforeSeconds = defaultRenewBeforeSeconds ,
40
- serializer = JSON ,
41
- cookiesOptions = { }
42
- } : ioRedisSessionOptions ) {
31
+ redisClient,
32
+ secret,
33
+ cookieName = 'session' ,
34
+ sessionPrefix = 'sk_ioredis_session' ,
35
+ userSessionsPrefix = 'sk_ioredis_user_sessions' ,
36
+ signed = true ,
37
+ useTTL = true ,
38
+ renewSessionBeforeExpire = false ,
39
+ renewBeforeSeconds = defaultRenewBeforeSeconds ,
40
+ serializer = JSON ,
41
+ cookiesOptions = { }
42
+ } : ioRedisSessionOptions ) {
43
43
if ( ! redisClient ) {
44
44
throw new Error ( 'A pre-initiated redis client must be provided to the RedisStore' ) ;
45
45
}
@@ -89,11 +89,13 @@ export class IoRedisSessionStore {
89
89
console . log ( 'Error in Set Session while serializing' , er ) ;
90
90
return formattedReturn ( null , true , 'Unable to stringify session data.' ) ;
91
91
}
92
+
93
+ const prefixedSessionKey = getSessionKey ( this . sessionPrefix , sessionKey ) ;
92
94
const redisPipeline = this . redisClient . pipeline ( ) ;
93
- redisPipeline . set ( getSessionKey ( this . sessionPrefix , sessionKey ) , serializedSessionData ) ;
94
- redisPipeline . sadd ( getUserSessionKey ( this . userSessionsPrefix , userId ) , getSessionKey ( this . sessionPrefix , sessionKey ) ) ;
95
+ redisPipeline . set ( prefixedSessionKey , serializedSessionData ) ;
96
+ redisPipeline . sadd ( getUserSessionKey ( this . userSessionsPrefix , userId ) , sessionKey ) ;
95
97
if ( this . useTTL && this . ttlSeconds ) {
96
- redisPipeline . expire ( sessionKey , this . ttlSeconds ) ;
98
+ redisPipeline . expire ( prefixedSessionKey , this . ttlSeconds ) ;
97
99
}
98
100
await redisPipeline . exec ( ) ;
99
101
if ( this . signedCookies ) {
@@ -111,8 +113,10 @@ export class IoRedisSessionStore {
111
113
} = await validateCookie ( cookies , this . cookieName , this . secret , this . signedCookies ) ;
112
114
if ( error ) return formattedReturn ( sessionId , error , message ) ;
113
115
const sessionData = await this . redisClient . get ( getSessionKey ( this . sessionPrefix , sessionId ) ) ;
116
+
114
117
if ( ! sessionData )
115
118
return formattedReturn ( null , true , `Unable to find data for the provided key - ${ sessionId } ` ) ;
119
+
116
120
let parsedSession ;
117
121
try {
118
122
parsedSession = this . serializer . parse ( sessionData ) ;
@@ -158,7 +162,7 @@ export class IoRedisSessionStore {
158
162
} ;
159
163
// till here
160
164
161
- deleteSession = async ( cookies : Cookies ) => {
165
+ deleteSession = async ( cookies : Cookies , userId = null ) => {
162
166
const {
163
167
data : sessionId ,
164
168
error,
@@ -168,8 +172,19 @@ export class IoRedisSessionStore {
168
172
console . log ( 'Error in delSession method' , message ) ;
169
173
return formattedReturn ( sessionId , error , 'Unable to validate key while deleting' ) ;
170
174
}
171
- const deleteData = await this . redisClient . del ( getSessionKey ( this . sessionPrefix , sessionId ) ) ;
172
- if ( ! deleteData ) return formattedReturn ( null , true , `Key not found while deleting` ) ;
175
+ const prefixedSessionKey = getSessionKey ( this . sessionPrefix , sessionId ) ;
176
+ const sessionData = await this . redisClient . get ( prefixedSessionKey ) ;
177
+ if ( ! sessionData ) return formattedReturn ( sessionId , true , `Not a valid session key` ) ;
178
+
179
+ if ( userId ) {
180
+ const redisPipeline = this . redisClient . pipeline ( ) ;
181
+ redisPipeline . del ( prefixedSessionKey ) ;
182
+ redisPipeline . srem ( getUserSessionKey ( this . userSessionsPrefix , userId ) , sessionId ) ;
183
+ await redisPipeline . exec ( )
184
+ } else {
185
+ await this . redisClient . del ( prefixedSessionKey ) ;
186
+ }
187
+
173
188
await this . deleteCookie ( cookies ) ;
174
189
return formattedReturn ( sessionId , false , `Key successfully deleted` ) ; // Returns unique key without prefix which is deleted from redis
175
190
} ;
0 commit comments