In this section of the course we're going to be adding login functionality
to our profiles API the type of authentication we're going to use is
called token authentication it works by generating a token which is like a
random string when you log in and then every request you make to the API that
you wish to authenticate you include this token in the headers I'm going to
be showing you how you do this in action in future videos but first we need to
add an endpoint to our API that allows you to generate an authentication token
this is effectively our login API
fortunately the Django rest framework
comes with an auth token view out of the box that makes it really easy to add a
login API to our profiles API
so what we're going to do is load up the atom
editor and make sure you have our project open then in the view dot py
file we're going to add a new import to the top called obtain auth token this is a
view that comes with the Django rest framework that we can use to generate an
auth token so underneath the last import of the rest framework let's type from
rest framework dot auth token dot views import obtain auth token
then below that we're
going to import the API settings and I'm going to explain why we're going to do this
in a moment so type from rest framework dot Settings import API underscore
settings
Ok then scroll to the bottom of the file and we're going to add a new class
called the user login API view so type class user login API view and we're
going to base our class from the obtain auth token class that we added just now
then add the doc string handle creating user authentication
tokens so this obtain auth token class that is
provided by the Django rest framework is really handy and you could just add it
directly to a URL in the URLs dot py file however it doesn't by default
enable itself in the browsable Django admin site so we need to override this
class and customize it so it's visible in the browsable api so it makes it
easier for us to test so what we need to do is add a class variable here called
renderer classes underscore classes equals and then we're going to import
the default renderer class from the API settings so that's why we needed to
import the API settings at the top of the file so set the renderer classes to API
underscore settings dot default underscore renderer underscore classes
what this does is it adds the renderer classes to our obtain auth token view
which will enable it in the Django admin the rest of the view sets and things
other things that are provided by the Django admin have this by default but the
obtain auth token doesn't have it by default so we need to add it manually
okay so now save that views dot py file and then we're going to add a URL to
this view to enable it so open up the urls dot py file in the profiles API
module then in the URLs pattern we're going to add a new URL in between the hello view
and this empty one here and we're going to type path login forward slash and we're
going to map this to views dot user login API view dot as view
then add a comma at
the end and save the file and that's how you enable the login endpoint on the
Django rest framework