[Answer]-Django First time Login detection with django-allauth

1πŸ‘

βœ…

I still don’t know whats the problem, but i ended up working around the issue by creating a boolean field in the db that tells me if the log in has happened or not

def check_if_first_time_login(user):
    is_first_time_login = None
    for type in ['administrator', 'agent']:
        try:
            user_type = getattr(user, type)
            is_first_time_login = user_type.is_first_time_login
        except ObjectDoesNotExist:
            pass
        else:
            log.info('The user {user} is an {type}'.format(**locals()))
            user_type.is_first_time_login = False
            user_type.save()

    return is_first_time_login


class AccountAdapter(DefaultAccountAdapter):

    def get_login_redirect_url(self, request):
        user = get_object_or_404(get_user_model(), pk=request.user.id)

        is_first_time_login = check_if_first_time_login(user)

        if is_first_time_login:
            log.info(
                'The user {user} is login in for the first time so '
                'lets set a new password'.format(**locals())
            )
            url = '/accounts/password/change/'
        else:
            log.info(
                'The user {user} is NOT login in for the first '
                'time'.format(**locals())
            )
            url = settings.LOGIN_REDIRECT_URL
        return resolve_url(url)
πŸ‘€psychok7

Leave a comment