Laravel 5.3从控制器方法重定向到另一个路由

时间:2016-11-22 00:44:02

标签: php laravel redirect curl

由于我的应用程序要求,我选择不使用Laravel内置的用户身份验证。我们依靠第三方SSO来验证我们的用户,并且我无法让Socialite使用他们的SSO,所以我不得不自定义构建一个Controller来处理身份验证过程。当我需要将用户从回叫路由&中重定向时,控制器正在执行b-e-a-utifully up直到该部分。控制器到会员路线&控制器。它不会重定向。期。我已经尝试了我知道如何从控制器内部重定向到另一条路线的每一种方法,它都无法工作。

这是我为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 ddouglas@douglaswebdev.net");
        $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');

没有运气。

1 个答案:

答案 0 :(得分:1)

你的意思是$this->createNewUserAndLogin()?也许尝试return $this->updateUserAndLogin($user);return $this->verifyToken();,以便返回路线主要方法的回复?