Giter Club home page Giter Club logo

atomjoy / socialites Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 54 KB

How to add Google and Github login button using Socialite in Laravel. Google One Tap with Vue and Laravel Socialite.

Home Page: https://github.com/atomjoy/socialites

PHP 47.77% Blade 52.23%
google-one-tap laravel-github-login laravel-google-login laravel-socialite github-login-button github-login-laravel google-login-button google-login-button-vue3-laravel github-login-button-vue3-laravel

socialites's Introduction

Google and Github Login with Laravel Socialite

How to add Google and Github login using Socialite in Laravel. Google One Tap with Vue and Laravel Socialite.

Install

Add package and routes.

composer require "atomjoy/socialites"

Callbacks and local domain

Change the callback domain, set the local domain to example.org and add ssl.

# Google callback
https://example.org/oauth/google/callback

# Github callback
https://example.org/oauth/github/callback

# How to set local domain and SSL for example.org (xampp)
https://github.com/atomjoy/xampp

Create Google project

https://console.cloud.google.com/projectcreate

Create Google OAuth consent screen

Create a consent screen for an app with permissions to:

  • auth/userinfo.email
  • auth/userinfo.profile
  • openid

Create Google oauth keys

Create External OAuth 2.0 client IDs add callback and retrieve keys

https://console.cloud.google.com/apis/credentials

Create a new oauth app on Github and get the keys

https://github.com/settings/developers

Setings

Update the .env file in the callback links, just change the domain.

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URL=https://example.org/oauth/google/callback
GOOGLE_HOME_URL=/

GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GITHUB_REDIRECT_URL=https://example.org/oauth/github/callback
GITHUB_HOME_URL=/

Config service

Append in config/services.php

<?php

return [
    'google' => [
        'client_id' => env('GOOGLE_CLIENT_ID'),
        'client_secret' => env('GOOGLE_CLIENT_SECRET'),
        'redirect' => env('GOOGLE_REDIRECT_URL'),
        'homepage' => env('GOOGLE_HOME_URL'),
    ],

    'github' => [
        'client_id' => env('GITHUB_CLIENT_ID'),
        'client_secret' => env('GITHUB_CLIENT_SECRET'),
        'redirect' => env('GITHUB_REDIRECT_URL'),
        'homepage' => env('GITHUB_HOME_URL'),
    ],

    'oauth_drivers' => ['github', 'google']
];

Login buttons

return view('socialites::login-buttons');

@if (Auth::check())
    <div>{{ Auth::user()->name }}</div>
    <a href="/oauth/logout" title="Logout">{{ trans('Logout') }}</a>
@else
    <a href="/oauth/google/redirect" title="Google">{{ trans('Login with Google') }}</a>
    <a href="/oauth/github/redirect" title="Github">{{ trans('Login with Github') }}</a>
@endif

Javascript Google One Tap dialog and button (optional)

return view('socialites::login-onetap');

@if (!Auth::check())
<div id="buttonDiv"></div>
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script>
    function handleCredentialResponse(response) {
        window.location.href = '/oauth/google/redirect'
        // Here we can do whatever process with the response we want (optional)
        // Note that response.credential is a JWT ID token
        // console.log("Encoded JWT ID token: " + response.credential);
        // fetch('/oauth/google/oauth?token=' + response.credential)
    }

    window.onload = function () {
        google.accounts.id.initialize({
            client_id: "{{ config('services.google.client_id') }}", // Or replace with your Google Client ID
            callback: handleCredentialResponse // We choose to handle the callback in client side, so we include a reference to a function that will handle the response
        });
        // Show "Sign-in" button (optional)
        // google.accounts.id.renderButton(document.getElementById("buttonDiv"),{ theme: "outline", size: "small" });
        // Display the One Tap dialog
        google.accounts.id.prompt();
        // Hide One Tap onclick
        const button = document.querySelector('body');
        button.onclick = () => {
            google.accounts.id.disableAutoSelect();
            google.accounts.id.cancel();
        }
    }
</script>
@endif

Run server

php artisan serve --host=localhost --port=8000

Publish assets

# Copy Icons
php artisan vendor:publish --tag=socialites-assets --force

# Edit Views
php artisan vendor:publish --tag=socialites-views --force

# Create config example config/socialites.php
php artisan vendor:publish --tag=socialites-config --force

Events

<?php

use Atomjoy\Socialites\Events\UserLogged;
use Atomjoy\Socialites\Events\UserCreated;

Add more drivers in the configuration file

Append in config/services.php

<?php

return [
    //...

    'facebook' => [
        'client_id' => env('FACEBOOK_CLIENT_ID'),
        'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
        'redirect' => env('FACEBOOK_REDIRECT_URL'),
        'homepage' => env('FACEBOOK_HOME_URL'),
    ],

    'oauth_drivers' => ['github', 'google', 'facebook'],
];

LICENSE

This project is licensed under the terms of the GNU GPLv3 license.

socialites's People

Contributors

atomjoy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.