Laravel uses social networking for GitHub login authentication

Source: Internet
Author: User
Tags auth


1. Introduction

In addition to allowing users to perform login authentication through the registration process, many websites also provide third-party login authentication using social network accounts in today's increasingly developed social networks. The powerful Laravel also provides the official package Societe, which currently supports multiple types of social media logins, including Facebook, Twitter, Google, LinkedIn, GitHub, and Bitbucket. Due to GFW, this section briefly demonstrates how to use GitHub for third-party login authentication through Societe.

2. Install the social networking package

To use social networks, run the following command in the root directory of the Laravel application to load the dependency package:

Composer require laravel/Societe

After the download is complete, we need to perform a series of configurations.

3. Related configuration

First, register SocialiteServiceProvider in config/app. php to providers:

Laravel \ social networking \ SocialiteServiceProvider: class,

Add the Societe facade to aliases for ease of use:

'Sociate' => Laravel \ sociate\ Facades \ sociate:: class,

Then add the github configuration information in config/services. php:

'Github '=> [
'Client _ id' => 'Your github app client_id ',
'Client _ secret' => 'Your github app client_secret ',
'Redirect' => 'http: // laravel. app: 8000/auth/github/callback'
]

Change client_id and client_secret to the client_id and client_secret corresponding to your github application. If not, create an application on GitHub. The specific creation method is as follows:

Create a GitHub application

Visit https://github.com/settings/ss, click "register new application", fill in the form information, and submit:

After the creation is successful, the following information is displayed:

Click the application name (LaravelApp in this example) to view the application details to obtain the Client ID and Client Secret information of the application.

4. Modify AuthController

After configuring services. php, we need to modify AuthController as follows:

<? Php

Namespace App \ Http \ Controllers;

Use social networks;
Use App \ User;
Use Illuminate \ Routing \ Controller;

Class AuthController extends Controller {
/**
* Redirect users to the GitHub authentication page
*/
Public function redirectToProvider ()
    {
Return Societe: driver ('github ')-> redirect ();
    }

/**
* Obtain authentication user information from GitHub
*/
Public function handleProviderCallback ()
    {
$ User = Societe: driver ('github ')-> user ();
Dd ($ user );
    }
}

5. Register the authentication route

Finally, we registered the route for GitHub login authentication in routes. php:

Route: get ('auth/github ', 'auth \ AuthController @ redirecttoprovider ');
Route: get ('auth/github/callback', 'auth \ AuthController @ handleprovidercallback ');

So far, we have completed all the configurations and modifications and can test them in the browser.

6. Use GitHub for logon authentication
Visit http://laravel.app: 8000/auth/github in the address bar of the browser and the page will jump to the GitHub authorization page:

Click the green authorization button, the page will jump to the callback page http://laravel.app: 8000/auth/github/callback, and output the user information, indicating that the login is successful!

7. Save user information to the local database

Social networking does not save user information to the database by default. You can choose to save GitHub user information or bind GitHub account and local user. Here we configure to save the GitHub user information to the local database. Therefore, we add two fields to the users Table: github_id and avatar. At the same time, modify the handleProviderCallback method of AuthController as follows:

Public function handleProviderCallback ()
{

$ User = Societe: driver ('github ')-> user ();
   
If (! User: where ('github _ id', $ user-> id)-> first ()){
$ UserModel = new User;
$ UserModel-> github_id = $ user-> id;
$ UserModel-> email = $ user-> email;
$ UserModel-> name = $ user-> name;
$ UserModel-> avatar = $ user-> avatar;
$ UserModel-> save ();
    }

$ UserInstance = User: where ('github _ id', $ user-> id)-> firstOrFail ();
Auth: login ($ userInstance );
Echo $ user-> name. 'login successful! ';
}
In this way, the user information will be saved to the local database after the callback URL is successfully redirected to the GitHub authorization.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.