-
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.
feat(common): adding getSession method (#160)
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
packages/auth-common/src/components/__tests__/getSession.test.ts
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,25 @@ | ||
import { type HeadersLike, getSession } from ".."; | ||
|
||
describe("getSession", () => { | ||
const clientId = "testClient"; | ||
|
||
it("should return empty string if no session is found", () => { | ||
const headers: HeadersLike = {}; | ||
expect(getSession({ headers, clientId })).toBe(""); | ||
}); | ||
|
||
it("should extract session from cookie", () => { | ||
const session = "some-session"; | ||
const headers: HeadersLike = { | ||
cookie: `auth.${clientId}.session=${session};`, | ||
}; | ||
expect(getSession({ headers, clientId })).toBe(session); | ||
}); | ||
|
||
it("should return empty string if cookie does not contain the session", () => { | ||
const headers: HeadersLike = { | ||
cookie: "someOtherCookie=value;", | ||
}; | ||
expect(getSession({ headers, clientId })).toBe(""); | ||
}); | ||
}); |
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,31 @@ | ||
import type { HeadersLike } from "./getToken"; | ||
|
||
const getFromCookie = (headers: HeadersLike, clientId: string) => { | ||
const cookie = headers?.cookie; | ||
if (typeof cookie !== "string") { | ||
return; | ||
} | ||
const re = new RegExp(`auth.${clientId}.session=(.+?)(?:;|$)`); | ||
const match = cookie.match(re); | ||
if (!match) { | ||
return; | ||
} | ||
return match[1]; | ||
}; | ||
|
||
/** | ||
* Get a Session Id from a request. | ||
* | ||
* @param headers An object containing the request headers, usually `req.headers`. | ||
* @param clientId The client ID to use. | ||
* | ||
*/ | ||
type GetSessionProps = { | ||
clientId: string; | ||
headers: HeadersLike; | ||
}; | ||
export const getSession = ({ headers, clientId }: GetSessionProps): string => { | ||
const fromCookie = getFromCookie(headers, clientId); | ||
|
||
return fromCookie || ""; | ||
}; |
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