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
We're going to set up token based authentication for our application. The application asks to share specific information between the sites. When the user chooses to grant permission, a token is stored on their machine. This will allow users to connect their GitHub account to our application.
MAKE SURE that the $appUrl does NOT end with a slash
More About Sessions
Sessions are a way to make data accessible across your entire website. A session creates a file in a temporary directory on your websites web server. This data will be available to all pages on the site during that visit.
When a session is started following things happen:
PHP first creates a unique identifier for that particular session which is a random string of 32 hexadecimal numbers such as 4j7foj34c3jc373hjkop2fc937e3253.
A cookie called PHPSESSID is automatically sent to the user's computer to store unique session identification string.
A file is automatically created on the server in the designated temporary directory and bears the name of the unique identifier prefixed by sess_ ie sess_34j7foj34c3jc373hjkop2fc937e3253.
When a PHP script wants to retrieve the value from a session variable, PHP automatically gets the unique session identifier string from the PHPSESSID cookie and then looks in its temporary directory for the file bearing that name and a validation can be done by comparing both values.
A session ends when the user closes the browser. The server will also terminate the session after a predetermined period of time, commonly 30 minutes.
Documentation
Why would I want a GitHub account?
Because GitHub is the social network of developers. Having started as a developer’s collaborative platform, GitHub is now the largest online storage space of collaborative works that exists in the world. It's the place where you will start contributing to open source, sharing projects and allowing others to see your work. If you are looking to build a career as a developer, GitHub can be an extremely important resource.
What happens if a user does NOT grant permission to share data?
They can either choose the back button or "Visit application’s website" which takes them to the $appUrl you entered. If a user does not grant permission, they only have access to the home page. We could allow users, who are not authenticated, to search and view repositories without the ability to watch/un-watch.
Removing the Token
The token is stored in session storage. If you need to remove the token for any reason, you can drop it by Login::dropToken() or $login->dropToken()
-
0:00
We're going to be setting up token based authentication for our application.
-
0:04
This will allow users to connect their GitHub account.
-
0:07
We need to provide authentication so that a user can watch and unwatch repose.
-
0:13
The first step, is to authorize our application with GitHub.
-
0:16
To do this, you'll need a GitHub account and the applications URL.
-
0:21
If you don't have a GitHub account, make sure you register now.
-
0:25
To get the applications URL, we'll preview our site in a browser and
-
0:30
pull the URL associated with that workspace.
-
0:33
Copy the URL and open GitHub.
-
0:37
Go to Settings and then Developer settings OAuth applications.
-
0:44
Register a new application.
-
0:46
For the homepage URL, paste in the URL from the workspace making sure
-
0:52
that it starts with HTTP and does not end with a forward slash.
-
0:57
Then in the authorization callback URL,
-
1:02
we'll use the same URL followed by inc/authenticate.php.
-
1:10
You can use whatever you want in the application name.
-
1:20
After registering your application, you'll be given a client ID and
-
1:23
client secret that will be using for our application.
-
1:27
Let's go back to work spaces and create our authenticate file.
-
1:36
New folder inc and a new file authenticate.php.
-
1:48
We start by requiring our autoload file.
-
1:50
We're going to be using this authenticate file as both a standalone and
-
1:54
an include file.
-
1:55
So we use the require_once.
-
2:06
We're going to store our token in a session.
-
2:09
So, we need to start a session.
-
2:13
Then we need three pieces of information to connect our app to GitHub.
-
2:18
The appUrl.
-
2:23
Which is the same workspace URL we pasted into GitHub.
-
2:30
The clientId, And the clientSecret.
-
2:39
These are the pieces of information we've received from GitHub.
-
2:55
Now we can set up the objects that we'll need.
-
2:59
We'll start with config.
-
3:07
OAuth\Configuration.
-
3:11
And we'll pass the clientId and the clientSecret.
-
3:18
We also passed the array of permissions that we need.
-
3:24
User and repo.
-
3:28
Next, we set up storage to use session storage.
-
3:41
Then we can add the log in.
-
3:54
We pass the config and the storage.
-
4:00
And finally we can add the API object.
-
4:10
Now we can use those objects to get and set a token.
-
4:15
If login hasToken
-
4:22
Then the token will equal login, getToken.
-
4:31
And then we can add that to our API.
-
4:34
setToken equal to token.
-
4:42
Else, we're going to direct the user to authenticate with GitHub.
-
4:46
From there they'll be redirected directly to this script.
-
4:50
We'll use a GIT variable to know which point in the transaction we are and
-
4:54
to know which page to show them after they authenticate.
-
5:00
If, isset, GET redirect.
-
5:10
At this point we're ready to obtain the token.
-
5:15
Login, obtain token.
-
5:21
And we'll get a code.
-
5:27
And a state, that GitHub passes.
-
5:51
Else.
-
5:53
We need to ask permissions.
-
5:56
Log in.
-
5:58
Ask permissions.
-
6:03
We passed the appUrl/inc/authenticate.php and
-
6:10
then the redirect.
-
6:16
We want to redirect to the page that they were on before they were asked to
-
6:20
authenticate.
-
6:22
We do this using SERVER('REQUEST_URI').
-
6:37
Let's add this file to our search page.
-
6:51
We also need to remove the API initialization since our authenticate file
-
6:55
is handling that now.
-
6:59
We're ready to give this a try in the browser.
-
7:08
We're asked to authorize the application to connect to GitHub.
-
7:15
Once we approve the authorization, we're redirected to our application.
-
7:20
Great.
-
7:21
Now we can add our final feature that requires permission from the user.
You need to sign up for Treehouse in order to download course files.
Sign up