-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using http only cookie and blacklist tokens
- Loading branch information
Showing
18 changed files
with
238 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Request, Response } from "express"; | ||
import { setDataAsSetInRedis } from "../../redis/set-data-as-set"; | ||
|
||
export const logout = async (req: Request, res: Response) => { | ||
try { | ||
const accesstoken = req.cookies.accesstoken; | ||
const refreshtoken = req.cookies.refreshtoken; | ||
if (!accesstoken || !refreshtoken) { | ||
return res | ||
.status(401) | ||
.json({ success: false, message: "token is missing" }); | ||
} | ||
|
||
// blacklist the tokens | ||
await setDataAsSetInRedis({ | ||
key: "blacklistedTokens", | ||
data: [accesstoken, refreshtoken], | ||
isString: true, | ||
}); | ||
|
||
res.clearCookie("accesstoken"); | ||
res.clearCookie("refreshtoken"); | ||
|
||
res.status(200).json({ success: true, message: "logged out successfully" }); | ||
} catch (error) { | ||
res.status(500).json(error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { redisClient } from "../utils/redis"; | ||
|
||
export const checkItemInSetRedis = async ( | ||
key: string, | ||
item: string | ||
): Promise<boolean> => { | ||
try { | ||
const isMember = await redisClient.sismember(key, item); | ||
return isMember === 1; | ||
} catch (error) { | ||
console.error(`Error checking item in set ${key}:`, error); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { redisClient } from "../utils/redis"; | ||
|
||
export const setDataAsSetInRedis = async ({ | ||
key, | ||
data, | ||
expirationTimeInSeconds, | ||
isString, | ||
}: { | ||
key: string; | ||
data: Array<any>; | ||
expirationTimeInSeconds?: number; | ||
isString?: boolean; | ||
}) => { | ||
try { | ||
const stringDataArray = data.map((d) => (isString ? d : JSON.stringify(d))); | ||
|
||
if (expirationTimeInSeconds) { | ||
return await redisClient | ||
.multi() | ||
.sadd(key, ...stringDataArray) | ||
.expire(key, expirationTimeInSeconds) | ||
.exec(); | ||
} else { | ||
return await redisClient.sadd(key, ...stringDataArray); | ||
} | ||
} catch (error) { | ||
console.error("Error setting data as set in Redis:", error); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.