I have implemented laravel api authentification using passport, it works perfeclty in my local. However, when I deployed my application to cPanel server it doesn't work. and it returns this error when trying to login or register
exception: "Symfony\Component\Debug\Exception\FatalErrorException" file: "/home/newalgeria/new_algeria/vendor/zendframework/zend-diactoros/src/PhpInputStream.php" line: 17 message: "Class 'Zend\Diactoros\Stream' not found" trace: []
I followed these steps to set it up :
1-
composer require laravel/passport
2- I added this line to the providers in config/app.php
'providers' =>[
Laravel\Passport\PassportServiceProvider::class,
],
3-
php artisan passport:install
php artisan migrate
4- Added the hasApiToken to the user Model
use Laravel\Passport\HasApiTokens;
...
...
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
...
...
..
}
5- Added this to app/Providers/AuthServiceProvider.php
public function boot()
{
$this->registerPolicies();
Passport::routes();
}
6- changed the driver to passport in config/auth.php
...
..
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
...
..
6- created my routes
Route::post('login', 'API\UserController@login');
Route::post('register', 'API\UserController@register');
Route::group(['middleware' => 'auth:api'], function(){
Route::post('details', 'API\UserController@details');
});
7- then my auth controller ( ## Where I'm finding the problem on createtoken() function ## )
/**
* login api
*
* @return \Illuminate\Http\Response
*/
public function login(){
if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')-> accessToken;
return response()->json(['success' => $success], $this-> successStatus);
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
/**
* Register api
*
* @return \Illuminate\Http\Response
*/
public function register(Request $request)
{
$input = $request->all();
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|unique:users',
'password' => 'required',
'c_password' => 'required|same:password',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()], 401);
}
$input = $request->all();
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
$success['token'] = $user->createToken('MyApp')-> accessToken;
$success['name'] = $user->name;
return response()->json(['success'=>$success], $this-> successStatus);
}
/**
* details api
*
* @return \Illuminate\Http\Response
*/
public function details()
{
$user = Auth::user();
return response()->json(['success' => $user], $this-> successStatus);
}
What I tried so far
1-I tried to run on the server
run compsoer php passport:install
again to creat new client ID but it didn't work the same error
2-I tried to update composer the same thing
3- I tried to comment this line ( where createToke('MyApp')) is on my auth contorller on the register function
$success['token'] = $user->createToken('MyApp')-> accessToken;
it creates new user but without token ==> that means the problem is in this function ? but it works on the local
I excpect the login and register normal as in the local but
I receive this error again and again
exception: "Symfony\Component\Debug\Exception\FatalErrorException" file: "/home/newalgeria/new_algeria/vendor/zendframework/zend-diactoros/src/PhpInputStream.php" line: 17 message: "Class 'Zend\Diactoros\Stream' not found" trace: []
P.S:
I am using larvel 5.8 whic require php >= 7.1 but. However in my server terminal when I run php -v it shows me that php's version is 5.6 but in the browser when I test phpversion(); it returns php 7.2