Skip to content

Commit

Permalink
setup: session handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Ketcap committed Aug 13, 2023
1 parent a30cef5 commit 2315b27
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
7 changes: 3 additions & 4 deletions apps/gql/app/graphql/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { readFileSync } from "node:fs";
import { join } from "node:path";
import { createSchema, createYoga } from "graphql-yoga";

import { getServerSession } from "@kampus/next-auth";

import { getSession } from "~/features/auth";
import { createActions } from "~/actions";
import { createClients } from "~/clients";
import { createLoaders } from "~/loaders";
Expand All @@ -17,10 +16,10 @@ const { handleRequest } = createYoga<KampusGQLContext>({
schema: createSchema<KampusGQLContext>({ typeDefs, resolvers }),
logging: "debug",
graphiql: true,
context: async () => {
context: async ({ request }) => {
const loaders = createLoaders(clients);
const actions = createActions(clients);
const session = await getServerSession();
const session = await getSession(request);

return {
loaders,
Expand Down
20 changes: 20 additions & 0 deletions apps/gql/features/auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type Session } from "@kampus/next-auth";

import { env } from "../../env";

export const getSession = async (request: Request): Promise<Session | null> => {
const headers = new Headers(request.headers);
try {
const cookie = headers.get("cookie");
const session = await fetch(`${env.NEXTAUTH_URL}/session`, {
method: "GET",
headers: {
cookie: cookie || "",
},
}).then((res) => res.json());
return session satisfies Session;
} catch (e) {
// handle error for session request fails
return null;
}
};

0 comments on commit 2315b27

Please sign in to comment.