Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(auth): redirect social login provider errors to the login page (#129780)

Rafael Audibert7ace68991608
diff --git a/posthog/middleware.py b/posthog/middleware.py
index f07c5bab17f0..aba4b134eda9 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -32,6 +32,7 @@ from django.utils.deprecation import MiddlewareMixin
from django.utils.functional import SimpleLazyObject
from django.utils.http import http_date, url_has_allowed_host_and_scheme
+import requests
import structlog
import posthoganalytics
from django_prometheus.middleware import Metrics
@@ -1626,6 +1627,11 @@ class SocialAuthExceptionMiddleware:
error_code = self._INVITE_ERROR_CODES.get(self._error_code(exception), "social_login_failure")
return redirect(sso_failure_redirect_url(request, error_code))
+ # social-core wraps only ConnectionError from provider calls, so a read timeout or a provider 5xx
+ # arrives here as a plain requests error. The detail holds the provider URL, so it stays out of the redirect.
+ if isinstance(exception, requests.RequestException):
+ return redirect(sso_failure_redirect_url(request, "social_login_failure"))
+
return None
def _error_code(self, exception: ValidationError) -> str:
diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.py
index c6447e87b661..41bc353a0b12 100644
--- a/posthog/test/test_middleware.py
+++ b/posthog/test/test_middleware.py
@@ -23,6 +23,7 @@ from django.test import (
)
from django.urls import path, resolve, reverse
+import requests
import structlog
from loginas import settings as la_settings
from parameterized import parameterized
@@ -2540,6 +2541,21 @@ class TestSocialAuthExceptionMiddleware(SimpleTestCase):
ValidationError("Something else.", code="something_else"),
"/login?error_code=social_login_failure",
),
+ (
+ "provider_read_timeout",
+ "/complete/google-oauth2/",
+ requests.exceptions.ReadTimeout("Read timed out."),
+ "/login?error_code=social_login_failure",
+ ),
+ (
+ "provider_server_error",
+ "/complete/github/",
+ requests.exceptions.HTTPError(
+ "500 Server Error: Internal Server Error for url: https://example.com",
+ response=requests.Response(),
+ ),
+ "/login?error_code=social_login_failure",
+ ),
]
)
def test_redirects_with_expected_url(self, _name, path, exception, expected_url):