Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace AppHttpControllers;
- use IlluminateHttpRequest;
- use AppHttpControllersController;
- use AppAuthenticateUser;
- class AuthController extends Controller {
- public function login(AuthenticateUser $authenticateUser, Request $request) {
- $authenticateUser->execute($request->has('code'), $this);
- return Socialite::with('github')->redirect();
- }
- public function logout() {
- if (Auth::check()) Auth::logout();
- return view('auth.loggedout');
- }
- }
- <?php
- namespace App;
- use IlluminateContractsAuthGuard;
- use LaravelSocialiteContractsFactory as Socialite;
- use AppRepositoriesUserRepository;
- use Request;
- class AuthenticateUser {
- private $socialite;
- private $auth;
- public function __construct(UserRepository $users, Socialite $socialite, Guard $auth) {
- $this->users = $users;
- $this->socialite = $socialite;
- $this->auth = $auth;
- }
- public function execute($hasCode) {
- if (!$hasCode) {
- return $this->getAuthorizationFirst();
- }
- $user = $this->users->findbyEmail($this->getUser());
- $this->auth->login($user, true);
- return redirect('/');
- }
- public function getAuthorizationFirst() {
- return $this->socialite->driver('github')->redirect();
- }
- public
- function getUser() {
- return $user = $this->socialite->driver('github')->user();
- }
- }
- Route::get('o365/connect', 'AuthController@login');
- Route::get('o365/callback', 'AuthController@login');
- Route::get('auth/logout', 'AuthController@logout');
- <?php
- namespace AppHttpMiddleware;
- use Closure;
- use IlluminateContractsAuthGuard;
- class Authenticate {
- /**
- * The Guard implementation.
- *
- * @var Guard
- */
- protected $auth;
- /**
- * Create a new filter instance.
- *
- * @param Guard $auth
- * @return void
- */
- public function __construct(Guard $auth) {
- $this->auth = $auth;
- }
- /**
- * Handle an incoming request.
- *
- * @param IlluminateHttpRequest $request
- * @param Closure $next
- * @return mixed
- */
- public function handle($request, Closure $next) {
- if ($this->auth->guest()) {
- if ($request->ajax()) {
- return response('Unauthorized.', 401);
- } else {
- return redirect()->guest('o365/connect');
- }
- }
- if (Auth::check()) {
- return $next($request);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement