Skip to content

Commit

Permalink
Refactor based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nkshah2 committed Oct 9, 2023
1 parent fbd9645 commit e70667a
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 51 deletions.
2 changes: 1 addition & 1 deletion lib/build/recipe/session/sessionClass.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ export default class Session implements SessionContainerInterface {
setClaimValue<T>(claim: SessionClaim<T>, value: T, userContext?: any): Promise<void>;
getClaimValue<T>(claim: SessionClaim<T>, userContext?: any): Promise<T | undefined>;
removeClaim(claim: SessionClaim<any>, userContext?: any): Promise<void>;
attachToRequestResponse(info: ReqResInfo): void;
attachToRequestResponse(info: ReqResInfo, userContext?: any): void;
}
9 changes: 5 additions & 4 deletions lib/build/recipe/session/sessionClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const utils_1 = require("./utils");
const jwt_1 = require("./jwt");
const logger_1 = require("../../logger");
const constants_1 = require("./constants");
const utils_2 = require("../../utils");
class Session {
constructor(
helpers,
Expand Down Expand Up @@ -177,7 +178,7 @@ class Session {
this.helpers.config,
this.reqResInfo.transferMethod,
this.reqResInfo.req,
{}
utils_2.makeDefaultUserContextFromAPI(this.reqResInfo.req)
);
}
} else {
Expand Down Expand Up @@ -265,7 +266,7 @@ class Session {
const update = claim.removeFromPayloadByMerge_internal({}, userContext);
return this.mergeIntoAccessTokenPayload(update, userContext);
}
attachToRequestResponse(info) {
attachToRequestResponse(info, userContext) {
this.reqResInfo = info;
if (this.accessTokenUpdated) {
const { res, transferMethod } = info;
Expand All @@ -276,7 +277,7 @@ class Session {
this.helpers.config,
transferMethod,
info.req,
{}
userContext !== undefined ? userContext : utils_2.makeDefaultUserContextFromAPI(info.req)
);
if (this.refreshToken !== undefined) {
cookieAndHeaders_1.setToken(
Expand All @@ -287,7 +288,7 @@ class Session {
this.refreshToken.expiry,
transferMethod,
info.req,
{}
userContext !== undefined ? userContext : utils_2.makeDefaultUserContextFromAPI(info.req)
);
}
if (this.antiCsrfToken !== undefined) {
Expand Down
49 changes: 29 additions & 20 deletions lib/build/recipe/session/sessionRequestFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,19 @@ async function getSessionFromRequest({ req, res, config, recipeInterfaceImpl, op
// override how the session is attached to the response.
// In that scenario the transferMethod passed to attachToRequestResponse likely doesn't
// matter, still, we follow the general fallback logic
await session.attachToRequestResponse({
req,
res,
transferMethod:
requestTransferMethod !== undefined
? requestTransferMethod
: allowedTransferMethod !== "any"
? allowedTransferMethod
: "header",
});
await session.attachToRequestResponse(
{
req,
res,
transferMethod:
requestTransferMethod !== undefined
? requestTransferMethod
: allowedTransferMethod !== "any"
? allowedTransferMethod
: "header",
},
userContext
);
}
return session;
}
Expand Down Expand Up @@ -281,11 +284,14 @@ async function refreshSessionInRequest({ res, req, userContext, config, recipeIn
cookieAndHeaders_1.clearSession(config, res, transferMethod, req, userContext);
}
}
await session.attachToRequestResponse({
req,
res,
transferMethod: requestTransferMethod,
});
await session.attachToRequestResponse(
{
req,
res,
transferMethod: requestTransferMethod,
},
userContext
);
logger_1.logDebugMessage("refreshSession: Success!");
// This token isn't handled by getToken/setToken to limit the scope of this legacy/migration code
if (req.getCookieValue(LEGACY_ID_REFRESH_TOKEN_COOKIE_NAME) !== undefined) {
Expand Down Expand Up @@ -389,11 +395,14 @@ async function createNewSessionInRequest({
}
}
logger_1.logDebugMessage("createNewSession: Cleared old tokens");
await session.attachToRequestResponse({
req,
res,
transferMethod: outputTransferMethod,
});
await session.attachToRequestResponse(
{
req,
res,
transferMethod: outputTransferMethod,
},
userContext
);
logger_1.logDebugMessage("createNewSession: Attached new tokens to res");
return session;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/build/recipe/session/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export interface SessionContainerInterface {
setClaimValue<T>(claim: SessionClaim<T>, value: T, userContext?: any): Promise<void>;
getClaimValue<T>(claim: SessionClaim<T>, userContext?: any): Promise<T | undefined>;
removeClaim(claim: SessionClaim<any>, userContext?: any): Promise<void>;
attachToRequestResponse(reqResInfo: ReqResInfo): Promise<void> | void;
attachToRequestResponse(reqResInfo: ReqResInfo, userContext?: any): Promise<void> | void;
}
export declare type APIOptions = {
recipeImplementation: RecipeInterface;
Expand Down
9 changes: 5 additions & 4 deletions lib/ts/recipe/session/sessionClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { parseJWTWithoutSignatureVerification } from "./jwt";
import { logDebugMessage } from "../../logger";
import RecipeUserId from "../../recipeUserId";
import { protectedProps } from "./constants";
import { makeDefaultUserContextFromAPI } from "../../utils";

export default class Session implements SessionContainerInterface {
constructor(
Expand Down Expand Up @@ -175,7 +176,7 @@ export default class Session implements SessionContainerInterface {
this.helpers.config,
this.reqResInfo.transferMethod,
this.reqResInfo.req,
{}
makeDefaultUserContextFromAPI(this.reqResInfo.req)
);
}
} else {
Expand Down Expand Up @@ -274,7 +275,7 @@ export default class Session implements SessionContainerInterface {
return this.mergeIntoAccessTokenPayload(update, userContext);
}

attachToRequestResponse(info: ReqResInfo) {
attachToRequestResponse(info: ReqResInfo, userContext?: any) {
this.reqResInfo = info;

if (this.accessTokenUpdated) {
Expand All @@ -287,7 +288,7 @@ export default class Session implements SessionContainerInterface {
this.helpers.config,
transferMethod,
info.req,
{}
userContext !== undefined ? userContext : makeDefaultUserContextFromAPI(info.req)
);
if (this.refreshToken !== undefined) {
setToken(
Expand All @@ -298,7 +299,7 @@ export default class Session implements SessionContainerInterface {
this.refreshToken.expiry,
transferMethod,
info.req,
{}
userContext !== undefined ? userContext : makeDefaultUserContextFromAPI(info.req)
);
}
if (this.antiCsrfToken !== undefined) {
Expand Down
49 changes: 29 additions & 20 deletions lib/ts/recipe/session/sessionRequestFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,19 @@ export async function getSessionFromRequest({
// override how the session is attached to the response.
// In that scenario the transferMethod passed to attachToRequestResponse likely doesn't
// matter, still, we follow the general fallback logic
await session.attachToRequestResponse({
req,
res,
transferMethod:
requestTransferMethod !== undefined
? requestTransferMethod
: allowedTransferMethod !== "any"
? allowedTransferMethod
: "header",
});
await session.attachToRequestResponse(
{
req,
res,
transferMethod:
requestTransferMethod !== undefined
? requestTransferMethod
: allowedTransferMethod !== "any"
? allowedTransferMethod
: "header",
},
userContext
);
}
return session;
}
Expand Down Expand Up @@ -316,11 +319,14 @@ export async function refreshSessionInRequest({
clearSession(config, res, transferMethod, req, userContext);
}
}
await session.attachToRequestResponse({
req,
res,
transferMethod: requestTransferMethod,
});
await session.attachToRequestResponse(
{
req,
res,
transferMethod: requestTransferMethod,
},
userContext
);

logDebugMessage("refreshSession: Success!");

Expand Down Expand Up @@ -443,11 +449,14 @@ export async function createNewSessionInRequest({
}
logDebugMessage("createNewSession: Cleared old tokens");

await session.attachToRequestResponse({
req,
res,
transferMethod: outputTransferMethod,
});
await session.attachToRequestResponse(
{
req,
res,
transferMethod: outputTransferMethod,
},
userContext
);
logDebugMessage("createNewSession: Attached new tokens to res");

return session;
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/recipe/session/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export interface SessionContainerInterface {
setClaimValue<T>(claim: SessionClaim<T>, value: T, userContext?: any): Promise<void>;
getClaimValue<T>(claim: SessionClaim<T>, userContext?: any): Promise<T | undefined>;
removeClaim(claim: SessionClaim<any>, userContext?: any): Promise<void>;
attachToRequestResponse(reqResInfo: ReqResInfo): Promise<void> | void;
attachToRequestResponse(reqResInfo: ReqResInfo, userContext?: any): Promise<void> | void;
}

export type APIOptions = {
Expand Down

0 comments on commit e70667a

Please sign in to comment.