'I can't distinguish roles with my implementation in DRF+Angular
I've made an implementation of a login system using django as backend and angular as frontend.
In the backend the authentication is implemented via the view associated to auth/signup
from django.contrib import admin
from django.urls import include, path
from rest_framework import routers
from shopping.views import listatodoseventos
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token
urlpatterns = [
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')),
path('events',listatodoseventos),
path('auth/', include('rest_auth.urls')),
path('auth/signup/', include('rest_auth.registration.urls')),
path('auth/refresh-token/', refresh_jwt_token)
]
In the frontend is done by this method
signup(username:string, email:string, password1:string, password2:string){
//todo
return this.http.post(
this.apiRoot.concat('signup/'),
{username, email, password1, password2}
).pipe(
tap(response=>this.setSession(response)),
shareReplay()
);
}
It generates me these tables related with users.
I want to associate each user with a role.
But I don't know how to do that cause I don't know how to create entries in auth_user_groups
Solution 1:[1]
What i did to associate a user with a specific role was to create groups and to send the user with the groups to my angular application.
There are two solution to your problem.
First would be to create groups: Here is how i created a group and added a user to it.
from django.contrib.auth.models import Group,
group, group_has_been_created = Group.objects.get_or_create(name=PRODUCT_MANAGER_GROUP_NAME)
group.user_set.add(product_manager_user)
After having a group and a associated user you can create a response to the angular application.
And another way would be to create profiles and link then to your user.
class UserProfile(models.Model):
user = models.OneToOneField(
User, on_delete=CASCADE, related_name="user_profile", verbose_name=_("user")
)
Here is a good article, which i also used :D. https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html
Hope this helps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | David Marogy |