2016-11-22 133 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

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

相關問題