-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Nextjs middleware and social Google login #13056
Comments
+1 |
Hi @Maayanyeru123 @hasadata thanks for opening this issue. Looking at @Maayanyeru123 's configuration, I anticipate you are calling |
adding the import fixed the issue |
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. |
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! |
Hey,
The |
Can you share please more on how did call the Redirect. Im facing the same issue. It'll be helpful to me |
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 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 |
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
2. login with Google account
3. Going to Home
4. returns to sign in as user is not authenticated
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
The text was updated successfully, but these errors were encountered: