Adding support to login via discord oauth.
This commit is contained in:
parent
910a8309b7
commit
8fd07c11a8
10 changed files with 112 additions and 0 deletions
43
app/Http/Controllers/Auth/OAuthController.php
Normal file
43
app/Http/Controllers/Auth/OAuthController.php
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
|
||||
class OAuthController extends Controller
|
||||
{
|
||||
/**
|
||||
* Redirect to OAuth driver.
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function create($driver)
|
||||
{
|
||||
return Socialite::driver($driver)->redirect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle callback from OAuth driver.
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function store(Request $request, $driver)
|
||||
{
|
||||
$oauth = Socialite::driver($driver)->stateless()->user();
|
||||
|
||||
$user = User::firstOrCreate([ "{$driver}_id" => $oauth->getId() ], [
|
||||
'username' => $oauth->getNickname()
|
||||
]);
|
||||
|
||||
Auth::guard('web')->login($user, true);
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
||||
return redirect('/');
|
||||
}
|
||||
}
|
||||
Reference in a new issue