ellaisys / aws-cognito

AWS Cognito package (with MFA Feature) using the AWS SDK for PHP/Laravel
https://ellaisys.github.io/aws-cognito/
MIT License
110 stars 41 forks source link

InvalidTokenException is thrown occasionally when the app runs on aws #10

Closed iturcanu closed 2 years ago

iturcanu commented 3 years ago

Hello, I am using this package and works like a charm, however, sometimes it throws an InvalidTokenException, and if I am running the same request, everything works alright. This happens only when the application is running on aws lambda, while on linux this error doesn't occur.

Does anyone have any idea why this would happen?

davidemorotti commented 2 years ago

Same problem here! I get the InvalidTokenException after a couple of refreshes when running the app on Vapor. I also tried to set the SESSION_DRIVER to DynamoDB, but it didn't help!

davidemorotti commented 2 years ago

Well... it appears that the package is saving tokens on files per default, ignoring the SESSION_DRIVER settings, I found this out by digging a bit more into the code.

Running the application on a serverless environment requires the usage of a separated persistent caching solution like Redis (running on AWS) or DynamoDB.

To make the package saving the tokens on those databases, you need to specify the correct driver in the environment file using the key AWS_COGNITO_TOKEN_STORAGE. As a value, you can use whichever standard Laravel driver you'd like to use.

nicholasc commented 2 years ago

☝️ This. It is not documented but you will have to specify a storage driver other than file when using a lambda. I use a redis instance in my case and everything works fine now.

Thank you for saving me time.

amitdhongde commented 2 years ago

The package has ability to manage the sessions and token via AWS DynamoDB. We are using it for a production deployment for over 6 months. The documentation is now updated with the details.