Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:23:10+00:00 2026-06-14T17:23:10+00:00

I am writing a custom authentication backend (and a custom user model) to check

  • 0

I am writing a custom authentication backend (and a custom user model) to check if a user account is locked or expired. I see that in all the examples of authentication backend return value is either a User object or None. And the only exception generated is User.DoesNotExist.

My question is how should I go about returning different results (e.g. account is locked or expired or max number of login attempts have reached)?

Should I raise custom exceptions or there is another way to do this?

I am using Django 1.5alpha.

EDIT:
I need to get multiple statuses to display appropriate message to a user and redirect to an appropriate view.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:23:11+00:00Added an answer on June 14, 2026 at 5:23 pm

    I ended up doing this in case anyone has the same question.

    class UserNotActivatedError(Exception):
        pass
    
    class UserLockedError(Exception):
        def __init__(self, remaining_mins):
            self.remaining_mins = remaining_mins
    
    # backend
    def authenticate(self, email=None, password=None):
        if email is None or password is None:
            return None
        try:
            user = ExtUser.objects.get(email=email)
            user.last_login_attempt_at = timezone.now()
            if not use.is_active:
                raise UserNotActivatedError
            if user.is_locked:
                # Check when it was locked and found the duration
                sec_to_go = user.get_remaining_locktime()
                if sec_to_go:
                    raise UserLockedError(sec_to_go)
            if user.check_password(password):
                user.last_login_at = timezone.now() 
                return user
            else:
                return None
        except User.DoesNotExist:
            return None
    

    Then in the login form you can catch these errors and pass on appropriate validation errors to the view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implemented a django custom authentication backend. My authenticate() returns a user object like
I'm writing a WCF service with some authentication and a custom error handler. However,
I am using Spring Framework . While writing custom queries, I am unable check
I am writing a custom .pac script for use with Firefox. Following numerous examples
I'm writing a custom deserializer that will deserialize a list by deserializing each of
I've got a site that uses Forms Authentication (with a custom membership provider, but
It is strange. I'm writing custom user registration and want to make redirection to
I'm writing a custom authentication adapter in Zend Framework 2. module/Application/src/Application/Auth/Adapter/Auth_Adapter.php - namespace Application\Auth;
I'm writing a RESTful webservice that returns custom/existing HTTP headers on successful/unsuccessful operations. For
I'm aware of writing custom attributes that decorate a method and get evaluated before

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.