Skip to content

Commit 62dc1c0

Browse files
committed
refactor(auth): consolidate remaining direct Amplify calls to use auth helpers
Replace direct runWithAmplifyServerContext/fetchAuthSession calls in cognito-token route with tryGetAuthSession(), and replace manual getAuthSession + prisma.user.findUnique in authActionClient with getSessionWithUser(). This eliminates duplicate auth code paths that were missed in the initial refactor, ensuring all authentication flows go through the centralized auth.ts helpers. The change also benefits from cache() memoization in getSessionWithUser() to avoid redundant DB lookups within the same request.
1 parent 7237951 commit 62dc1c0

File tree

3 files changed

+7
-25
lines changed

3 files changed

+7
-25
lines changed

webapp/src/app/api/cognito-token/route.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import { NextResponse } from 'next/server';
2-
import { cookies } from 'next/headers';
3-
import { fetchAuthSession } from 'aws-amplify/auth/server';
4-
import { runWithAmplifyServerContext } from '@/lib/amplifyServerUtils';
2+
import { tryGetAuthSession } from '@/lib/auth';
53

64
export async function GET() {
75
try {
8-
const session = await runWithAmplifyServerContext({
9-
nextServerContext: { cookies },
10-
operation: (contextSpec) => fetchAuthSession(contextSpec),
11-
});
12-
13-
if (session.tokens?.accessToken == null) {
6+
const session = await tryGetAuthSession();
7+
if (!session) {
148
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
159
}
1610

1711
return NextResponse.json({
18-
accessToken: session.tokens.accessToken.toString(),
12+
accessToken: session.accessToken,
1913
});
2014
} catch (error) {
2115
console.error('Error fetching Cognito token:', error);

webapp/src/lib/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const getAuthSession = cache(async () => {
3030

3131
/**
3232
* Try to get the authenticated session, returning null on failure.
33-
* Use in API Routes where you need to distinguish 401 from 500.
33+
* Use in API Routes to avoid try/catch boilerplate for auth checks.
3434
*/
3535
export async function tryGetAuthSession() {
3636
try {

webapp/src/lib/safe-action.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { getAuthSession } from '@/lib/auth';
2-
import { prisma } from '@/lib/prisma';
1+
import { getSessionWithUser } from '@/lib/auth';
32
import { createSafeActionClient, DEFAULT_SERVER_ERROR_MESSAGE } from 'next-safe-action';
43

54
export class MyCustomError extends Error {
@@ -26,17 +25,6 @@ const actionClient = createSafeActionClient({
2625
});
2726

2827
export const authActionClient = actionClient.use(async ({ next }) => {
29-
const { userId } = await getAuthSession();
30-
31-
const user = await prisma.user.findUnique({
32-
where: {
33-
id: userId,
34-
},
35-
});
36-
37-
if (user == null) {
38-
throw new Error('user not found');
39-
}
40-
28+
const { user } = await getSessionWithUser();
4129
return next({ ctx: { userId: user.id } });
4230
});

0 commit comments

Comments
 (0)