3

I have hosted my app to AWS with Cloudfront. I am using socialite for Social login. Facebook login works fine, but in Google login, it callback to ec2-x-xx-xxx-xxx.ap-northeast.AmazonAWS... Insted of callback to my URL.

For this I am getting InvalidStateException.

I have tried with stateless but no luck.

    public function redirectGoogleLogin()
    {
      
        return Socialite::driver('google')->stateless()->redirect();
    }
    
    public function callbackGoogleLogin()
    {
        $socialUser = Socialite::driver('google')->stateless()->user();
    }
1
  • Have you tried setting the correct, full URL of the callback (with protocol and domain) in the redirect config value of Socialite? If you only set a relative path there, it may perhaps end up using the wrong domain.
    – CherryDT
    Commented Jun 5 at 16:21

1 Answer 1

-1

The issue you're encountering where the Google login callback directs to an AWS IP address instead of your domain name is typically related to how OAuth callbacks are configured and how your server environment (in this case, AWS with Cloudfront) handles HTTP headers, particularly the Host header. Here’s how you can troubleshoot and resolve this problem:

1. Verify OAuth Callback URLs

First, ensure that the callback URL configured in the Google Developer Console matches exactly the URL you expect. It should not point to an IP address but to a domain name.

  • Go to the Google Developer Console.
  • Select your project.
  • Navigate to the OAuth consent screen and check the Authorized redirect URIs.
  • Make sure that the URL is exactly as you want it, using your domain name and not the IP address.

2. Check Laravel Configuration

Ensure your Laravel application is correctly configured to generate URLs using your domain name:

  • config/app.php: Check that the url configuration is set to your domain.
    'url' => env('APP_URL', 'https://your-domain.com'),
    
  • Environment Configuration: Ensure your .env file has the APP_URL set to your domain.
    APP_URL=https://your-domain.com
    

3. Adjust Cloudfront Settings

Since you’re using AWS Cloudfront, you might encounter issues where Cloudfront doesn’t pass the original Host header to your EC2 instances. Here’s how you can adjust that:

  • Go to the AWS Management Console.
  • Navigate to your Cloudfront distribution.
  • Go to the Behaviors tab.
  • Edit the behavior used for your application.
  • Set the Cache Based on Selected Request Headers option to "Whitelist".
  • Add Host to the whitelist of headers.
  • This ensures that the Host header containing your domain name is passed to your backend application.

4. Laravel Trust Proxies Configuration

If your application is behind a reverse proxy (like Cloudfront), ensure Laravel trusts the X-Forwarded-* headers:

  • Edit or create the file app/Http/Middleware/TrustProxies.php.
  • Make sure your middleware looks like this:
    <?php
    
    namespace App\Http\Middleware;
    
    use Illuminate\Http\Request;
    use Fideloper\Proxy\TrustProxies as Middleware;
    
    class TrustProxies extends Middleware
    {
        /**
         * The trusted proxies for this application.
         *
         * @var array|string
         */
        protected $proxies = '*';  // Trust all proxies
    
        /**
         * The headers that should be used to detect proxies.
         *
         * @var int
         */
        protected $headers = Request::HEADER_X_FORWARDED_ALL;
    }
    

5. Testing

After making these changes, clear your application cache:

php artisan config:cache
php artisan cache:clear

Then, try the login process again.

Additional Notes

  • Ensure that any security groups and firewall settings in your AWS configuration allow for proper communication and don't inadvertently rewrite headers or redirect traffic.
  • Check the network configuration to make sure that incoming requests to your domain are not being misrouted or intercepted in a way that changes the requested URL.

If these steps don't resolve your issue, you might need to look deeper into the network flow or potentially consult AWS support to understand how the traffic routing and DNS resolution is being handled for your setup.

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.