Closed Maayanyeru123 closed 1 month ago
+1 I am using amplify Authenticator UI and facing the same issues, the cookies are not set, the middleware is unable to retrieve the user
Hi @Maayanyeru123 @hasadata thanks for opening this issue.
Looking at @Maayanyeru123 's configuration, I anticipate you are calling signInWithRedirect
from the route /signin
, after your end user sign in at Google's UI, it redirects your end user back to the route /home
. If that's the case, you would need to follow this instruction to ensure the OAuth completion listener is added to your /home
route so it can completes the sign in flow and persist the auth token in cookie store.
adding the import fixed the issue
Thanks for the confirmation, @hasadata.
@Maayanyeru123, let us know if it resolves it for you as well.
Thanks for the confirmation, @hasadata.
@Maayanyeru123, let us know if it resolves it for you as well.
Not for me :) probably something I am missing here. The Home page is secured asset so implementing the import 'aws-amplify/auth/enable-oauth-listener'; will even work?
I fixed it with an ugly hack i created a middle page called redirect but this is so ugly
@Maayanyeru123, would you mind sharing the hacky way that you had to work around this? Wondering if we can provide any suggestions on it if you can share the frontend code. Thanks!
@Maayanyeru123, would you mind sharing the hacky way that you had to work around this? Wondering if we can provide any suggestions on it if you can share the frontend code. Thanks!
Hey,
So I have created the following page redirect that is not protected by the middleware it imports aws-amplify/auth/enable-oauth-listener
it is working perfectly fine but when i am putting it on the protected resource(Home Page) it doesn't work
"use client";
import 'aws-amplify/auth/enable-oauth-listener';
import { useRouter } from "next/navigation";
import { useAppSelector } from '@/app/redux/store';
import { useEffect } from 'react';
import { HOME} from '@/app/utils/client/consts';
import { getCurrentUser } from "aws-amplify/auth";
function Redirect() {
const router = useRouter();
const username = useAppSelector((state:any)=>state.AuthReducer.value.username);
let currentUser;
useEffect(() => {
async function goToHome() {
currentUser = await getCurrentUser();
router.push(HOME);
}
goToHome();
}, []);
return (
<div>
{currentUser}
</div>
);
};
export default Redirect;
The currentUser = await getCurrentUser();
returns the user correctly.
Can you share please more on how did call the Redirect. Im facing the same issue. It'll be helpful to me
Hi @Maayanyeru123 @hasadata thanks for opening this issue.
Looking at @Maayanyeru123 's configuration, I anticipate you are calling
signInWithRedirect
from the route/signin
, after your end user sign in at Google's UI, it redirects your end user back to the route/home
. If that's the case, you would need to follow this instruction to ensure the OAuth completion listener is added to your/home
route so it can completes the sign in flow and persist the auth token in cookie store.
Hi I'm having a similar issue although I'm using next 14 BUT with the pages router and authenticating towards a custom provider.
The authentication works out fine but my middleware seems to run - and call fetchAuthSession (just like in the docs) - before the cookies have been stored, when loading e.g. "../overview?code=.." thus returning undefined tokens. If I then manually refresh the page e.g. "../overview" in the browser the tokens are returned correctly in the middleware.
I haven't been able to make it work when by importing import 'aws-amplify/auth/enable-oauth-listener';
but maybe I need to do something else when using the pages router?
Thanks in advance!
Hi @jonaskinnvall the issue you are facing is somewhat a limitation of doing the final step of the OAuth sign in on the client side - exchanging auth tokens using the code. When the Hosted UI redirects back to the redirect URL you specified, before the auth tokens are exchanged, there is no valid user session, therefore if you are redirecting back to a protected route, the fetchAuthSession call in the middleware will return nothing.
Hence, my recommendation here is that the redirect URL should be an unprotected route, e.g. it can be the sign-in page again, in which you can add a Hub listener to listen to signedIn
event, when it triggers, you can then navigate to another protected route that you prefer.
Thanks for the quick answer - very good insight :) I solved it for the time being by redirecting to sign-in - as you also suggest - but instead render a kind of "go to application" button for the user to click which links to the protected route and then it of course works! But I'll try the Hub out to, which I've actually used like this previously, don't know why I didn't come to think of that now...
@Maayanyeru123, wanted to check in and see if there's still a blocker here or if the workaround is still working for you. This comment above may also be helpful, but let us know if there's a need to keep this issue open or help further.
Thanks!
Closing this issue as we have not heard back from you. If you are still experiencing this, please feel free to reply back and provide any information previously requested and we'd be happy to re-open the issue.
Thank you!
Before opening, please confirm:
JavaScript Framework
React
Amplify APIs
Authentication, GraphQL API, Storage
Amplify Version
v6
Amplify Categories
auth
Backend
Amplify CLI
Environment information
Describe the bug
When using NextJs middleware as stated in Amplify documentation after signing in with Google provider "await fetchAuthSession" Doesn't return the signing user
Expected behavior
Getting the signing user session
Reproduction steps
1, going to app Home page
Code Snippet
aws-exports.js
serverUtils.ts
layout.tsx
middleware.ts
SocialLoginButton.tsx
Log output
aws-exports.js
/ eslint-disable / // WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.
const awsmobile = { "aws_project_region": "eu-west-1", "aws_cognito_identity_pool_id": "XXXX", "aws_cognito_region": "eu-west-1", "aws_user_pools_id": "XXXXX", "aws_user_pools_web_client_id": "XXXXX", "oauth": { "domain": "XXXXXX", "scope": [ "phone", "email", "openid", "profile", "aws.cognito.signin.user.admin" ], "redirectSignIn": "http://localhost:3000/home", "redirectSignOut": "http://localhost:3000/login/signin", "responseType": "code" }, "federationTarget": "COGNITO_USER_POOLS", "aws_cognito_username_attributes": [], "aws_cognito_social_providers": [ "GOOGLE" ], "aws_cognito_signup_attributes": [ "EMAIL" ], "aws_cognito_mfa_configuration": "OFF", "aws_cognito_mfa_types": [ "SMS" ], "aws_cognito_password_protection_settings": { "passwordPolicyMinLength": 8, "passwordPolicyCharacters": [] }, "aws_cognito_verification_mechanisms": [ "EMAIL" ] };
export default awsmobile;
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