# Need advise for user authentication

**URL:** <https://forum.djangoproject.com/t/need-advise-for-user-authentication/23745>\
**Category:** Getting Started\
**Created:** [September 9, 2023, 12:12pm UTC](https://forum.djangoproject.com/t/need-advise-for-user-authentication/23745 "2023-09-09T12:12:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![perry7t](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/perry7t/32/12386_2.png) [@perry7t](https://forum.djangoproject.com/u/perry7t)\
**Post date:** [September 9, 2023, 12:12pm UTC](https://forum.djangoproject.com/t/need-advise-for-user-authentication/23745/1 "2023-09-09T12:12:07Z")

</div>

I build a planning app that can be used by several customers. Data is separated with an owner field. So far so good. I am struggling to create a user friendly sign in proces. I would like to have email as username (no separate username!). And then a one on one relation with a profile model.  
What would be a good practice setup to do this? Once working I want to be able to style the login form.  
Thanks for your concern

---

<div class="post-metadata">

**Author:** ![foxy4096](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/foxy4096/32/29065_2.png) [@foxy4096](https://forum.djangoproject.com/u/foxy4096)\
**Post date:** [September 9, 2023, 2:33pm UTC](https://forum.djangoproject.com/t/need-advise-for-user-authentication/23745/2 "2023-09-09T14:33:19Z")

</div>

Ok let’s break it down

- You want to have a custom user which have email and password, no username, you can easily do that  
you just need to create a custom user extending the `AbstractBaseUser`

Example:

```py
# accounts/models.py

from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    email = models.EmailField(max_length=254)
    
    USERNAME_FIELD = "email"

    def __str__ (self):
        return self.username

```

After this add this in your `settings.py`

```py
AUTH_USER_MODEL = "accounts.CustomUser"

```

Ref: [Customizing authentication in Django | Django documentation | Django (djangoproject.com)](https://docs.djangoproject.com/en/4.2/topics/auth/customizing/#specifying-a-custom-user-model)

- For creating the o2o with CustomUser - UserProfile, do something like this

```py
# accounts/models.py

from django.db import models

class UserProfile(models.Model):
    user = models.OneToOneField(
       to=CustomUser, on_delete=models.CASCADE
    )
    # Other fields

```

To automatically create ceate a userprofile when user is created use signals

```py
# accounts/signals.py

from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import UserProfile, CustomUser

@receiver(post_save, sender=CustomUser)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

```

And just hook up the signals in the apps.py

```py
# accounts/apps.py

from django.apps import AppConfig

class AccountsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'apps.accounts'

    def ready(self):
        from . import signals

```

Now the UserProfile will automatically gets created whenever a CustomUser get created

- Now to the last part, for styling you can use css and forms widgets or if you are using bootstrap just simply use [`crispy_forms`](https://pypi.org/project/django-crispy-forms/) package

Ref: [Using Django Form Fields, Widgets, and Attributes (ordinarycoders.com)](https://ordinarycoders.com/blog/article/using-django-form-fields-and-widgets)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 9, 2023, 3:11pm UTC](https://forum.djangoproject.com/t/need-advise-for-user-authentication/23745/3 "2023-09-09T15:11:13Z")

</div>

As an alternative - I always look for third-party packages to handle situations that seem “common” to me, and the place to look is `djangopackages.org`. [Searching for `email`](https://djangopackages.org/search/?q=email) shows a number of different packages. Two appear to be likely candidates, with recent release dates:

- [Django Packages : django-username-email](https://djangopackages.org/packages/p/django-username-email/)  
and
- [Django Packages : Django use Email as Username](https://djangopackages.org/packages/p/django-use-email-as-username/)

Whether you choose to use them or not is your decision - but if nothing else, you might want to look at them to see what they do and the features they provide - it might give you some ideas on your own.

> [@perry7t](#):
>
> And then a one on one relation with a profile model.

Contrary to the above response, I will not use a signal unless I have no other choice. You have a couple different options for this.

1. In your “profile edit” page, use `get_or_create` to cause the profile to be created the first time someone attempts to edit it.
2. Override the appropriate part of the user-creation process to create the `Profile` instance at the same time.

> [@perry7t](#):
>
> Once working I want to be able to style the login form.

See: [Using the Django authentication system | Django documentation | Django](https://docs.djangoproject.com/en/4.2/topics/auth/default/#module-django.contrib.auth.views)

---

<div class="post-metadata">

**Author:** ![foxy4096](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/foxy4096/32/29065_2.png) [@foxy4096](https://forum.djangoproject.com/u/foxy4096)\
**Post date:** [September 16, 2023, 1:39pm UTC](https://forum.djangoproject.com/t/need-advise-for-user-authentication/23745/4 "2023-09-16T13:39:02Z")

</div>

> Contrary to the above response, I will not use a signal unless I have no other choice.

Mr. Ken, why would you not want to use signals in this scenario.

I’ve seen and used this pattern many times

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 16, 2023, 2:07pm UTC](https://forum.djangoproject.com/t/need-advise-for-user-authentication/23745/5 "2023-09-16T14:07:08Z")

</div>

> [@foxy4096](#):
>
> Mr. Ken, why would you not want to use signals in this scenario.

For clarity, it’s not _just_ this scenario.

In general, I **never** _want_ to use signals. However, sometimes I _need_ to based on circumstances outside my control.

First, I’ll start out from this quote from [the official Signals docs](https://docs.djangoproject.com/en/4.2/topics/signals/):

> Warning  
> Signals give the appearance of loose coupling, but they can quickly lead to code that is hard to understand, adjust and debug.  
> Where possible you should opt for directly calling the handling code, rather than dispatching via a signal.

Also, there are operations on models (e.g. [bulk\_create](https://docs.djangoproject.com/en/4.2/ref/models/querysets/#bulk-create)) that don’t fire signals on models.

For another perspective, see: [Django Anti-Patterns: Signals | Lincoln Loop](https://lincolnloop.com/insights/django-anti-patterns-signals/). I didn’t write it, but I agree with it wholeheartedly.

I have encountered too many situations with code written to use signals ending up misbehaving because of other code being written that doesn’t account for the edge cases involved.

On the other hand, I have never worked with a system using signals where they’re not needed that wasn’t impoved overall by removing the signal.  
In all cases, the developers recognized that the “readability” and “understandability” was improved by _not_ having to **remember** that there _might_ be signals being called when a model is being saved - that having that code _with_ the model or even in the `save` method, was much easier to keep track of.

Now, I’m well aware that there are many very-experienced Django people who disagree with me, and that’s fine and good. Signals _can_ be useful, if you are always aware of their limitations. I’ve just made the decision that the balance-point lies on the side against using them.

---

<div class="post-metadata">

**Author:** ![foxy4096](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/foxy4096/32/29065_2.png) [@foxy4096](https://forum.djangoproject.com/u/foxy4096)\
**Post date:** [September 16, 2023, 2:36pm UTC](https://forum.djangoproject.com/t/need-advise-for-user-authentication/23745/6 "2023-09-16T14:36:21Z")

</div>

I just read the blog post which you have mentioned.

Well I can agree on some points why we should avoid signals:

- makes it hard debug (Totally)
- decreases the readablity (Like if you need to know how something is happening behing the scenes, you need to look in the other files, unlike if we just impleament the same things in the models without extra config, like we have seen with signals to use it, we have to configure the `apps.py`)

But somethings we might need to use signals  
Well it’s just totally up to developers to which coding pattern they follow  
Everyone have a unique way in making things

---

<div class="post-metadata">

**Author:** ![perry7t](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/perry7t/32/12386_2.png) [@perry7t](https://forum.djangoproject.com/u/perry7t)\
**Post date:** [September 22, 2023, 7:14am UTC](https://forum.djangoproject.com/t/need-advise-for-user-authentication/23745/7 "2023-09-22T07:14:02Z")

</div>

After studying options I choose cuser as authentication. And a profile view that uses [get\_or\_create](https://docs.djangoproject.com/en/4.2/ref/models/querysets/#get-or-create) to check if the current user already has a profile record. This looks like the most straight forward simple solution.
