I’m having an issue with user authentication using the social-auth-app-django
library in my Django project. After the user is successfully authenticated via a social provider (in this case, Google), the user doesn’t seem to be authenticated when redirected to the dashboard
view.
Code
# Decorator to check if the user is authenticated and verified
def verified_user_required(view_func):
print('nDecorator: verified_user_required')
@wraps(view_func)
def wrapper(request, *args, kwargs):
print('Verified user required')
if not request.user.is_authenticated:
print('User not authenticated')
return redirect('login')
nwd_user = get_nwd_user(request.user)
if not nwd_user or not nwd_user.is_verified():
print('User not verified')
return redirect('verify-email')
print('User verified')
return view_func(request, *args, kwargs)
return wrapper
# View function to handle social auth callback
@psa('social:complete')
def auth_callback(request, backend):
print("nView Auth Callback")
# Retrieve the backend and user ID from the session
backend_name = request.session.get('auth_backend')
user_id = request.session.get('authenticated_user_id')
print("Backend from session:", backend_name)
print("User ID from session:", user_id)
# Ensure the backend is correctly set
if not backend_name or not user_id:
return redirect('login')
# Get the user object
try:
user = User.objects.get(id=user_id)
except User.DoesNotExist:
return redirect('login')
# Log in the user
login(request, user, backend=backend_name)
# Debugging: Print the backend and user information
print("Backend:", backend)
print("Request Backend:", request.backend)
print("User:", request.user)
# Check if the user is authenticated
if request.user.is_authenticated:
# Perform any additional logic you need here
print(f"User {request.user.email} has been authenticated with {backend_name}.")
return redirect('dashboard')
else:
return redirect('login')
# View function for the dashboard
@verified_user_required
def dashboard(request):
print('n View Dashboard')
return render(request, 'dashboard.html', {})
The auth_callback view is designed to add my user wrapper model to the model created by the auth, and then authenticate this user. The user is then redirected to the dashboard, but first is passed through the verified_user_required
decorator, which is where the authenticated state is lost. The console output can be seen below:
[04/Jun/2024 10:56:33] "GET /accounts/complete/google-oauth2/?state=...Q&scope=email%20profile%20openid%20https://www.googleapis.com/auth/userinfo.profile%20https://www.googleapis.com/auth/userinfo.email&authuser=1&prompt=consent HTTP/1.1" 302 0
User authenticated: {'username': 'XXXXX', 'email': '[email protected]', 'first_name': 'XXXXX', 'last_name': 'XXXXX', 'extra_data': {'auth_time': 1717494993, 'expires': 3599, 'token_type': 'Bearer', 'access_token': '...'}}
User authenticated: XXXXX
Redirecting to auth-callback with backend: google-oauth2
View Auth Callback
Backend from session: google-oauth2
User ID from session: 98
Backend: google-oauth2
Request Backend: <social_core.backends.google.GoogleOAuth2 object at 0x0000027977EE6C10>
User: [email protected]
User [email protected] has been authenticated with google-oauth2.
[04/Jun/2024 10:56:33] "GET /auth-callback/google-oauth2 HTTP/1.1" 302 0
[04/Jun/2024 10:56:33] "GET /dashboard/ HTTP/1.1" 302 0
Verified user required
User not authenticated
Login View
not post
[04/Jun/2024 10:56:33] "GET /login/ HTTP/1.1" 200 46054
Why does django seem to lose track of this session authentication state between the auth_callback
view and the verified_user_required
decorator?