Skip to content

Commit

Permalink
Merge pull request #14 from curveball/use-ctx-session
Browse files Browse the repository at this point in the history
Use ctx.session instead of ctx.state.session
  • Loading branch information
evert authored Feb 2, 2021
2 parents 089028b + 2cdff35 commit 73a4255
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
10 changes: 6 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"homepage": "https://github.com/curveball/browser-to-bearer#readme",
"devDependencies": {
"@curveball/core": "^0.16.1",
"@curveball/session": "^0.6.0",
"@curveball/session": "^0.6.1",
"@types/chai": "^4.2.14",
"@types/mocha": "^8.2.0",
"@types/node": "^10.17.51",
Expand All @@ -60,7 +60,7 @@
},
"peerDependencies": {
"@curveball/core": ">=0.9.0 <1.0.0",
"@curveball/session": "^0.6.0"
"@curveball/session": "^0.6.1"
},
"dependencies": {
"node-fetch": "^2.6.1"
Expand Down
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async function handleOAuth2Code(ctx: Context, options: OAuth2Options) {
const rBody = await response.json();

ctx.response.status = 303;
ctx.state.session.oauth2tokens = {
ctx.session.oauth2tokens = {
accessToken: rBody.access_token,
expires: Date.now() + (rBody.expires_in * 1000),
refreshToken: rBody.refresh_token,
Expand All @@ -116,7 +116,6 @@ async function handleOAuth2Code(ctx: Context, options: OAuth2Options) {
throw new Error('Sandbox violation');
}
ctx.response.headers.set('Location', state);

}
type OAuth2Token = {
accessToken: string,
Expand All @@ -126,15 +125,15 @@ type OAuth2Token = {

async function getOAuth2Tokens(ctx: Context, options: OAuth2Options): Promise<OAuth2Token | null> {

if (!('session' in ctx.state)) {
if (!('session' in ctx)) {
throw new Error('A session middleware must run before the browser-to-bearer middleware');
}

if (!ctx.state.session.oauth2tokens) {
if (!ctx.session.oauth2tokens) {
return null;
}

const token: OAuth2Token = ctx.state.session.oauth2tokens;
const token: OAuth2Token = ctx.session.oauth2tokens;

if (token.expires > Date.now()) {
return token;
Expand All @@ -160,13 +159,13 @@ async function getOAuth2Tokens(ctx: Context, options: OAuth2Options): Promise<OA
}
const rBody = await response.json();

ctx.state.session.oauth2tokens = {
ctx.session.oauth2tokens = {
accessToken: rBody.access_token,
expires: Date.now() + (rBody.expires_in * 1000),
refreshToken: rBody.refresh_token,
};

return ctx.state.session.oauth2tokens;
return ctx.session.oauth2tokens;

}

Expand Down

0 comments on commit 73a4255

Please sign in to comment.