I have a server action that I’m using to call a protected route that I’ve created, which looks like so:
'use server';
import { getSession } from '@auth0/nextjs-auth0';
export const uploadImage = async (base64ImageString) => {
const { accessToken } = await getSession();
console.log(accessToken);
try {
const response = await fetch(
process.env.NEXT_JS_PUBLIC_LOCALHOST + '/api/someendpoint,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: {
image: base64ImageString,
},
}
);
const result = await response.json();
console.log(result);
return response.data;
} catch (error) {
console.log(error);
return {};
}
};
and I am calling a protected route, which looks like so:
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
import { NextResponse } from 'next/server';
export const POST = withApiAuthRequired(async (req, res) => {
try {
const session = await getSession(req);
console.log('POST REQUEST SESSION', session);
return res.json({
msg: 'looking good',
});
} catch (error) {
console.error('Error uploading file:', error);
return NextResponse.json(
{ message: 'File upload failed' },
{ status: 500 }
);
}
});
When I call uploadImage
I do see the accessToken
get logged to the console, but my protected route doesn’t seem to allow the request. I keep seeing the below log from uploadImage
within the console:
{
error: 'not_authenticated',
description: 'The user does not have an active session or is not authenticated'
}
Does anyone have any tips for why this is happening?