Skip to content

Commit

Permalink
Merge pull request #50 from UNIZAR-30226-2024-03/auth
Browse files Browse the repository at this point in the history
Remove jose dependency and commented code
  • Loading branch information
Arejula11 authored Apr 4, 2024
2 parents 5340632 + 8c60fa7 commit 6f11237
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 63 deletions.
9 changes: 0 additions & 9 deletions playbeat/package-lock.json

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

1 change: 0 additions & 1 deletion playbeat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"axios": "^1.6.8",
"clsx": "^2.1.0",
"framer-motion": "^11.0.5",
"jose": "^5.2.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"svelte": "^4.2.12",
Expand Down
56 changes: 3 additions & 53 deletions playbeat/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
import { errors, jwtVerify } from "jose";

import { defineMiddleware } from "astro/middleware";
import { TOKEN, PUBLIC_ROUTES } from "./constants";

// The JWT secret
const secret = new TextEncoder().encode(import.meta.env.JWT_SECRET_KEY);
// const secret = new TextEncoder().encode(import.meta.env.JWT_SECRET_KEY);

/**
* Verify if the client token is valid.
*/
const verifyAuth = async (token?: string) => {
if (!token) {
return {
status: "unauthorized",
msg: "Please pass a request token",
} as const;
}

try {
const jwtVerifyResult = await jwtVerify(token, secret);

return {
status: "authorized",
payload: jwtVerifyResult.payload,
msg: "successfully verified auth token",
} as const;
} catch (err) {
if (err instanceof errors.JOSEError) {
return { status: "error", msg: err.message } as const;
}
return { status: "error", msg: "could not validate auth token" } as const;
}
};

export const onRequest = defineMiddleware(async (context, next) => {
// Ignore auth validation for public routes
Expand All @@ -49,30 +24,5 @@ export const onRequest = defineMiddleware(async (context, next) => {
}else {
return Response.redirect(new URL("/", context.url));
}
// const validationResult = await verifyAuth(token);



// Handle the validation result
// switch (validationResult.status) {
// case "authorized":
// // Respond as usual if the user is authorised
// return next();

// case "error":
// case "unauthorized":
// // If an API endpoint, return a JSON response
// if (context.url.pathname.startsWith("/api/")) {
// return new Response(JSON.stringify({ message: validationResult.msg }), {
// status: 401,
// });
// }
// // Otherwise, this is a standard page. Redirect to the root page for the user to login
// else {
// return Response.redirect(new URL("/", context.url));
// }

// default:
// return Response.redirect(new URL("/", context.url));
// }

});

0 comments on commit 6f11237

Please sign in to comment.