-
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 current user" error when using custom tokenProvider with Next.JS server components #470
Comments
Hi @HieronymusLex thanks for reaching out. The singleton method In the meantime, you may consider to directly use the lower level SSR adapter function I will mark this issue as a feature request for now. |
@HuiSF Thanks for the update and workaround! I think it would be worth calling some of these features out in the docs and adding some an example for Next.JS regarding the custom token provider section. Regarding using the
I'm assuming my error is getting the client from |
just following up here with the following code which is working:
|
I've been struggling with the exact same issue and exact same codebase 😄 export const {
handlers: { GET, POST },
auth,
signOut,
signIn,
} = NextAuth({
secret: config.auth.authSecret,
providers: [provider],
pages: {
signIn: '/api/auth/sign-in',
},
callbacks: {
async jwt({ token, account }) {
if (account) {
return produce(token, (draft) => {
draft.accessToken = account.access_token;
draft.idToken = account.id_token;
draft.refreshToken = account.refresh_token;
});
}
return token;
},
async session({ session, token }) {
return produce(session, (draft) => {
draft.accessToken = token.accessToken as string;
draft.idToken = token.idToken as string;
draft.refreshToken = token.refreshToken as string;
draft.user.id = token.sub!;
});
},
},
});
const nextAuthTokenProvider: TokenProvider = {
async getTokens() {
const session = await auth();
if (!session) {
return null;
}
const accessTokenString = session.accessToken;
const idTokenString = session.idToken;
return {
accessToken: decodeJWT(accessTokenString),
idToken: decodeJWT(idTokenString),
};
},
};
export async function configureAmplify() {
Amplify.configure(amplifyConfig, {
ssr: true,
Auth: {
tokenProvider: nextAuthTokenProvider,
},
});
} @HieronymusLex Your solution looks great for the SSR. Do you also happen to have something for the client side working? I mean I'm also trying to generate a client for client side with the same session details from |
ooo. I created a server action, and and called from client 😍 worked great for me. Thanks for the direction here. Your code snippet helped me get into the right direction, thanks again. |
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
GraphQL API
Amplify Categories
api
Environment information
Describe the bug
I have an AppSync API configured to use OIDC based auth. My project does not use the Auth module or Cognito
Per the Custom Token Providers section in the docs, I have configured a
tokenProvider
which obtains the tokens from the next-auth package (which in turn is using my 3rd auth provider). I now want to use these to authenticate requests to AppSync.The
tokenProvider
does not work with server rendered components which call the graphql API usingcookiesClient.graphql
where thecookiesClient
is obtained fromgenerateServerClientUsingCookies
- instead a No current user error is thrown (caused by this code).From debugging, I can see that when
InternalGraphQLAPIClass._headerBasedAuth
callsamplify.Auth.fetchAuthSession
it callsgetTokens
and theauthOptions
on the AuthClass singleton are null at this point. Important this issue only occurs in server rendered components. The token provider works as expected in client rendered components.I'm unsure but my current thought is that this is occurring because the library options are not being passed through correctly in the adapter-nextjs code - the code here indicates that only the resourcesConfig is being considered, not the library options. In my scenario
resourcesConfig.Auth
is undefined, but it even so on L41 it is always setting the token provider to the cognito user pool token provider anywaysExpected behavior
The custom token provider should be used when making graphql calls from both server and client components
Reproduction steps
Code Snippet
Log output
aws-exports.js
No response
Manual configuration
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response
The text was updated successfully, but these errors were encountered: