Closed Deanfei closed 2 weeks ago
Hello @Deanfei. How does your oauth config look like ? Also make sure you are importing the signInWithRedirect
API as follows,
import { signInWithRedirect } from 'aws-amplify/auth';
Can you also listen for the following hub events and see if you get any logs?
Hub.listen("auth", ({ payload }) => {
switch (payload.event) {
case "signInWithRedirect":
console.log('signInWithRedirect event');
break;
case "signInWithRedirect_failure":
console.log('signInWithRedirect_failure event', payload.data);
break;
}
});
Finally, if nothing from above worked. Can you also downgrade the version of amplify to 6.0.27 and see if the issue persists ?
I did imported from import { signInWithRedirect } from 'aws-amplify/auth';
. I just upgraded from 6.0.27
to the latest because neither works.
The oauth config is correct. I believe the problem is due to this piece of code https://github.com/aws-amplify/amplify-js/blob/main/packages/auth/src/providers/cognito/apis/signInWithRedirect.ts#L119C1-L122C6
In javascript, this function won't return {type, error, url}
at all. export const openAuthSession: OpenAuthSession = async (url: string) => {
if (!window?.location) {
return;
}
// enforce HTTPS
window.location.href = url.replace('http://', 'https://');
};
The Hub events won't be triggered as it's part of the completion oauth flow https://github.com/aws-amplify/amplify-js/blob/main/packages/auth/src/providers/cognito/apis/signInWithRedirect.ts#L120-L137. The code stuck there. I assume it's an obvious bug there.
Hello @Deanfei . The code above you describe will be compiled into RN only. Web applications will behave different. Is it possible for you to provide a sample app so we can try reproducing this issue ?
Sorry, I don't have a sample app on hand. When I debug the code in the node_module of the browsers, it did reached to the code I attached. I don't understand how come it's only be compiled into RN but actually run in the browser in my react app?
I am wondering would your team like have a public repo that have the react SSO working so that we can have a reference?
I see you are using pnpm
as the package manager. Can you switch to npm
or yarn
and see if you still see the code you pointed bundled in the node_modules
?
Hi @israx Very appreciated your quick response and support. I did use npm
or yarn
for my project. pnpm
is for something else.
l eventually made it working. That's due to my signin page is lazy loaded. When I did a eager bundling with the main app. Everything just worked magically...😔 This is the code I used (just removed it fixed the issue).
const SignIn = Loadable(
lazy(async () => import('../../components/views/authentication/SignIn')),
);
I am not sure if it's reproducible on a brand new project or not and why. However, I generally felt the Amplify V6 has some problem internally. I got the exact same code with V5 which works perfectly.
I'll close this issue as I hope our Amplify could fix that in a future version. Thanks again.
I have the same issue with the signInWithRedirect, this is the code :
` const response = await signInWithRedirect({provider: { custom: 'custom-idp' }});
const { username, userId, signInDetails } = await getCurrentUser();
if(signInDetails?.loginId) {
console.log('Login !')
console.log(`The username: ${signInDetails.loginId}`);
router.push('/');
}
`
my response is the following : http://localhost:3000/?code=[Authorization_code]&state=[State_code]
I'm currently using npm
and I'm at version "aws-amplify": "^6.2.0"
It doesn't login, and there is nothing present in the cookies
I'm trying to follow the current thread, available if you need more information.
Thanks
@Deanfei, glad to hear you're unblocked!
@jojimcNova, is this a new v6 app or one that you upgraded from v5? And could you share what your signInDetails
code looks like?
Hi @cwomack ,
Yes it's a brand new project I created. I'm using NextJs, and I want to add a custom login page, where the user can login with credentials (signIn
), which currently works correctly, and via signInWithRedirect
which I have this issue.
I don't have any signInDetails
because the response I receive from my signInWithRedirect
is undefined
hello @jojimcNova . Can you add the following code into the page you are redirected to from your social provider.
import 'aws-amplify/auth/enable-oauth-listener';
This will allow the signInWithRedirect
to complete the authentication flow in multipage applications.
For more info check this doc section
Hi @israx, I added the suggested import, and there is no difference. The return value is still the same as follow :
After I'm redirected to the login page like nothing happened. When I try to console.log the answer of the signInWithRedirect
, it is undefined. I'm about to handle it manually, but maybe I can get some answer with you before starting.
Thanks
I would say the hub won't emit any events since they are part of the completion oauth flow. At this stage, the amplify should exchange access tokens with the code and state here. If it stuck here, that means we are not logged in yet.
When you see the cookies from the cognito stored in the localstorage (or session storage depends on your own config), that means we succeeded the signin.
@jojimcNova can you make sure to ?
signInWithRedirect
from the clientHi @israx , yes this is what I did :
1 - I have a login form with credentials and a custom provider. The credential way works properly, but the signInWithRedirect
is called from the client side.
const response = await signInWithRedirect({provider: { custom: 'custom-idp' }});
2 - Yes, the ssr flag is enable. I have it setup in the layout as follow :
"use client";
import config from "@/awsconfig";
import { Amplify } from "aws-amplify";
Amplify.configure(config, { ssr: true });
export default function ConfigureAmplifyClientSide() {
return null;
}
and in the layout, it is setup as follow :
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<ThemeRegistry>
<body>
<ConfigureAmplifyClientSide />
{children}
</body>
</ThemeRegistry>
</html>
);
}
By the way, when importing the import 'aws-amplify/auth/enable-oauth-listener';
when I tried to go in the function, it looks like it's empty :
export {};
Is it normal ?
@jojimcNova yeah the import 'aws-amplify/auth/enable-oauth-listener';
is used to enable a side effect only. Just to confirm, after doing all of above you are still getting the code
and state
params hanging on your URL, right ? Something to clarify is that the signInWithRedirect
API doesn't resolve to a value. You would need to listen for the signIWithRedirect
or signInWithRedirect_failure
hub events.
Also if the issue still persists, could you please open a new GH issue describing reproduction steps ? So we can assist you better.
Hi @israx,
I've tried with the Hub, there is no response. I opened a GH issue : https://github.com/aws-amplify/amplify-js/issues/13332
Before opening, please confirm:
JavaScript Framework
React
Amplify APIs
Authentication
Amplify Version
v6
Amplify Categories
auth
Backend
Amplify Gen 2 (Preview)
Environment information
Describe the bug
As suggested by AWS in the V6 migration from V5, I now use the following code for federation signin as:
However, the browser always stuck on the step
xxx.com?code=xxx&state=xxx
and not proceeded anymore. After debugging on the source code, and I found that it could be caused by this piece of code: https://github.com/aws-amplify/amplify-js/blob/main/packages/auth/src/providers/cognito/apis/signInWithRedirect.ts#L119C1-L122C6As the comment says it's only resolves on the react native but not the browser. But I assume it's shared with the react version? I am currently using
aws-amplify/auth
for my react application, should I use a different package?Expected behavior
when signed in with federation, the code should automatically handle the
code
andstate
to exchange new tokens.Reproduction steps
Code Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
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