-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No federated jwt in function handler #420
Comments
Hey,👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂 |
Hi @Z-Shang 👋 thanks for raising this issue. It looks like you might just have to update your amplify packages: npm install @aws-amplify/backend@latest @aws-amplify/backend-cli@latest and to make sure the data-schema package is also up to date: npm update @aws-amplify/data-schema Lastly, please check out this page in our docs on accessing the GraphQL API from a Lambda function: We recently released support for better DX when using |
Hi @chrisbonifacio :D Thanks for your response!
And weirdly my next.js middleware stopped working :( import { NextResponse, NextRequest } from "next/server";
import { fetchAuthSession } from "@aws-amplify/auth/server";
import { runWithAmplifyServerContext } from "@/utils/amplify-utils";
export async function middleware(request: NextRequest) {
const response = NextResponse.next();
const authenticated = await runWithAmplifyServerContext({
nextServerContext: { request, response },
operation: async (contextSpec) => {
console.log("ContextSpec:", contextSpec);
try {
const session = await fetchAuthSession(contextSpec, {});
console.log("Session:", session);
return session.tokens !== undefined;
} catch (error) {
console.error("Auth error:", error);
return false;
}
},
});
if (!authenticated) {
console.log("Redirecting to login");
return NextResponse.redirect(new URL("/login", request.url));
}
return response;
} My client side code looks like: const getUserAttrs = async () => {
const res = await fetch("/api/user", {
method: "GET",
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-cache",
},
cache: "no-store",
credentials: "include",
});
const data = await res.json();
return data.user;
}; I did some logging that shows the authenticator has produced a valid I wonder if there was any breaking change and how should I migrate my authentication flow to the latest version? |
Hi @Z-Shang, I'm having a little bit of trouble understanding exactly what the middleware logic is supposed to do in the example you shared. It looks like it's simply calling Can you confirm that the console log of the session is showing anything? Is the "No federated jwt" error coming from the middleware? Are there other middleware examples of API calls being unauthorized? Also, it looks like your Lastly, this is our docs example on how to authenticate in middleware, which seems very close to what you're doing. The only noteworthy difference I can tell is that you are passing an empty object to |
Hey @Z-Shang 👋 the can you inspect the request cookie header you are sending to your API /api/user endpoint? it should have access token, id token and refresh token. Lastly, was that |
Environment information
Describe the bug
I have a simple data schema where a data model
Room
that can have a list of active users, the authorization forRoom
isallow.authenticated()
I have made a mutation for
Room
namelyjoinRoom
using function handlerOn the client side, I'm hosting a Next.JS app with the
Authenticator
from@aws-amplify/ui-react
for authenticationWhen calling the mutation from the client side with the generated client, I'm getting the following error:
Reproduction steps
My data
resource.ts
is like:and my
joinRoom.ts
looks like:On the client side, the mutation is invoked like:
The text was updated successfully, but these errors were encountered: