Skip to content

Commit

Permalink
make auth config & session validation more error resistant
Browse files Browse the repository at this point in the history
  • Loading branch information
dallen4 committed Oct 20, 2023
1 parent 0fc4a46 commit 35f5009
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
11 changes: 11 additions & 0 deletions web/api/auth0.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { getSession } from '@auth0/nextjs-auth0';
import { ManagementClient } from 'auth0';
import { NextApiRequest, NextApiResponse } from 'next';
import { UserMetadata } from 'types/users';

const { host } = new URL(process.env.AUTH0_ISSUER_BASE_URL!);
Expand Down Expand Up @@ -36,3 +38,12 @@ export const updateUser = async (id: string, data: UserMetadata) => {
metadata: updatedUser.user_metadata,
};
};

export const getAuthSession = async (
req: NextApiRequest,
res: NextApiResponse,
) =>
getSession(req, res).catch((err) => {
console.error(err);
return null;
});
5 changes: 4 additions & 1 deletion web/pages/api/auth/[auth0].ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { handleAuth } from '@auth0/nextjs-auth0';
import { handleAuth, initAuth0 } from '@auth0/nextjs-auth0';

if (!process.env.AUTH0_BASE_URL)
process.env.AUTH0_BASE_URL = process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL!;

export default handleAuth();
4 changes: 2 additions & 2 deletions web/pages/api/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { createDrop } from 'api/drops';
import { DISABLE_CAPTCHA_COOKIE } from '@config/cookies';
import { cors } from 'api/middleware/cors';
import { runMiddleware } from 'api/middleware';
import { getSession } from '@auth0/nextjs-auth0';
import { getAuthSession } from 'api/auth0';

export default async function drop(req: NextApiRequest, res: NextApiResponse) {
await runMiddleware(req, res, cors);

const session = await getSession(req, res);
const session = await getAuthSession(req, res);

if (!['POST', 'GET', 'DELETE'].includes(req.method!)) {
res.setHeader('Allow', 'POST,GET,DELETE');
Expand Down
5 changes: 2 additions & 3 deletions web/pages/api/me.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { cors } from 'api/middleware/cors';
import { runMiddleware } from 'api/middleware';
import { getUserById } from 'api/auth0';
import { getSession } from '@auth0/nextjs-auth0';
import { getAuthSession, getUserById } from 'api/auth0';

export default async function me(req: NextApiRequest, res: NextApiResponse) {
await runMiddleware(req, res, cors);
Expand All @@ -13,7 +12,7 @@ export default async function me(req: NextApiRequest, res: NextApiResponse) {
return;
}

const session = await getSession(req, res);
const session = await getAuthSession(req, res);

if (session) {
const user = await getUserById(session!.user.sub);
Expand Down

0 comments on commit 35f5009

Please sign in to comment.