Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
With the basic setup in place, it's time to customize how our users sign up. We'll require an email address, require the email to be validated before they can sign in, and we'll customize a few templates to make things work exactly how we want.
-
0:00
First I wanna setup the right links in the layout template.
-
0:03
This is the one that, if you remember are using this for
-
0:05
all of the layout in the site.
-
0:08
I need to import the social account template tag library.
-
0:13
So, import social account and then down here I want to change the login stuff.
-
0:20
So right now there is this login link and there is a signup link.
-
0:26
Now, I don't need both of those because we're going to have one workflow where
-
0:30
you click a button and you're logged in.
-
0:32
So I'm going to delete the sign up one and I'm gonna change this one,
-
0:38
instead of it being url login, it's going to be provider login url for github.
-
0:45
So this tag comes from the social account set of tags.
-
0:48
And I'm also gonna change it so instead of saying login it's gonna say your account.
-
0:55
All right.
-
0:56
I want to do the same thing in index.html.
-
0:58
Might have all these buttons for sign up here and
-
1:01
I want to change these to be provider_login_url 'github' or
-
1:06
at least I'll do the first one you can do all of them if you want.
-
1:09
Actually that would take too long,
-
1:16
so copy that, paste it there,
-
1:21
paste it there, paste it there,
-
1:26
and paste it there.
-
1:30
And I will also need to do the import up here of load social account.
-
1:38
Okay, now if I open up the home page, let me refresh this.
-
1:47
Now that should be load socialaccount,
-
1:52
not import socialaccount.
-
1:58
There we go.
-
1:59
So now I have the signup link, I have the your account link, and
-
2:01
I can see that they are going to the GitHub location.
-
2:04
If I click the link, this asked me if I want to authorize the application and
-
2:10
I'll say authorize, come right back, and I can see that I'm signed up.
-
2:14
So it worked, and now if I look at this I can see that it sent me an email.
-
2:19
So that's pretty cool.
-
2:20
Let's see if it did send an email.
-
2:24
So yep, here is this email, user Kenneth has given
-
2:28
yours is an email address to connect their account to confirm, go to this link.
-
2:32
All right, so if we're gonna send an email with a verification link,
-
2:36
I think that we should require that before they can get into their account,
-
2:39
they have to click that link.
-
2:41
So it's time to do a couple of settings changes.
-
2:45
So I'm gonna again go back to msg settings.
-
2:48
And again, down here at the bottom I'm gonna add a couple of things.
-
2:53
So the first thing I'm gonna add is the setting that is ACCOUNT_EMAIL_REQUIRED and
-
2:57
I am going to set that equal to true.
-
3:00
And what this requires is that they give us their email address when they sign up,
-
3:04
so that when we have the email address which we have to have in order to send
-
3:07
them an email, right?
-
3:08
So then I'm gonna set ACCOUNT_EMAIL_VERIFICATION
-
3:13
equal to true, and
-
3:14
that means that we require the email to be verified before their account is active.
-
3:21
And the last thing I want to add in here is ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION and
-
3:26
I am going to set that to true as well.
-
3:30
And normally django-allauth sends you the email you click the link saying
-
3:35
that it's you and then you're taken to a page that has you login.
-
3:39
I don't wanna have that second log in pages that seems silly.
-
3:42
So I'm just gonna log the them right away and that's what the setting does.
-
3:46
Okay, so we've added two more steps to the sign up flow.
-
3:49
They have to check their email for the confirmation link.
-
3:51
Then they had to verify that they really did click the link and
-
3:54
these views are already provided by django-allauth but
-
3:57
they used two templates that we need to create to match our style.
-
4:01
So the first thing I need to do is I need to create a new folder inside templates,
-
4:06
that's called account.
-
4:08
That's where the templates exist.
-
4:10
And then I will make a new file inside of account which is, email.confirm.html.
-
4:16
So I'm gonna copy and paste this code in, and
-
4:20
then we will talk about what is going on in here.
-
4:24
So, this is a pretty similar form to a lot of other forms that we used
-
4:28
in Django authentication.
-
4:30
The difference thing is we're loading account and
-
4:32
social account libraries up at the top.
-
4:34
Confirmation is, they get the email confirmation and then they click the link,
-
4:41
they click this link here and this link creates what's called a confirmation.
-
4:46
That confirmation links to the email stuff that was sent out,
-
4:50
which links to the email, and the user, and all this stuff.
-
4:53
So, this is going to show the email address, and
-
4:57
this is going to show the username.
-
5:01
And they're just making sure it's really them.
-
5:03
This is the form where they'll click, yes this is me.
-
5:06
They'll confirm it.
-
5:07
And if the confirmation doesn't get created because it's been too long,
-
5:11
it's expired.
-
5:12
Then we'll see a message that says, the confirmation is expired.
-
5:16
Go click this link to get a new confirmation email.
-
5:20
So pretty straightforward.
-
5:21
You've probably seen this on other sites.
-
5:24
The next one that we need to create is verification_sent.html.
-
5:31
So this one actually comes before the one that we just looked at.
-
5:35
This one is the one that is shown after they've clicked the signup button.
-
5:38
And then we're telling them hey we sent you an email,
-
5:41
you need to go check your email.
-
5:42
So, you can read this if you want, but there's nothing really different here.
-
5:48
It's all just HTML.
-
5:50
So, I wanna try the new sign up process.
-
5:52
Now the first thing I need to do though,
-
5:54
is I need to delete the accounts that are in here.
-
5:59
So I'm gonna come up here to users, and this is the new user.
-
6:04
So I'm gonna go ahead and delete that user.
-
6:08
And yep, I'm sure, go ahead and delete it.
-
6:10
All right, click log out, just in case, okay.
-
6:15
So I don't have my account,
-
6:16
I'm gonna click your account, which takes me to GitHub.
-
6:19
And now you can see that we have a new part here this added permissions,
-
6:23
the personal user data e-mail address, and
-
6:25
that's because of this setting right here, this e-mail required.
-
6:30
That tells django-allauth to tell GitHub we need their email address which asks for
-
6:35
the email address.
-
6:36
Because normally all we get is,
-
6:37
hey this account wants to use your account as login.
-
6:41
So this says hey we're gonna send them the e-mail address as well.
-
6:45
So we're gonna authorize that application,
-
6:47
and then this redirects back to this page where this is our verify your email,
-
6:52
and it says a link's been sent, you need to check that link and and come back.
-
6:56
So if we look here there's a new email.
-
6:59
And it's got a new URL, so I'm gonna copy this URL, copy,
-
7:04
Cmd+C, and then I'm gonna paste this in.
-
7:08
And now I get the confirm email page.
-
7:12
This is the page that we created that has the form that has,
-
7:16
hey is this email address a valid address for you?
-
7:19
So I'm gonna, yeah it is, and cool so
-
7:22
now I've confirmed my email address and I have successfully signed in as me.
-
7:27
Great.
-
7:29
So our log out is working, but I wanna use the django allauth log out.
-
7:34
So it's a little bit different.
-
7:36
So I'm gonna come back over here to layout.html and
-
7:40
I'm gonna find the logout button, which is right here.
-
7:43
And instead of accounts:logout, the one I wanna use as account_logout.
-
7:48
And this is one that's provided by django-allauth.
-
7:51
And it has an interesting side effect.
-
7:54
Let me show you what it does.
-
7:57
So if I click logout, oops sorry,
-
8:00
I have to do that when it's using the new URL not the old one.
-
8:04
All right so now, Let's refresh.
-
8:31
So I'm going to change this to account logout but that goes
-
8:34
to slash accounts slash log which is why when I click that I got the old one.
-
8:39
I can look at my URL's.
-
8:42
I have accounts up here, right.
-
8:46
So we can actually just turn both of these off, and now if
-
8:53
go look at my account and then I go to log out, this is the new page.
-
8:59
This is the page I was talking about as being weird.
-
9:02
It's different.
-
9:03
So django-allauth made a decision to require post requests for
-
9:08
logging someone out.
-
9:10
And, it seems wrong at first but I actually think this is pretty smart.
-
9:14
It prevents someone from putting your logout URL
-
9:17
into say an image tag's source attribute.
-
9:19
And then any user who loaded that image would be logged out, but
-
9:22
the template here needs some love and attention.
-
9:26
So I'm gonna create, yet one more account or one more account template, rather,
-
9:30
which is logout.html.
-
9:34
And again, I'm gonna paste in some code.
-
9:39
And this should not have anything in it that's surprising to you.
-
9:43
It just has a form that says, Log out.
-
9:47
So if they click the button.
-
9:51
They get this, and it will log them out.
-
9:53
So, this looks a lot better, and
-
9:55
if I click this button I should be logged out, and I am.
-
9:59
I get you have signed out.
-
10:01
So that's how to set up social authentication with a GitHub
-
10:05
inside of Django.
-
10:06
Using social authentication with the responsibility of securing your users
-
10:09
account information, at least the basics, onto a service that you don't control.
-
10:14
This has its own pluses and minuses of course.
-
10:16
If that service goes down or
-
10:17
out of business, your users won't be able to log in to your site and
-
10:21
of course if that site's accounts got hacked, yours technically would be too.
-
10:25
Thankfully at least with major services those two problems are fairly rare
-
10:29
which means your users can log in to your site and.
-
10:31
You can concentrate on new features and bug fixes for
-
10:33
your code you could also use django-allauth in-house services too.
-
10:38
If your site or a service on your network provides allauth capabilities,
-
10:42
you can build your own back-end for django-allauth and
-
10:44
use that to do your account verification, it's a really flexible library.
You need to sign up for Treehouse in order to download course files.
Sign up