DEV Community

Bum-Ho12
Bum-Ho12

Posted on

2

Python: creating tokens without the Django auth models

Hi, as a beginner in django_restframework, it is difficult to work with tokens without first creating a BaseUserManager account model and abstract it to be able to access the AuthToken functionality of restframework. I mean this way:

BaseUserManager model code implementation
Basically, you can create tokens for your normal model, but it requires a bit of work. I mean if your model is this way:

simple account model
all you have to do is:

Create a new python file in your app and give it a name. The file is for handling tokens, mine i have called it tokenizer.py.

token Model code

Create another file handling the fetching of tokens, I have named mine token_getter.py

code that gets tokens from the AuthToken model

Now, create a third file that handles the token validation, name it whatever you think is best for your taste. I just happened to call mine auth_validity.py

code that describe token validation
In your views.py,
import
from .auth_validity import is_authenticated
from .token_getter import get_token
and
from .tokenizer import AuthToken
now you can use it like this while you create a user and assign token
AuthToken.objects.create(user=account)
obj = Account.objects.get(email_address = account.email_address)
obj.token = AuthToken.objects.get(user=obj).key
obj.save()

or do this for validation
if is_authenticated(request):
# content

or do this to login
account = Account.objects.get(token = get_token(request))
Well, i don't know how secure this implementation is, but it is easier if you want to avoid using AbstractBaseUser and BaseUserManager so as to access token authentication in django restframework.
Hope this is helpful to someone out there looking for an easier way out without adding extra packages or having to use BaseUserManager.

💡 One last tip before you go

Tired of spending so much on your side projects? 😒

We have created a membership program that helps cap your costs so you can build and experiment for less. And we currently have early-bird pricing which makes it an even better value! 🐥

Just one of many great perks of being part of the network ❤️

Top comments (1)

Collapse
 
dumebii profile image
Dumebi Okolo

Thanks for sharing!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay