vue-social-auth is an easily configurable solution for Vue.js & Laravel with Socialite that provides Social login using Github, Facebook, Google, and other OAuth providers.
The best part about this library is that it is not strictly coupled to one request handling library like vue-axios. You will be able to use it with different libraries.
For now, it is tested to work with vue-resource and axios (using vue-axios wrapper).
WARNING: Default request library is axios
using vue-axios
wrapper plugin.
NOTE: It also works with any Php with Socialite
Supported OAuth providers and configurations
Installation
firstly install Socialite
NOTE: make sure you config your Socialite
configuration data in services.php
& .env
for more details check https://socialiteproviders.netlify.com/
composer require laravel/socialite
Next install vue-social-auth
npm install vue-social-auth
Usage
import Vue from 'vue'
import VueAxios from 'vue-axios'
import VueSocialauth from 'vue-social-auth'
import axios from 'axios';Vue.use(VueAxios, axios)
Vue.use(VueSocialauth, { providers: {
github: {
clientId: '',
redirectUri: '/auth/github/callback' // Your client app URL
}
}
})<button @click="AuthProvider('github')">auth Github</button>
<button @click="AuthProvider('facebook')">auth Facebook</button>
<button @click="AuthProvider('google')">auth Google</button>
<button @click="AuthProvider('twitter')">auth Twitter</button>
Vew Component
<script> export default { data(){ return { } },
methods: {
AuthProvider(provider) {
var self = this
this.$auth.authenticate(provider).then(response =>{
self.SocialLogin(provider,response) }).catch(err => {
console.log({err:err})
}) },
SocialLogin(provider,response){ this.$http.post('/sociallogin/'+provider,response).then(response => { console.log(response.data) }).catch(err => {
console.log({err:err})
})
},
}
}
</script>
Vue Router
{
path: '/auth/:provider/callback',
component: {
template: '<div class="auth-component"></div>'
}
},
Vue is Done let move to backend config Laravel
with Socialite
Laravel Router
Route::post('sociallogin/{provider}', 'Auth\AuthController@SocialSignup');
Route::get('auth/{provider}/callback', 'OutController@index')->where('provider', '.*');
OutController
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class OutController extends Controller
{
public function __construct()
{ }
public function index()
{ return view('welcome'); }
}
Auth\AuthController
<?phpnamespace App\Http\Controllers\Auth;use App\Http\Controllers\Controller;
use Illuminate\Http\Request;use Socialite;class AuthController extends Controller
{
public function __construct()
{ }
public function SocialSignup($provider)
{
// Socialite will pick response data automatic
$user = Socialite::driver($provider)->stateless()->user(); return response()->json($user);
}}
services.php
<?phpreturn [ // ..... 'twitter' => [
'client_id' => env('TWITTER_ID'),
'client_secret' => env('TWITTER_SECRET'),
'redirect' => env('TWITTER_URL'),
], 'facebook' => [
'client_id' => env('FACEBOOK_ID'),
'client_secret' => env('FACEBOOK_SECRET'),
'redirect' => env('FACEBOOK_URL'),
], 'github' => [
'client_id' => env('GITHUB_ID'),
'client_secret' => env('GITHUB_SECRET'),
'redirect' => env('GITHUB_URL'),
], 'google' => [
'client_id' => env('GOOGLE_ID'),
'client_secret' => env('GOOGLE_SECRET'),
'redirect' => env('GOOGLE_URL'),
],];
.env
TWITTER_ID=Your ID
TWITTER_SECRET=Your Secret
TWITTER_URL=https://example.com/auth/twitter/callbackFACEBOOK_ID=Your ID
FACEBOOK_SECRET=Your Secret
FACEBOOK_URL=https://example.com/auth/facebook/callbackGITHUB_ID=Your ID
GITHUB_SECRET=Your Secret
GITHUB_URL=https://example.com/auth/github/callbackGOOGLE_ID=Your ID
GOOGLE_SECRET=Your Secret
GOOGLE_URL=https://example.com/auth/google/callback
VerifyCsrfToken Middleware
you may need to disable Csrf for the route if you receive Error: Request failed with status code 419
<?phpnamespace App\Http\Middleware;use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [ '/sociallogin/google','/sociallogin/facebook','/sociallogin/github','/sociallogin/twitter'
];
}
if any issue check
