Skip to content

Commit 3c24971

Browse files
authored
fix(jargons-editor): login issues by changing the cookie name using appropriate naming convention (#110)
### Description Fixed login issues by renaming `jargons.dev:token` to `jargondevsToken` ensuring the access token gets the appropriate cookie value enabling the authentication to work seamlessly. ### Related Issue Fixes #109
1 parent ef921b9 commit 3c24971

4 files changed

Lines changed: 156 additions & 103 deletions

File tree

src/lib/actions/do-auth.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,46 @@
11
import app from "../octokit/app.js";
22
import { decrypt, encrypt } from "../utils/crypto.js";
33
import { GET as getAuthorization } from "../../pages/api/github/oauth/authorize.js";
4-
import { isObjectEmpty as isStateEmpty, resolveCookieExpiryDate } from "../utils/index.js";
4+
import {
5+
isObjectEmpty as isStateEmpty,
6+
resolveCookieExpiryDate,
7+
} from "../utils/index.js";
58

69
/**
710
* Authentication action with GitHub OAuth
8-
* @param {import("astro").AstroGlobal} astroGlobal
11+
* @param {import("astro").AstroGlobal} astroGlobal
912
*/
1013
export default async function doAuth(astroGlobal) {
11-
const { url: { searchParams }, cookies } = astroGlobal;
14+
const {
15+
url: { searchParams },
16+
cookies,
17+
} = astroGlobal;
1218
const code = searchParams.get("code");
13-
const accessToken = cookies.get("jargons.dev:token", {
14-
decode: value => decrypt(value)
19+
const accessToken = cookies.get("jargonsdevToken", {
20+
decode: (value) => decrypt(value),
1521
});
1622

1723
/**
1824
* Generate OAuth Url to start authorization flow
1925
* @todo improvement: store `state` in cookie for later retrieval/comparison with auth `state` in `github/oauth/callback`
20-
* @param {{ path: string }} state
26+
* @param {{ path: string }} state
2127
*/
2228
function getAuthUrl(state) {
2329
let parsedState = "";
2430

25-
if (!isStateEmpty(state)){
31+
if (!isStateEmpty(state)) {
2632
if (state.path) parsedState += `path:${state.path}`;
27-
const otherStates = String(Object.keys(state)
28-
.filter(key => key !== "path")
29-
.map(key => key + ":" + state[key]).join("|"));
33+
const otherStates = String(
34+
Object.keys(state)
35+
.filter((key) => key !== "path")
36+
.map((key) => key + ":" + state[key])
37+
.join("|")
38+
);
3039
if (otherStates.length > 0) parsedState += `|${otherStates}`;
3140
}
3241

3342
const { url } = app.oauth.getWebFlowAuthorizationUrl({
34-
state: parsedState
43+
state: parsedState,
3544
});
3645

3746
return url;
@@ -41,29 +50,29 @@ export default async function doAuth(astroGlobal) {
4150
if (code) {
4251
const response = await getAuthorization(astroGlobal);
4352
const auth = await response.json();
44-
53+
4554
if (auth.accessToken) {
46-
cookies.set("jargons.dev:token", auth.accessToken, {
55+
cookies.set("jargonsdevToken", auth.accessToken, {
4756
expires: resolveCookieExpiryDate(28800),
4857
path: "/",
49-
encode: value => encrypt(value)
58+
encode: (value) => encrypt(value),
5059
});
5160
}
5261
}
53-
62+
5463
const userOctokit = app.getUserOctokit({ token: accessToken.value });
5564
const { data } = await userOctokit.request("GET /user");
56-
65+
5766
return {
5867
getAuthUrl,
5968
isAuthed: true,
60-
authedData: data
61-
}
69+
authedData: data,
70+
};
6271
} catch (error) {
6372
return {
6473
getAuthUrl,
6574
isAuthed: false,
66-
authedData: null
67-
}
75+
authedData: null,
76+
};
6877
}
69-
}
78+
}

src/lib/actions/do-contribution-stats.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ import { PROJECT_REPO_DETAILS } from "../../../constants.js";
55

66
/**
77
* Get some jargons contribution stats for current user on the Jargons Editor
8-
* @param {import("astro").AstroGlobal} astroGlobal
8+
* @param {import("astro").AstroGlobal} astroGlobal
99
*/
1010
export default async function doContributionStats(astroGlobal) {
1111
const { cookies } = astroGlobal;
12-
const { repoFullname, repoMainBranchRef } = PROJECT_REPO_DETAILS;
12+
const { repoFullname, repoMainBranchRef } = PROJECT_REPO_DETAILS;
1313

14-
const accessToken = cookies.get("jargons.dev:token", { decode: value => decrypt(value) });
14+
const accessToken = cookies.get("jargonsdevToken", {
15+
decode: (value) => decrypt(value),
16+
});
1517
const userOctokit = app.getUserOctokit({ token: accessToken.value });
1618

1719
/**
18-
* @todo Implement narrowed search to project's main branch
20+
* @todo Implement narrowed search to project's main branch
1921
*/
2022
const baseQuery = `repo:${repoFullname} is:pull-request type:pr author:@me`;
2123
const baseStatsUrlQuery = `is:pr author:@me`;
@@ -25,27 +27,39 @@ export default async function doContributionStats(astroGlobal) {
2527
* [potential bottleneck]: no way to know whether a PR is "merged" considering that "closed" doesn't mean merged
2628
*/
2729
const { data: newType } = await userOctokit.request("GET /search/issues", {
28-
q: `${baseQuery} label:":book: new word" is:merged is:closed`
30+
q: `${baseQuery} label:":book: new word" is:merged is:closed`,
2931
});
3032
const { data: editType } = await userOctokit.request("GET /search/issues", {
31-
q: `${baseQuery} label:":book: edit word" is:merged is:closed`
32-
});
33-
const { data: pendingType } = await userOctokit.request("GET /search/issues", {
34-
q: `${baseQuery} label:":book: edit word",":book: new word" is:unmerged is:open`
33+
q: `${baseQuery} label:":book: edit word" is:merged is:closed`,
3534
});
35+
const { data: pendingType } = await userOctokit.request(
36+
"GET /search/issues",
37+
{
38+
q: `${baseQuery} label:":book: edit word",":book: new word" is:unmerged is:open`,
39+
}
40+
);
3641

3742
return {
3843
newWords: {
3944
count: newType.total_count,
40-
url: buildStatsUrl(repoFullname, `${baseStatsUrlQuery} is:merged is:closed label:":book: new word"`)
45+
url: buildStatsUrl(
46+
repoFullname,
47+
`${baseStatsUrlQuery} is:merged is:closed label:":book: new word"`
48+
),
4149
},
4250
editedWords: {
4351
count: editType.total_count,
44-
url: buildStatsUrl(repoFullname, `${baseStatsUrlQuery} is:merged is:closed label:":book: edit word"`)
52+
url: buildStatsUrl(
53+
repoFullname,
54+
`${baseStatsUrlQuery} is:merged is:closed label:":book: edit word"`
55+
),
4556
},
4657
pendingWords: {
4758
count: pendingType.total_count,
48-
url: buildStatsUrl(repoFullname, `${baseStatsUrlQuery} is:unmerged is:open`)
49-
}
50-
}
51-
}
59+
url: buildStatsUrl(
60+
repoFullname,
61+
`${baseStatsUrlQuery} is:unmerged is:open`
62+
),
63+
},
64+
};
65+
}

src/lib/actions/do-edit-word.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@ import { PROJECT_REPO_DETAILS } from "../../../constants.js";
44

55
/**
66
* Edit Word action - meant to be executed on `editor/edit/[word]` route
7-
* @param {import("astro").AstroGlobal} astroGlobal
7+
* @param {import("astro").AstroGlobal} astroGlobal
88
*/
99
export default async function doEditWord(astroGlobal) {
10-
const { cookies, params: { word } } = astroGlobal;
10+
const {
11+
cookies,
12+
params: { word },
13+
} = astroGlobal;
1114

12-
const accessToken = cookies.get("jargons.dev:token", { decode: value => decrypt(value) });
15+
const accessToken = cookies.get("jargonsdevToken", {
16+
decode: (value) => decrypt(value),
17+
});
1318
const userOctokit = app.getUserOctokit({ token: accessToken.value });
1419

15-
const response = await getExistingWord(userOctokit, PROJECT_REPO_DETAILS, word);
20+
const response = await getExistingWord(
21+
userOctokit,
22+
PROJECT_REPO_DETAILS,
23+
word
24+
);
1625

1726
return response;
18-
}
27+
}

0 commit comments

Comments
 (0)