Skip to content

Commit fdccfad

Browse files
committed
feat: enable upsert for user authentication and database operations, and handle upserted IDs in authentication flow.
1 parent ab46b7c commit fdccfad

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

src/backend/auth/github.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ async function authenticateAndCreateJWT(
3030
const oauthUrl = provider === "github"
3131
? "https://github.com/login/oauth/access_token"
3232
: provider === "gitlab"
33-
? "https://gitlab.com/oauth/token"
34-
: null;
33+
? "https://gitlab.com/oauth/token"
34+
: null;
3535

3636
if (oauthUrl === null) {
3737
ctx.response.body = "Unsupported provider";
@@ -47,14 +47,14 @@ async function authenticateAndCreateJWT(
4747
code,
4848
}).toString()
4949
: provider === "gitlab"
50-
? new URLSearchParams({
51-
client_id: id,
52-
client_secret: secret,
53-
code,
54-
grant_type: "authorization_code",
55-
redirect_uri: `${frontend}/login`,
56-
}).toString()
57-
: "";
50+
? new URLSearchParams({
51+
client_id: id,
52+
client_secret: secret,
53+
code,
54+
grant_type: "authorization_code",
55+
redirect_uri: `${frontend}/login`,
56+
}).toString()
57+
: "";
5858

5959
const resp = await fetch(rootUrl.toString(), {
6060
method: "POST",
@@ -69,7 +69,7 @@ async function authenticateAndCreateJWT(
6969

7070
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
7171

72-
if (status.matchedCount == 1) {
72+
if (status.matchedCount == 1 || status.upsertedId !== undefined) {
7373
const id_jwt = await createJWT(provider, userId);
7474
Sentry.captureMessage("User " + userId + " logged in", "info");
7575
ctx.response.body = id_jwt;
@@ -97,7 +97,7 @@ async function handleJwtAuthentication(ctx: Context) {
9797
const provider = document.provider;
9898
const user = await checkJWT(provider, jwt_token);
9999
const apiKey = generateApiKey(user)
100-
ctx.response.body = {user , apiKey};
100+
ctx.response.body = { user, apiKey };
101101
}
102102

103103
export { githubAuth, gitlabAuth, handleJwtAuthentication };

src/backend/db.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ async function checkUser(accessToken: string, provider: string) {
4141
"authToken": accessToken,
4242
},
4343
},
44+
"upsert": true,
4445
};
4546

4647
options.body = JSON.stringify(query);

0 commit comments

Comments
 (0)