PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 4, 2022

[FIXED] Laravel passport returning Unauthenticated in production but works on localhost

 February 04, 2022     laravel, php     No comments   

Issue

I have a laravel project that works very well on an X server and put that same project (same code) on another Y server, to my surprise the authenticated routes didn't work, I always get the unauthenticated error, why does this happen?

{
  "message":"Unauthenticated.",
  "success":false,
  "status_code":500
}

I used the commands: - php artisan passport: install - php artisan config: cache - php artisan cache: clear - php artisan key: generate

And yet I still get the unauthenticated error, the token is being passed correctly by the front end, it's a bearer token, this token is generated when the user logs in using the createToken ('myApi') -> accessToken method

public function login(AuthLoginRequest $request)
{
    $user = User::with('role')->where(['email' => $request->email])->get()->first();
    if(!$user){
        abort(404, 'userNotFound');
    }

    if(!password_verify($request->password, $user->password)){
        abort(401, 'invalidCredentials');
    }

    $token = $user->createToken('MyApiToken')->accessToken;
    Access::customCreate($request->all(), $user);

    $response = [
        'id' => $user->id,
        'name' => $user->name,
        'email' => $user->email,
        'profile_image' => $user->profile_image,
        'token' => $token,
        'old_password_changed' => $user->old_password_changed
    ];

    return response()->json($response, 200);
}

The login code works perfectly, it returns me the Bearer token, but after sending any request with this token I get the Unauthenticated error. Where am i going wrong? Since the same code works on another server.


Solution

I have a solution.

What was the problem?

For some reason, the Apache server has modules that ignore the Authorization header, say it's for security (and I don't doubt it).

How did I solve it?

I simply disabled the following modules in the Apache settings:

  • mod_cgid
  • mod_fcgid
  • mod_proxy_scgi

And in the httpd.conf file I added the line (Linux/CentOS 7.6):

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1


Answered By - viniciussvl
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing