简体   繁体   中英

Unable to get authenticated user using Laravel 5.8 and Auth0

I have a Laravel 5.8 API that I want to secure using Auth0. So far I've followed every step of this tutorial :

  1. On the front side, Login/logout links are currently implemented in Blade, and this works fine, though the rendered content on the page is done using Vue Router, making AJAX requests to the API for the data.
  2. The default User model in Laravel has been modified to store name , sub , and email per the tutorial, and this populates as well.
  3. The API endpoint is secured using the jwt middleware created during the tutorial, and I can successfully submit a GET along with a hard-coded Bearer auth token in Postman and get a good response.

However, at some point I'd like to be able to pass an access token off to Vue so it can do its thing, but I'm unable to get the current authenticated user. After hitting Auth0, it redirects back to my callback route with auth gobbledlygook in the URL. The route in turn loads a controller method , and everything even looks good there:

// Get the user related to the profile
$auth0User = $this->userRepository->getUserByUserInfo($profile);   // returns good user

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()) {                                  // returns false
        $user = $service->callOnLogin($auth0User);
    } else {
        // If not, the user will be fine
        $user = $auth0User;
    }
    \Auth::login($user, $service->rememberUser());     // "normal" Laravel login flow?
}

I'm not an expert on the framework, but the last line above seems to start the "normal" Laravel user login flow. Given that, shouldn't I see something other than null when I do auth()->user() , or even app('auth0')->getUser() ?

Try using a simple tutorial if you're a beginner, I would recommend this

It uses a simple JWT package to create a jwt token which you can get when the user authenticates.

JWTAuth::attempt(['email'=>$email,'password'=>$password]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM