Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Remove username field from user model #94

Open
brylie opened this issue Jan 9, 2024 · 2 comments
Open

[Feature]: Remove username field from user model #94

brylie opened this issue Jan 9, 2024 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@brylie
Copy link
Member

brylie commented Jan 9, 2024

Feature Description

Remove the username field from the user model and use the email address as username.

Motivation

We don't have a use case for usernames.

@brylie brylie added the enhancement New feature or request label Jan 9, 2024
@brylie brylie added good first issue Good for newcomers help wanted Extra attention is needed labels Jan 9, 2024
@hudy0
Copy link

hudy0 commented Jan 16, 2024

Hello, @brylie I was working on this issue.

Issue

The task is to eliminate the username field from the user model while retaining the ability for users to log in using either a username or an email address.

Changes Made

Email Backend

UserModel = get_user_model()
class EmailBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            user = UserModel.objects.get(
                Q(email__iexact=username),
            )
        except UserModel.DoesNotExist:
            UserModel().set_password(password)
            return
        except UserModel.MultipleObjectsReturned:
            user = (
                UserModel.objects.filter(
                    Q(email__iexact=username),
                )
                .order_by("id")
                .first()
            )

        if user.check_password(password) and self.user_can_authenticate(user):
            return user

LoginForm

class LoginForm(AuthenticationForm):
    username = forms.CharField(label="Email / Username")

User Model

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_("email address"), unique=True)

LoginView

class LoginView(auth_views.LoginView):
    form_class = LoginForm
    template_name = "registration/login.html"

These changes have been tested and if are compatible with the project's requirements.
I will submit the Pull request
Screenshot 2024-01-16 at 03 08 51

@brylie
Copy link
Member Author

brylie commented Jan 16, 2024

It looks good so far. Go ahead and fork this repository, commit your changes to your fork, and open a pull request. That way we can review and discuss the code. It will also make it easier to give you credit for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

2 participants