Programmatic login succeeds, but not recognized
I have a magnet link feature where my web app sends a login URL with a username & one-time token encrypted. When clicking the link on the email, the user is sent to an authentication where I programmatically log in and then redirect to the member page.
Authentication and login are successful, but when I redirect to the main page, Django sends the user back to the login page. However, when I click the logo, the main page shows.
authentication & login code
def magnet_link_login(request, *args, **kwargs): if request.user and request.user.id != None: return redirect('stream:home') magnet_token = request.GET['key'] if 'key' in request.GET else '' decrypted_token = decrypt_text(magnet_token) split_texts = decrypted_token.split('&') if len(split_texts)<2: #invalid and redirect to an invalid magnet link page pass uname = split_texts.split('=') auth_token = split_texts.split('=') #fetch the user record acc = Account.objects.get(slug=uname) if not acc: #error and redirect to an invalid account name page pass #validate and login try: logged_user = authenticate(username=acc.username,password=auth_token) if logged_user is not None: login(request, logged_user) return redirect('stream:home') else: return redirect('invalid-link') except Exception as e: print(e) return redirect('invalid-link')
My member page (stream:home) is a CBV.
class Home(LoginRequiredMixin, ListView): paginate_by = 6 context_object_name = 'content_list' ***SOME STUFF HERE*** def get_queryset(self): *** SOME STUFF HERE*** def get_context_data(self, **kwargs): *** SOME STUFF HERE*** return context
Except for LoginRequiredMixin at the CBV, I do not check login explicitly and redirect to the login page (the login page is defined at the settings.py). I have already checked handful of threads dealing with similar issues, but my issue is not resolved.
What am I doing wrong here?
The code looks fine to me.Try Removing LoginRequiredMixin method and explicitly check if user is logged in or not.Check if that works?