forked from BakayYank/laravel-nuxt-vuetify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
655 additions
and
144 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/node_modules | ||
/public/hot | ||
/log | ||
/public/storage | ||
/storage/*.key | ||
/vendor | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Exceptions; | ||
|
||
use Exception; | ||
|
||
class EmailTakenException extends Exception | ||
{ | ||
/** | ||
* Render the exception as an HTTP response. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function render($request) | ||
{ | ||
return response()->view('oauth.emailTaken', [], 400); | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use App\User; | ||
use App\OAuthProvider; | ||
use App\Http\Controllers\Controller; | ||
use App\Exceptions\EmailTakenException; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use Illuminate\Foundation\Auth\AuthenticatesUsers; | ||
|
||
class OAuthController extends Controller | ||
{ | ||
use AuthenticatesUsers; | ||
|
||
/** | ||
* Create a new controller instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
config([ | ||
'services.github.redirect' => route('oauth.callback', 'github'), | ||
]); | ||
} | ||
|
||
/** | ||
* Redirect the user to the provider authentication page. | ||
* | ||
* @param string $provider | ||
* @return \Illuminate\Http\RedirectResponse | ||
*/ | ||
public function redirectToProvider($provider) | ||
{ | ||
return [ | ||
'url' => Socialite::driver($provider)->stateless()->redirect()->getTargetUrl(), | ||
]; | ||
} | ||
|
||
/** | ||
* Obtain the user information from the provider. | ||
* | ||
* @param string $driver | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function handleProviderCallback($provider) | ||
{ | ||
$user = Socialite::driver($provider)->stateless()->user(); | ||
$user = $this->findOrCreateUser($provider, $user); | ||
|
||
$this->guard()->setToken( | ||
$token = $this->guard()->login($user) | ||
); | ||
|
||
return view('oauth/callback', [ | ||
'token' => $token, | ||
'token_type' => 'bearer', | ||
'expires_in' => $this->guard()->getPayload()->get('exp') - time(), | ||
]); | ||
} | ||
|
||
/** | ||
* @param string $provider | ||
* @param \Laravel\Socialite\Contracts\User $sUser | ||
* @return \App\User|false | ||
*/ | ||
protected function findOrCreateUser($provider, $user) | ||
{ | ||
$oauthProvider = OAuthProvider::where('provider', $provider) | ||
->where('provider_user_id', $user->getId()) | ||
->first(); | ||
|
||
if ($oauthProvider) { | ||
$oauthProvider->update([ | ||
'access_token' => $user->token, | ||
'refresh_token' => $user->refreshToken, | ||
]); | ||
|
||
return $oauthProvider->user; | ||
} | ||
|
||
if (User::where('email', $user->getEmail())->exists()) { | ||
throw new EmailTakenException; | ||
} | ||
|
||
return $this->createUser($provider, $user); | ||
} | ||
|
||
/** | ||
* @param string $provider | ||
* @param \Laravel\Socialite\Contracts\User $sUser | ||
* @return \App\User | ||
*/ | ||
protected function createUser($provider, $sUser) | ||
{ | ||
$user = User::create([ | ||
'name' => $sUser->getName(), | ||
'email' => $sUser->getEmail(), | ||
]); | ||
|
||
$user->oauthProviders()->create([ | ||
'provider' => $provider, | ||
'provider_user_id' => $sUser->getId(), | ||
'access_token' => $sUser->token, | ||
'refresh_token' => $sUser->refreshToken, | ||
]); | ||
|
||
return $user; | ||
} | ||
} |
Oops, something went wrong.