如何使用auth0

时间:2019-03-26 17:14:44

标签: laravel authentication auth0

我正在使用auth0项目(软件包为auth0/login)在laravel上工作。

我的项目有本地身份验证用户。

我要为auth0添加auth方法,所以我已经为auth0设置了auth配置。

这是我的/config/auth.php代码

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],    
        'auth0web'=>[
          'driver' => 'session',
          'provider' => 'auth0_users',
        ],


    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'auth0_users'=>[
            'driver' => 'auth0'
        ],

    ],

这是我的回调路由的控制器功能。


    public function auth0SigninCallback()
    {

        //return response()->json(Auth:: guard('auth0web)->user());
        $service = \App::make('auth0');

        // Try to get the user information
        $profile = $service->getUser();
        $userRepo = new Auth0UserRepository();

        $auth0User = $userRepo->getUserByUserInfo($profile);

        return response()->json($auth0User);
    }

Auth :: guard('auth0web)-> user(); 在此代码中,身份验证用户为空。

$profile暂时正确。 我如何获得不同的警卫(Auth::guard('auth0web')->user())进行身份验证 感谢您的进步。

PS 这是我的会话数据,也许是正确的

{
    "_token": "OAmhhYKWeO0tK6E5FN3DHaRvVYx6PP7z0YPMAPrz",
    "_previous": {
        "url": "http://xxxxx.dev/login"
    },
    "_flash": {
        "old": [],
        "new": []
    },
    "auth0__user": {
        "sub": "auth0|xxxxxxxxxxx",
        "nickname": "xxxxxxxx",
        "name": "xxxxx@xxxxx.com",
        "picture": "https://s.gravatar.com/avatar/xxxx.png",
        "updated_at": "2019-03-26T17:28:53.981Z",
        "email": "xxxxxx@ixxx.com",
        "email_verified": true
    }
}

我尝试覆盖回调路由控制器

这是代码

        // Get a handle of the Auth0 service (we don't know if it has an alias)
        $service = \App::make('auth0');

        // Try to get the user information
        $profile = $service->getUser();
        // Get the user related to the profile
        $auth0User = $this->userRepository->getUserByUserInfo($profile);

        if ($auth0User) {
            // If we have a user, we are going to log them in, but if
            // there is an onLogin defined we need to allow the Laravel developer
            // to implement the user as they want an also let them store it.
            if ($service->hasOnLogin()) {
                $user = $service->callOnLogin($auth0User);
            } else {
                // If not, the user will be fine
                $user = $auth0User;
            }
            Auth::guard('auth0web')->login($user, $service->rememberUser());
        }
        // dd(auth());
        // \Log::info(auth()->user());
        return \Redirect::intended('/home');
}

目前auth()正常工作

但是在重定向到首页Auth时已初始化

这是我自定义的中间件。

  public function handle($request, Closure $next)
    {
        // \Log::info('------------here---------');
        // dd(auth());
        $auth = Auth::user() ?? Auth::guard('ldap')->user();
        // dd(auth());
        // \Log::info(auth()->user());
        if (!isset($auth->id)) {
            return redirect('login');
        } else {
            return $next($request);
        }
    }

在这一部分,我从auth()->user()中得到空

0 个答案:

没有答案