11 Social Authentication
11 Social Authentication
In this lecture, we will be studying third-party or social authentication. We all have seen the
buttons like Sign-in using Facebook, log-in using Google or Github, on the various social
platforms. This is known as Social Authentication. Whenever we click on that button it does
two things - It saves a lot of time and user effort.
● These days since people already have accounts on places like Gmail, Facebook,
Github, etc, it is possible to use an authentication service provided by them called
OAuth { Which means their authentication can be used on my platform as well }.
● We will be using Google login in this module but soon you will gain the necessary
understanding to look into other authentication modules.
● We will implement OAuth by using cookies.
● We need to establish the identity and tell Google for security purposes that we are
taking out the information of that user. The user also gives out permission.
http://console.ldeveloper.google.com/
{new project }
● Give the name of your project ( codeial -sample ) and click on {create it}.
1
In OAuth -1, we generate the access token. Once we got it, we kept on using it until and
In OAuth - 2, we get only the desired information, your token has a limited set of
permissions and your token can anytime be revoked by Google but it is much more secure
as compared to OAuth - 1.
● Click on the option to create credentials, there we need to click on OAuth client ID.
Warning - To create an OAuth client ID you must first set up the product name on
● Give your application name there and leave the rest of the fields empty for now and
save it.
● The application type is a web application, the name will be the name of your project.
}[ For call back URL we need to mention it here and in code also ].
2
Social Authentication:: How does it work
● This involves the third-party authentication provider which establishes the identity
of the user.
● There will be a user, third-party authentication provider, and server with the
database.
● The user comes and clicks on the sign-in or sign up with google. A pop-up opens in
another window that asks us to sign in with google if we are not signed in or we
have signed using multiple accounts. For the latter case, we choose from one of
those accounts.
● Google then asks for permission from the user, that ‘project_name is asking for
your permission to access your account’. This happens only if the user is accessing
● When the pop-up opens and we submit some credentials or select an account, the
request is redirected to the servers of Google. Google checks its database whether
the user exists or not. If the user exists, Google checks if and the credentials are
correct or not.
● If the authentication is unsuccessful, you will be redirected back to the login page
with an error.
● Once the credentials are matched successfully, the user will be redirected to
another URL on the same browser for the server which is called using the callback
URL with some data of the signed-in user. This is created by the developer of the
website.
● The server checks with the database whether the Email ID that has been provided
3
● If, however, if the Email ID is not in the database, then OAuth will create the user
credentials in the database, post which the user will be signed in to the website.
● We need to install the crypto library { npm install crypto } and require it inside {
● We need to tell passport to use google strategy using passport.use in which we pass
● We need to put in the callback function which will be taking { access token, refresh
token, profile information, and the final function which will be taking callback from
this function }.
● If we find the user then we will call done without an error and pass the user to it { if
found, set this user as req. user} if the user is not found then create the user { if not
● While creating the user uses a profile, the profile has different fields { display name,
4
crypto.randomBytes(20).toString(‘hex’)
{ passport-google -oauth-2-strategy.js }
Place the sign-in button, make the routes and get it working.
● Inside the routes folder and in {users.js} file we will be creating two routes - { when
we will click on the button for google sign-in for fetching the data when google
5
fetches the data from the database and sends it back to the routes which is the
callback URL }.
● The scope is the information that we are looking to fetch - { profile, email}.
● Until and unless we ask for a refresh token it is not coming up.
{ user.js }
6
{ Pages for sign-in and sign-up }
7
{ Google Auth }