2016-11-22 68 views
1

由于我的应用程序要求,我选择不使用Laravel内置的用户身份验证。我们依靠第三方SSO对用户进行身份验证,并且我无法让Socialite与他们的SSO一起工作,所以我不得不定制构建控制器来处理身份验证过程。所述控制器被执行B-E-A-utifully直到部分,当我需要用户从回调路由&控制器向会员路线&控制器重定向。它不会重定向。期。我已经尝试了每种方法,我知道如何重定向到控制器内的另一个路由,它不会工作。Laravel 5.3从控制器方法重定向到另一个路由

这里是我的Laravel 5.3定制AuthController:

<?php 

namespace App\Http\Controllers; 

use App\User; 
use Curl\Curl; 
use App\Http\Controllers\PhealController as Pheal; 
use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Validator; 
use Illuminate\Support\Facades\Auth; 
use Illuminate\Routing\Redirector; 

class AuthController extends Controller 
{ 

    protected $curl; 
    private $data; 

    public function __construct() 
    { 
     $this->curl = new Curl(); 
     $this->pheal = new Pheal(); 
     $this->data = []; 
    } 
    public function sendToSSO() 
    { 
     $url = env('EVE_SSO_LOGIN')."?response_type=code&redirect_uri=".env('EVE_CALLBACK_URL')."&client_id=".env('EVE_CLIENT_ID')."&scope=".env('EVE_SCOPES'); 
     return redirect($url); 
    } 

    public function handleCallback(Request $request) 

    { 
     $this->curl->setHeader('Authorization', "Basic ". base64_encode(env('EVE_CLIENT_ID').":".env('EVE_SECRET'))); 
     $this->curl->setHeader('Content-Type', "application/x-www-form-urlencoded"); 
     $this->curl->setHeader('Host', "login.eveonline.com"); 
     $this->curl->post('https://login.eveonline.com/oauth/token', [ 
      'grant_type' => 'authorization_code', 
      'code' => $request->code 
     ]); 

     $response = $this->curl->response; 

     if (isset($response->error)) { 
      throw new \Exception($response->error_description); 
     } 

     $this->data = [ 
      'accessToken' => $response->access_token, 
      'refreshToken' => $response->refresh_token 
     ]; 

     $this->verifyToken(); 

    } 

    public function verifyToken() 
    { 

     $this->curl->setHeader('User-Agent', "David Douglas [email protected]"); 
     $this->curl->setHeader('Authorization', "Bearer ". $this->data['accessToken']); 
     $this->curl->setHeader('Host', "login.eveonline.com"); 
     $this->curl->get('https://login.eveonline.com/oauth/verify'); 

     $response = $this->curl->response; 

     if (isset($response->error)) { 
      throw new \Exception($response->error_description); 
     } 


     $this->data['characterID'] = $response->CharacterID; 
     $this->data['characterName'] = $response->CharacterName; 
     $this->data['accessTokenExpire'] = $response->ExpiresOn; 

     try { 
      $characterInfo = $this->pheal->call('eve', 'CharacterInfo', ['characterID' => $this->data['characterID']])['result']; 
     } catch (\Exceoption $e) { 
      abort(404); 
     } 

     if (!isset($characterInfo['allianceID'])) { 
      abort(403, "Care Factor Alliance Members Only. Sorry :-("); 
     } 
     if ($characterInfo['allianceID'] !== env('CF-ALLIANCE-ID')) { 
      abort(403, "Care Factor Alliance Members Only. Sorry :-("); 
     } 

     $this->data['corporationID'] = $characterInfo['corporationID']; 
     $this->data['corporation'] = $characterInfo['corporation']; 

     $user = User::find($this->data['characterID']); 

     if ($user) { 
      $this->updateUserAndLogin($user); 
     } else { 
      $this->createNewUserAndLogin(); 
     } 
    } 

    private function getData() 
    { 
     return $this->data; 
    } 

    public function createNewUserAndLogin() 
    { 
     dd('To be Created'); 
    } 
    public function updateUserAndLogin($user) 
    { 
     $user->corporationID = $this->data['corporationID']; 
     $user->corporation = $this->data['corporation']; 
     $user->accessToken = $this->data['accessToken']; 
     $user->refreshToken = $this->data['refreshToken']; 
     $user->accessTokenExpire = $this->data['accessTokenExpire']; 
     $user->save(); 
     //Auth::login($user); 

     return redirect('member/dashboard/'); 
    } 


} 

我也曾尝试:

return redirect()->route('member.dashboard'); 

没有运气。

+0

我知道后续的计算器答案,并没有奏效。 http://stackoverflow.com/questions/21079280/laravel-redirect-from-controller-to-named-route-with-prams-in-url# – Dave

+0

whar错误信息,你遇到过吗? –

+0

不幸的是没有。现在这只是一个没有输出的处理脚本,所以脚本停止而显示白色屏幕,而不是用户登录和重定向。 – Dave

回答

1

你的意思是$this->createNewUserAndLogin()?也许试图return $this->updateUserAndLogin($user);return $this->verifyToken();所以你返回的路线的主要方法的反应如何?

+0

谢谢你的回答,但那没有奏效。我甚至将代码从该方法移动到handleCallback方法中,但仍然没有运气。谢谢你。 – Dave

+0

我很抱歉。我错过了你回答的第二部分,你说在$ this-> verifytoken()前面加一个回车符。一旦我这样做,应用程序重定向。非常感谢。 – Dave

+0

真棒,很高兴它工作:) –

相关问题