-
Notifications
You must be signed in to change notification settings - Fork 79
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
[Bug] Amplify Gen2: AuthMode Type Mismatch ('userPool' vs 'userPools') Causes Authentication Failures #3083
Comments
Hey,👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂 |
Hey @ykethan, thanks for raising the issue! We'll investigate and see what's going on there, and get to back to you for next steps |
@georgechristman - Could you please share the contents of |
Hi @iartemiev, please see attached. Yes I saw the code and it's very strange. It does not function with userPool but does function with userPools.
|
After investigating, we've found that the For server components in Next.js, you should use Here's an example of how to set up your configuration files:
import type { ResourcesConfig } from 'aws-amplify';
import outputs from '@/amplify_outputs.json';
export const getAmplifyConfig = (): ResourcesConfig => {
return {
Auth: {
Cognito: {
userPoolId: outputs.auth.user_pool_id,
userPoolClientId: outputs.auth.user_pool_client_id,
identityPoolId: outputs.auth.identity_pool_id,
signUpVerificationMethod: 'link',
allowGuestAccess: true,
loginWith: {
oauth: {
domain: outputs.auth.oauth.domain,
scopes: outputs.auth.oauth.scopes,
redirectSignIn: outputs.auth.oauth.redirect_sign_in_uri,
redirectSignOut: outputs.auth.oauth.redirect_sign_out_uri,
responseType: outputs.auth.oauth.response_type as 'code',
},
},
},
},
};
};
'use client';
import type { Schema } from '@/amplify/data/resource';
import { Amplify } from 'aws-amplify';
import { generateClient } from 'aws-amplify/api';
import outputs from '@/amplify_outputs.json';
import { getAmplifyConfig } from './amplifyConfig';
Amplify.configure(outputs);
const currentConfig = Amplify.getConfig();
const customConfig = {
...currentConfig,
...getAmplifyConfig(),
};
Amplify.configure(customConfig, { ssr: true });
export default function AmplifyClientConfig() {
return null;
}
export const client = generateClient<Schema>();
import { Amplify } from 'aws-amplify';
import outputs from '@/amplify_outputs.json';
import { getAmplifyConfig } from './amplifyConfig';
Amplify.configure(outputs);
const currentConfig = Amplify.getConfig();
const customConfig = {
...currentConfig,
...getAmplifyConfig(),
};
Amplify.configure(customConfig);
export const updatedConfig = Amplify.getConfig();
import type { Schema } from '@/amplify/data/resource';
import { createServerRunner } from '@aws-amplify/adapter-nextjs';
import { generateServerClientUsingCookies } from '@aws-amplify/adapter-nextjs/data';
import { cookies } from 'next/headers';
import { updatedConfig } from './amplifyServerConfig';
export const { runWithAmplifyServerContext } = createServerRunner({
config: updatedConfig,
});
export const cookieBasedClient = generateServerClientUsingCookies<Schema>({
config: updatedConfig,
cookies,
}); With this setup, you can use the
import {cookieBasedClient} from '../lib/amplifyServerUtils'
export async function createOrder(order: Order) {
const order = await cookieBasedClient.models.Order.create(
orderData,
{ authMode: 'userPool' }
);
} This approach ensures that you're using the correct client for server-side operations while maintaining the user's authentication state through cookies. Feel free to adjust or expand on this response as needed for your GitHub issue. |
This issue is now closed. Comments on closed issues are hard for our team to see. |
Environment information
Describe the bug
AuthMode Type Definition Mismatch in Amplify Gen2
Description
There is a type definition mismatch between the runtime implementation and TypeScript types for
AuthMode
in Amplify Generation 2. The runtime expects'userPools'
(plural) as a valid auth mode, but the TypeScript type definition only includes'userPool'
(singular). This leads to TypeScript errors and potential runtime authentication issues.Current Behavior
AuthMode
includes'userPool'
(singular)'userPools'
(plural)'userPool'
(as suggested by types), results in authentication errorsError Details
When attempting to create an order with
userPool
(as suggested by types):Expected Behavior
The TypeScript type definition should match the runtime implementation by including
'userPools'
as a valid value forAuthMode
, preventing both type errors and runtime authentication issues.Reproduction Steps
userPools
as the auth mode:'userPool'
as suggested by types, observe the runtime error as well as the [docs]:(https://docs.amplify.aws/react/build-a-backend/data/customize-authz/per-user-per-owner-data-access/)Current Workaround
Currently, developers need to use type assertions to work around this issue:
Additional Context
The
AuthMode
type is currently defined as:This type definition enforces using 'userPool' (singular), but the runtime implementation requires using 'userPools' (plural). When we use the working runtime value 'userPools', TypeScript shows a compilation error because it's not included in the AuthMode type.
This leads to a situation where using the correct runtime value ('userPools') works at runtime but fails TypeScript compilation, forcing developers to use type assertions as a workaround.
Impact
This type mismatch:
Reproduction steps
Reproduction Steps
userPool
as the auth mode within an NextJS actionThe text was updated successfully, but these errors were encountered: