Skip to content

Commit

Permalink
Add login users trait
Browse files Browse the repository at this point in the history
  • Loading branch information
CasperLaiTW committed Jun 2, 2017
1 parent 31b0a6a commit 575f5e6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"name": "vohinc/km-socialite",
"type": "library",
"description": "KM socialite plugin",
"require": {
"laravel/socialite": "^3.0"
"laravel/socialite": "^3.0",
"illuminate/routing": "^5.4"
},
"authors": [
{
Expand Down
70 changes: 70 additions & 0 deletions src/LoginUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Voh\KmSocialite;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

/**
* Trait LoginUsers
* @package Voh\KmSocialite
*/
trait LoginUsers
{
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/';

/**
* Create a new controller instance.
*
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}

/**
* @return mixed
*/
public function oauth()
{
return Socialite::driver('km')->redirect();
}

/**
* @return mixed
*/
public function login()
{
/** @var \Laravel\Socialite\Two\User $rawUser */
$rawUser = Socialite::driver('km')->user();

$user = $this->performLogin($rawUser);

Auth::login($user);

return redirect(session('url.intended', $this->redirectTo));
}

/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
Auth::guard()->logout();

$request->session()->flush();

$request->session()->regenerate();

return redirect('/');
}
}

0 comments on commit 575f5e6

Please sign in to comment.