Search code examples
djangodjango-allauth

Why is django allauth redirecting to /accounts/login/?=next/MY_PAGE instead of logging in?


I am attempting to handle user authentication on a site using Django allauth. However, upon entering a username and password and clicking "log in" on my site, I am redirected to /accounts/login/?=next/MY_PAGE and the login page is simply reloaded. I have seen a number of similar questions, and I know that sometimes people run into issues with this when authenticating with socials, but I am not using any form of social authentication. I already have the LOGIN_REDIRECT_URL set to the desired page's URL, and I attempted to create an adapter as follows:

class AccountAdapter(DefaultAccountAdapter):
    def is_open_for_signup(self, request):
        return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True)

    def get_login_redirect_url(self, request):
        path = "/news/"
        return path

where get_login_redirect_url should direct the application to the desired page. I have read in other posts that the fact that the next page (in this case "News") isn't being loaded means that allauth is not actually logging me in, and that's why I end up just getting the login page reloaded. Would anyone be able to point me in the right direction? Thanks!

ADDITIONAL INFO Here is how I am attempting to handle the login now.

<!-- <form class="login" method="POST" action="{% url 'account_login' %}"> -->
            <form class="login" method="POST" action="{% if request.path|slice:':6' == '/login' %}/news{% else %}{{request.path}}{% endif %}">
            {% csrf_token %}
            {{ form|crispy }}
            {% if redirect_field_value %}
              <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
            {% endif %}
            <div class="form-group container-login100-form-btn">
              <button type="submit" class="login100-form-btn">{% trans 'Log In' %}</button>
            </div>
            <div class="form-group container-login100-form-btn">
              <a href="{% url 'account_signup' %}" class="btn btn-link">{% trans 'Or sign up for ConnectHer here' %}</a>
              <a href="{% url 'account_reset_password' %}" class="btn btn-link pull-right">{% trans 'Forgot your password?' %}</a>
            </div>
          </form>

The commented out line at the top was my orig version of the form before attempting to fix my problem.


Solution

  • How are you showing the login link to the user? I'd bet that the link on your login page is something like:

    <a href="/login?next={{ request.path }}">Login</a>
    

    which is basically (when on the login page):

    <a href="/login?next=/login">Login</a>
    

    which send you right back. You can test in the template to set the next= param:

    <form class="login" method="POST" action="/login?next={% if request.path|slice:':6' == '/login' %}/news{% else %}{{request.path}}{% endif %}">
    

    Bonus:

    Here is how to add a |startswith filter to your template:

    def startswith(val1, val2):
        if not val1:
            return False
        else:
            return val1.startswith(val2)
    
    register.filter("startswith", startswith)