Last active
November 5, 2019 03:39
-
-
Save brickgale/2a4c4b310db7b42b2e43492e9bebb646 to your computer and use it in GitHub Desktop.
Laravel 5.4 with Laravel Socialite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Auth; | |
use JWTAuth; | |
use Socialite; | |
use App\User; | |
use App\Transformers\UserTransformer; | |
use App\Http\Controllers\Controller; | |
use Tymon\JWTAuth\Exceptions\JWTException; | |
use Illuminate\Http\Request; | |
class LoginController extends Controller | |
{ | |
/** | |
* Authenticate User Via Provider | |
* | |
* @param provider | |
* @return Response | |
*/ | |
public function authenticateViaProvider(Request $request) | |
{ | |
$credentials = $request->only('provider', 'id', 'token'); | |
$providerUser = Socialite::driver($credentials['provider']) | |
->userFromToken($credentials['token']); | |
if($providerUser) { | |
$o_user = User::findByProvider($credentials['provider'], $credentials['id'], $credentials['token'])->first(); | |
if($o_user) { | |
$token = JWTAuth::fromUser($o_user); | |
if($o_user->isActivated()) { | |
$user = $this->transformItem($o_user, new UserTransformer); | |
return $this->response->array(compact('token','user'))->setStatusCode(200); | |
} | |
} | |
} | |
return $this->response->errorUnauthorized('Token is not valid'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
class User extends Authenticatable | |
{ | |
use Notifiable; | |
use SoftDeletes; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'first_name', 'last_name', 'email', 'status', 'password', 'facebook_id', 'facebook_token', 'password_reset' | |
]; | |
/** | |
* The attributes that should be hidden for arrays. | |
* | |
* @var array | |
*/ | |
protected $hidden = [ | |
'password', 'remember_token', | |
]; | |
//relationships | |
//scopes | |
public function scopeFindByEmail($query, $email) { | |
return $query->where('email', $email); | |
} | |
public function scopeFindByProvider($query, $provider, $provider_id, $provider_token) { | |
return $query->where($provider.'_id', $provider_id) | |
->where($provider.'_token', $provider_token); | |
} | |
public function scopeFindByProviderId($query, $provider, $provider_id) { | |
return $query->where($provider.'_id', $provider_id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
|-------------------------------------------------------------------------- | |
| Web Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register web routes for your application. These | |
| routes are loaded by the RouteServiceProvider within a group which | |
| contains the "web" middleware group. Now create something great! | |
| | |
*/ | |
Route::get('auth/redirect/{provider}', 'Auth\SocialController@redirectToProvider'); | |
Route::get('auth/handler/{provider}', 'Auth\SocialController@handleProviderCallback'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment