Skip to content

Commit

Permalink
Refactor the logout routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Apr 8, 2024
1 parent 53bf6cd commit c7e89e3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 71 deletions.
77 changes: 77 additions & 0 deletions src/routes/auth/logout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import e from "@edgedb";
import { DATABASE_DELETE_FAILED, UNAUTHORIZED } from "constants/responses";
import { Elysia } from "elysia";
import { HttpStatusCode } from "elysia-http-status-code";
import { client } from "index";
import { auth } from "plugins/auth";
import { promiseResult } from "utils/errors";
import { responseBuilder } from "utils/response";

export const logoutRouter = new Elysia()
.use(HttpStatusCode())
.use(auth)
.delete("/all", async ({ auth, set, httpStatus }) => {
if (!auth.isAuthorized) {
set.status = httpStatus.HTTP_401_UNAUTHORIZED;
return UNAUTHORIZED;
}
if (auth.createdBy !== "login") {
set.status = httpStatus.HTTP_403_FORBIDDEN;
return responseBuilder("error", {
error:
"Access token must be generated using log in and not a refresh token",
});
}

const delQuery = e.count(
e.delete(e.RefreshToken, (t) => ({
filter: e.op(t["<tokens[is User]"].username, "=", auth.username),
})),
);

const result = await promiseResult(() => delQuery.run(client));

if (result.isError) {
set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR;
return DATABASE_DELETE_FAILED;
}

return responseBuilder("success", {
message: "Logged out from all sessions",
data: {
sessionCount: result.data,
},
});
})
.delete("/:refreshToken", async ({ params, auth, set, httpStatus }) => {
if (!auth.isAuthorized) {
set.status = httpStatus.HTTP_401_UNAUTHORIZED;
return UNAUTHORIZED;
}

const delQuery = e.delete(e.RefreshToken, (t) => ({
filter_single: e.op(
e.op(t["<tokens[is User]"].username, "=", auth.username),
"and",
e.op(t.token, "=", params.refreshToken),
),
}));
const result = await promiseResult(() => delQuery.run(client));

if (result.isError) {
set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR;
return DATABASE_DELETE_FAILED;
}

if (!result.data) {
set.status = httpStatus.HTTP_404_NOT_FOUND;
return responseBuilder("error", {
error: "Could not find that refresh token",
});
}

return responseBuilder("success", {
message: "Deleted one refresh token successfully",
data: null,
});
});
73 changes: 3 additions & 70 deletions src/routes/auth/refreshToken.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import e from "@edgedb";
import {
DATABASE_DELETE_FAILED,
DATABASE_WRITE_FAILED,
UNAUTHORIZED,
} from "constants/responses";
import { DATABASE_WRITE_FAILED } from "constants/responses";
import { Elysia, t } from "elysia";
import { HttpStatusCode } from "elysia-http-status-code";
import { client } from "index";
Expand All @@ -14,10 +10,12 @@ import { promiseResult } from "utils/errors";
import { randomNumber } from "utils/random";
import { responseBuilder } from "utils/response";
import { wait } from "utils/time";
import { logoutRouter } from "./logout";

export const refreshTokenRouter = new Elysia({ prefix: "/refresh-token" })
.use(HttpStatusCode())
.use(auth)
.use(logoutRouter)
.post(
"/password",
async ({ body, set, httpStatus }) => {
Expand Down Expand Up @@ -99,68 +97,3 @@ export const refreshTokenRouter = new Elysia({ prefix: "/refresh-token" })
detail: { tags: ["Auth"] },
},
)
.delete("/all", async ({ auth, set, httpStatus }) => {
if (!auth.isAuthorized) {
set.status = httpStatus.HTTP_401_UNAUTHORIZED;
return UNAUTHORIZED;
}
if (auth.createdBy !== "login") {
set.status = httpStatus.HTTP_403_FORBIDDEN;
return responseBuilder("error", {
error:
"Access token must be generated using log in and not a refresh token",
});
}

const delQuery = e.count(
e.delete(e.RefreshToken, (t) => ({
filter: e.op(t["<tokens[is User]"].username, "=", auth.username),
})),
);

const result = await promiseResult(() => delQuery.run(client));

if (result.isError) {
set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR;
return DATABASE_DELETE_FAILED;
}

return responseBuilder("success", {
message: "Logged out from all sessions",
data: {
sessionCount: result.data,
},
});
})
.delete("/:refreshToken", async ({ params, auth, set, httpStatus }) => {
if (!auth.isAuthorized) {
set.status = httpStatus.HTTP_401_UNAUTHORIZED;
return UNAUTHORIZED;
}

const delQuery = e.delete(e.RefreshToken, (t) => ({
filter_single: e.op(
e.op(t["<tokens[is User]"].username, "=", auth.username),
"and",
e.op(t.token, "=", params.refreshToken),
),
}));
const result = await promiseResult(() => delQuery.run(client));

if (result.isError) {
set.status = httpStatus.HTTP_500_INTERNAL_SERVER_ERROR;
return DATABASE_DELETE_FAILED;
}

if (!result.data) {
set.status = httpStatus.HTTP_404_NOT_FOUND;
return responseBuilder("error", {
error: "Could not find that refresh token",
});
}

return responseBuilder("success", {
message: "Deleted one refresh token successfully",
data: null,
});
});
2 changes: 1 addition & 1 deletion src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const promiseResult = async <T>(callback: () => Promise<T>) => {
}) as const,
)
.catch(
(e) =>
(e: Error) =>
({
status: "error",
error: e,
Expand Down

0 comments on commit c7e89e3

Please sign in to comment.