WEBVTT
Kind: captions
Language: en

00:00:00.030 --> 00:00:04.350
In this section of the course we're
going to be adding login functionality

00:00:04.350 --> 00:00:10.349
to our profiles API the type of
authentication we're going to use is

00:00:10.349 --> 00:00:17.190
called token authentication it works by
generating a token which is like a

00:00:17.190 --> 00:00:23.400
random string when you log in and then
every request you make to the API that

00:00:23.400 --> 00:00:28.529
you wish to authenticate you include
this token in the headers I'm going to

00:00:28.529 --> 00:00:34.079
be showing you how you do this in action
in future videos but first we need to

00:00:34.079 --> 00:00:39.480
add an endpoint to our API that allows
you to generate an authentication token

00:00:39.480 --> 00:00:44.160
this is effectively our login API

00:00:44.160 --> 00:00:45.870
fortunately the Django rest framework

00:00:45.870 --> 00:00:51.690
comes with an auth token view out of the
box that makes it really easy to add a

00:00:51.690 --> 00:00:56.120
login API to our profiles API

00:00:56.120 --> 00:00:58.800
so what we're going to do is load up the atom

00:00:58.800 --> 00:01:04.019
editor and make sure you have our
project open then in the view dot py

00:01:04.019 --> 00:01:10.830
file we're going to add a new import to the
top called obtain auth token this is a

00:01:10.830 --> 00:01:15.360
view that comes with the Django rest
framework that we can use to generate an

00:01:15.360 --> 00:01:21.619
auth token so underneath the last import
of the rest framework let's type from

00:01:21.620 --> 00:01:32.760
rest framework dot auth token dot views import
obtain auth token

00:01:32.760 --> 00:01:33.680
then below that we're

00:01:33.689 --> 00:01:37.860
going to import the API settings and I'm
going to explain why we're going to do this

00:01:37.860 --> 00:01:47.280
in a moment so type from rest framework
dot Settings import API underscore

00:01:47.280 --> 00:01:50.240
settings

00:01:50.240 --> 00:01:55.200
Ok then scroll to the bottom of
the file and we're going to add a new class

00:01:55.200 --> 00:02:04.590
called the user login API view so type
class user login API view and we're

00:02:04.590 --> 00:02:11.540
going to base our class from the obtain
auth token class that we added just now

00:02:11.540 --> 00:02:17.629
then add the doc string
handle creating user authentication

00:02:17.629 --> 00:02:23.129
tokens
so this obtain auth token class that is

00:02:23.129 --> 00:02:27.840
provided by the Django rest framework is
really handy and you could just add it

00:02:27.840 --> 00:02:34.200
directly to a URL in the URLs dot py
file however it doesn't by default

00:02:34.200 --> 00:02:41.099
enable itself in the browsable Django
admin site so we need to override this

00:02:41.099 --> 00:02:46.140
class and customize it so it's visible
in the browsable api so it makes it

00:02:46.140 --> 00:02:51.950
easier for us to test so what we need to
do is add a class variable here called

00:02:51.950 --> 00:02:59.430
renderer classes underscore classes
equals and then we're going to import

00:02:59.430 --> 00:03:05.040
the default renderer class from the API
settings so that's why we needed to

00:03:05.040 --> 00:03:11.129
import the API settings at the top of the
file so set the renderer classes to API

00:03:11.129 --> 00:03:18.709
underscore settings dot default
underscore renderer underscore classes

00:03:18.709 --> 00:03:26.190
what this does is it adds the renderer
classes to our obtain auth token view

00:03:26.190 --> 00:03:31.380
which will enable it in the Django admin
the rest of the view sets and things

00:03:31.380 --> 00:03:35.099
other things that are provided by the Django
admin have this by default but the

00:03:35.099 --> 00:03:40.579
obtain auth token doesn't have it by
default so we need to add it manually

00:03:40.579 --> 00:03:47.370
okay so now save that views dot py file
and then we're going to add a URL to

00:03:47.370 --> 00:03:54.000
this view to enable it so open up the
urls dot py file in the profiles API

00:03:54.000 --> 00:04:00.720
module then in the URLs pattern we're going to add a new URL in between the hello view

00:04:00.720 --> 00:04:10.470
and this empty one here and we're going to
type path login forward slash and we're

00:04:10.470 --> 00:04:21.640
going to map this to views dot user login
API view dot as view

00:04:21.640 --> 00:04:23.360
then add a comma at

00:04:23.370 --> 00:04:30.520
the end and save the file and that's how
you enable the login endpoint on the

00:04:30.520 --> 00:04:33.720
Django rest framework

