<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bum-Ho12</title>
    <description>The latest articles on DEV Community by Bum-Ho12 (@bumho12).</description>
    <link>https://dev.to/bumho12</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1178590%2Fb8238bf5-cee7-4b82-944c-1458d9b03a4f.jpeg</url>
      <title>DEV Community: Bum-Ho12</title>
      <link>https://dev.to/bumho12</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bumho12"/>
    <language>en</language>
    <item>
      <title>Python: creating tokens without the Django auth models</title>
      <dc:creator>Bum-Ho12</dc:creator>
      <pubDate>Wed, 18 Oct 2023 16:24:33 +0000</pubDate>
      <link>https://dev.to/bumho12/creating-tokens-without-the-django-user-model-3fmj</link>
      <guid>https://dev.to/bumho12/creating-tokens-without-the-django-user-model-3fmj</guid>
      <description>&lt;p&gt;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:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdaftzl2rvwrqxt06add6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdaftzl2rvwrqxt06add6.png" alt="BaseUserManager model code implementation" width="800" height="681"&gt;&lt;/a&gt;&lt;br&gt;
Basically, you can create tokens for your normal model, but it requires a bit of work. I mean if your model is this way:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjbofz4l3k4mwjly7puud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjbofz4l3k4mwjly7puud.png" alt="simple account model" width="800" height="317"&gt;&lt;/a&gt;&lt;br&gt;
all you have to do is:&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vhvlmsl2nijqvdcwixs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9vhvlmsl2nijqvdcwixs.png" alt="token Model code" width="800" height="627"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create another file handling the fetching of tokens, I have named mine token_getter.py&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm698n0l83dr90ps98q93.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm698n0l83dr90ps98q93.png" alt="code that gets tokens from the AuthToken model" width="800" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fai9ouz5yqtn4q7nzkf7n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fai9ouz5yqtn4q7nzkf7n.png" alt="code that describe token validation" width="505" height="298"&gt;&lt;/a&gt;&lt;br&gt;
In your views.py,&lt;br&gt;
import&lt;br&gt;
&lt;code&gt;from .auth_validity import is_authenticated&lt;/code&gt;&lt;br&gt;
&lt;code&gt;from .token_getter import get_token&lt;/code&gt;&lt;br&gt;
and&lt;br&gt;
&lt;code&gt;from .tokenizer import AuthToken&lt;/code&gt;&lt;br&gt;
now you can use it like this while you create a user and assign token&lt;br&gt;
&lt;code&gt;AuthToken.objects.create(user=account)&lt;br&gt;
            obj                 = Account.objects.get(email_address = account.email_address)&lt;br&gt;
            obj.token           = AuthToken.objects.get(user=obj).key&lt;br&gt;
            obj.save()&lt;/code&gt;&lt;br&gt;
or do this for validation&lt;br&gt;
&lt;code&gt;if is_authenticated(request):&lt;br&gt;
        # content&lt;/code&gt;&lt;br&gt;
or do this to login&lt;br&gt;
&lt;code&gt;account             = Account.objects.get(token = get_token(request))&lt;/code&gt;&lt;br&gt;
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.&lt;br&gt;
Hope this is helpful to someone out there looking for an easier way out without adding extra packages or having to use BaseUserManager.&lt;/p&gt;

</description>
      <category>django</category>
      <category>token</category>
      <category>baseusermanager</category>
      <category>abstractbaseuser</category>
    </item>
  </channel>
</rss>
