Separate AUTH_USER_MODEL for Django Rest Framework - django

The Django-Project contains a REST-API, based on TokenAuthentication and a Web Login to the Backend with the Django Auth User.
The Problem is: I found no way to specify two different AUTH_USER_MODEL'S for each authentication individually.
For the REST-API users, I defined a Custom User Model:
class User(models.Model):
id = models.IntegerField(primary_key=True)
name = ....
....
USERNAME_FIELD = 'id'
REQUIRED_FIELDS = []
In the settings I added AUTH_USER_MODEL = 'backend.User', so that the rest_framework Token authentication references to the right user model.
But when enabling the django.contrib.auth.views.login for the Web-Backend, this also uses the backend.Usermodel for authentication. But it should use the django built in user model.
How can I specify a second user model specifically for either the rest framework api or for the django.contrib.auth.views?

Related

Django DRF Djoser - change default User model to custom model which is not equal but inherited from AUTH_USER_MODEL

In django have the following custom user structure:
class CinemaUser(AbstractUser): ...
class CinemaAdmin(CinemaUser):....
class Cinemagoer(CinemaUser): ...
Logic is that I have
Cinemagoer - an ordinary user (can register themselves),
CinemaAdmin - kind of administrator (is created only by superuser) and
Superuser.
in settings: AUTH_USER_MODEL = CinemaUser
the problem is with Djoser,
a request http://localhost:8000/auth/users/ (with data in body) creates CinemaUser, because Djoser has User = get_user_model(), which means Djoser uses AUTH_USER_MODE, while I need Djoser to use Cinemagoer model to create Cinemagoer). How can I use Cinemagoer with Djoser in this case?
I tried to change in Djoser/serializers directly User = CinemaUser ... just for test, it works fine, but it is definitely the wrong way.

How to change default USER Authorisation to custom model in DRF

I have model.py file which has one model named as Tutor
class Tutor(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
email = models.CharField(max_length=255)
password = models.CharField(max_length=255)
I want to implement token authentication using this Tutor model, for login and signup. As of now I have setup token authentication and its working good, but its default authorisation is for USER, which get generated using python manage.py createsuperuser, and I want to use Tutor for Authroization.
How to go building this, any resources for my usecase would mean a lot :)
You can change your default auth model by updating the AUTH_USER_MODEL = "<app_name>.<model_name>" in settings.py
In your case, if your app name is User, it would be AUTH_USER_MODEL = "User.Tutor"

how to receive username with token by django rest authetication?

I am using django Django=2.1.7 and rest framework djangorestframework=3.9.2 This is my url for login
path('rest-auth/login', include('rest_auth.urls')),
When I enter username and password I got the token from rest API. But I want my user detail like name, id etc to show in my react components. Please help me how to achieve. I have seen many answers on StackOverflow even the official documentation is not descriptive https://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication
If you use rest-auth you can get the id, username, email, first_name and last_name of the logged user using this URL: http://localhost:8000/rest-auth/user/
The mentioned package provides us the ability to override the settings
In the login process, the response comes from either TOKEN_SERIALIZER or JWT_SERIALIZER. In your case, I assume that you are not using the JWT method.
So, create a new serializer class as per your desired structure and wire it up using REST_AUTH_SERIALIZERS settings dictionary.
Here is one sample
#serializer.py
from django.contrib.auth import get_user_model
from rest_framework import serializers
from rest_framework.authtoken.models import Token
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = ('id', 'username', 'email')
class MyCustomTokenSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True)
class Meta:
model = Token
fields = ('key', 'user')
and in your settings.py,
REST_AUTH_SERIALIZERS = {
'TOKEN_SERIALIZER': 'path.to.custom.MyCustomTokenSerializer',
...
...
}
There is no way to automatically populate user data once a user logs in. You should just be able to take the username the user entered during the login process, save it in a variable, and after receipt of token make another API call to get the user information.

Python Social Auth. relate to existing Profiles model

I just included python social auth and added Facebook login. I have a Profile model where I save some user data like "description" and "username".
class Profile(models.Model):
username = models.CharField(max_length=75)
user_des = models.CharField(max_length=250, blank=True, null=True)
...
How do I relate the user's Facebook account to the existing model, and where do I save that relation?
Looking at the social auth docs I found that you can specify custom user models using this in your settings.py:
SOCIAL_AUTH_USER_MODEL = 'myapp.CustomUser'
In your case it will be:
SOCIAL_AUTH_USER_MODEL = 'myapp.Profile'
The docs I found this on are here:
http://django-social-auth.readthedocs.org/en/latest/configuration.html#custom-user-model
They also include documentation of ORMs here:
http://django-social-auth.readthedocs.org/en/latest/configuration.html#orms
edit:
By default social-auth uses the
django.contrib.auth.User
model to save users, the documentation can be found here:
https://docs.djangoproject.com/en/dev/ref/contrib/auth/

Device authentication in django

I have a Device model in django and I would like to be able to authenticate it.
class Device(models.Model):
device_key = models.CharField(max_length=100)
udid = models.CharField(max_length=100, unique=True)
To be more specific, given the above Device model, I would like to be able to achieve something similar to TokenAuthentication (http://django-rest-framework.org/api-guide/authentication.html#tokenauthentication).
Sending a request to a login URL with the device_key and udid should return a token, which identifies the Device model, and can be used for further requests.
Note:
Devices are NOT Users. I already use the User model for different purposes.
This question is basically similar to asking how do I authenticate a custom user model in django, which does not have a decent answer either (Django custom User model authentication)
Is there a "django" way to do this?