Skip to content

OAuth cookie not found error #335

@kaitpw

Description

@kaitpw

Hi, I'm quit inexperienced with oauth but I'm trying to get some working in my Fresh project. Unfortunately on both my dev and deployed sites I get this error on the servers console after I've gone through the whole oauth flow. Also if you have better suggestions for how to set up my plugin please tell. I think I may have ended up in this state from troubleshooting reasons but idk.

Error

...
Using redirect URI: http://localhost:8000/callback
An error occurred during route handling or page rendering.

  44 |         const auth = await getAuth();
  45 |         if (!auth) return new Response("Auth not configured", { status: 500 });
> 46 |         const { response } = await auth.handleCallback(req);
     |                                         ^
  47 |         return response;
  48 |       },
  49 |     },

Error: OAuth cookie not found
    at handleCallback (https://jsr.io/@deno/kv-oauth/0.11.0/lib/handle_callback.ts:67:43)
    at Object.handleCallback (https://jsr.io/@deno/kv-oauth/0.11.0/lib/create_helpers.ts:160:20)
    at Object.handler (file:///Users/kai-admin/A/PE/pm-upgrade/qr-repo/plugins/auth.ts:46:37)
    at eventLoopTick (ext:core/01_core.js:177:7)

My fresh Plugin

// plugins/auth.ts
import { createGoogleOAuthConfig, createHelpers, Helpers } from "@deno/kv-oauth";
import type { FreshContext, Plugin } from "$fresh/server.ts";
import { State } from "../types/app.ts";

async function getAuth() {
	try {
		const isProd = Deno.env.get("ENV") === "dev";
		const domain = isProd ? "http://localhost:8000" : "https://xxxx.deno.dev";
		const redirectUri = `${domain}/callback`;
		const oauth2Client = createGoogleOAuthConfig({
			redirectUri,
			scope: "https://www.googleapis.com/auth/userinfo.profile",
		});

		console.log(`Using redirect URI: ${redirectUri}`);

		const authHelpers = createHelpers(oauth2Client);
		return authHelpers;
	} catch (error) {
		throw error;
	}
}

export default {
	name: "auth",
	routes: [
		{
			path: "/callback",
			handler: async (req: Request) => {
				const auth = await getAuth();
				const { response } = await auth.handleCallback(req);
				return response;
			},
		},
		{
			path: "/api/auth",
			handler: async (req: Request) => {
				const auth = await getAuth();
				const { action } = await req.json();
				if (action === "signin") {
					const response = await auth.signIn(req);
					const url = response.headers.get("Location");
					return new Response(JSON.stringify({ url }), {
						headers: { "Content-Type": "application/json" },
					});
				}
				if (action === "signout") return await auth.signOut(req);
				return new Response("Invalid action", { status: 400 });
			},
		},
	],
	async handler(req: Request, ctx: FreshContext<State>) {
		const auth = await getAuth();

		if (!ctx.state.auth) {
			ctx.state.auth = { sessionId: undefined };
		}

		if (auth) {
			ctx.state.auth.sessionId = await auth.getSessionId(req);
		}

		return await ctx.next();
	},
} as Plugin;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions