Skip to content

Commit

Permalink
use host instead of hostname and handle no origin
Browse files Browse the repository at this point in the history
  • Loading branch information
stepandel committed Sep 29, 2023
1 parent f93ee19 commit 2511e26
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions packages/backend/src/worker/rpgfApi/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,22 @@ export async function handleAuthRequest(

async function handleNonceRequest(request: Request) {
try {
const origin = request.headers.get("origin")!;
const url = new URL(origin);
const domain = url.hostname;

const nonce = await makeSIWENonce();

const origin = request.headers.get("origin");
if (!origin) {
return createResponse(
{ nonce },
200,
{
"Set-Cookie": `nonce=${nonce}; Path=/; HttpOnly; Secure; SameSite=None; max-age=300`, // TODO: set Secure for production
},
request
);
}

const url = new URL(origin);
const domain = url.host;
return createResponse(
{ nonce },
200,
Expand All @@ -66,10 +76,6 @@ async function handleVerifyRequest(
env: Env
): Promise<Response> {
try {
const origin = request.headers.get("origin")!;
const url = new URL(origin);
const domain = url.hostname;

const { message, signature } = (await request.json()) as any;

if (!message || !signature) {
Expand All @@ -90,6 +96,20 @@ async function handleVerifyRequest(
env.JWT_SECRET
);
if (success) {
const origin = request.headers.get("origin");
if (!origin) {
return createResponse(
{ success },
200,
{
"Set-Cookie": `access-token=${jwt}; Path=/; HttpOnly; Secure; SameSite=None; max-age=7200`, // 2 hours // TODO: set Secure for production
},
request
);
}

const url = new URL(origin);
const domain = url.host;
return createResponse(
{ success },
200,
Expand Down Expand Up @@ -174,9 +194,20 @@ async function handleSessionRequest(
// ----------------

async function handleSignOut(request: Request) {
const origin = request.headers.get("origin")!;
const origin = request.headers.get("origin");
if (!origin) {
return createResponse(
{ success: true },
200,
{
"Set-Cookie": `access-token=; Path=/; HttpOnly; Secure; SameSite=None; max-age=0`, // TODO: set Secure for production
},
request
);
}

const url = new URL(origin);
const domain = url.hostname;
const domain = url.host;
// Remove cookies
return createResponse(
{ success: true },
Expand Down

0 comments on commit 2511e26

Please sign in to comment.