Laravel,Socialite&中间件无限重定向循环

时间:2016-02-07 10:42:18

标签: php laravel laravel-5.1 laravel-socialite

我按照社交网站上的laracasts教程进行了操作。我的最终目标是连接到azure广告,但我遇到了各方面的问题,所以我想我会从他的github教程开始并首先让它工作。我很好,直到我尝试使用中间件 - 现在我陷入无限重定向循环。如果我突破循环并转到主页,它已成功登录我 - 但显然用户不会知道这一点,并认为它失败了。

非常感谢任何帮助。

AuthController:     

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\AuthenticateUser;

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');
}   

}

的authenticateUser:

<?php

namespace App;

use Illuminate\Contracts\Auth\Guard;
use Laravel\Socialite\Contracts\Factory as Socialite;
use App\Repositories\UserRepository;
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();
}

}

routes.php文件:

Route::get('o365/connect', 'AuthController@login');        
Route::get('o365/callback', 'AuthController@login');        
Route::get('auth/logout', 'AuthController@logout');   

验证中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

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  \Illuminate\Http\Request  $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);
    }        
}
}

0 个答案:

没有答案