0

I've created Lumen Passport in my local and free Heroku and successfully created it. Now I’m creating my production lumen in digital ocean VPS. All of my applications work fine and give good bad responses format when it should be failed responses, except my login. In my login, it passes a good response when correct emails and passwords but returns 500 in other cases.

This is my code in my LoginService.php

public function generate_token($email, $password){
    // $token = Http::post(config('service.passport.login_endpoint'), [
    //     'grant_type' => 'password',
    //     'client_id' => config('service.passport.client_id'),
    //     'client_secret' => config('service.passport.client_secret'),
    //     'username' => $email,
    //     'password' => $password,
    // ]);

    // return $token->json();
    $http = new Client;
    try {
        $response = $http->post(config('service.passport.login_endpoint'), [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => config('service.passport.client_id'),
                'client_secret' => config('service.passport.client_secret'),
                'username' => $email,
                'password' => $password,
            ]
        ]);
        return [true, $response->getBody()];
    } catch (ClientException $e) {
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        return [false, Psr7\str($e->getResponse())];
        // return $responseBodyAsString;
    }
}

public function login($email, $password){
    $login = $this->generate_token($email, $password);

    // if(isset($login['error'])){
    //     $response = [
    //         "success" => false, 
    //         "message" => (object)[
    //             "errorInfo" => [
    //                 "status" => 401,
    //                 "reason" => $login['error'],
    //                 "server_code" => 401,
    //                 "status_detail" => $login['error_description']
    //             ]
    //         ],
    //         "status" => 401
    //     ];
    // } else {
    //     $response = [
    //         "success" => true,
    //         "data" => [
    //             "message" => "success generate token",
    //             "token" => $login['access_token']
    //         ],
    //         "status" => 200
    //     ];
    // }
    if($login[0]){
        $response = [
            "success" => true,
            "data" => json_decode($login[1]),
            "status" => 200
        ];
    } else {
        $response = [
            "success" => true,
            "data" => $login[1],
            "status" => 200
        ];
    }


    return $response;
}

This is my error log from my server

2022/02/08 09:11:58 [error] 61359#61359: *4187 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught UnexpectedValueException: The stream or file "/var/www/html/ims/storage/logs/lumen-2022-02-08.log" could not be opened in append mode: failed to open stream: Permission denied in /var/www/html/ims/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:111
Stack trace:
#0 /var/www/html/ims/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(121): Monolog\Handler\StreamHandler->write()
#1 /var/www/html/ims/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(42): Monolog\Handler\RotatingFileHandler->write()
#2 /var/www/html/ims/vendor/monolog/monolog/src/Monolog/Logger.php(317): Monolog\Handler\AbstractProcessingHandler->handle()
#3 /var/www/html/ims/vendor/monolog/monolog/src/Monolog/Logger.php(539): Monolog\Logger->addRecord()
#4 /var/www/html/ims/vendor/illuminate/log/Logger.php(174): Monolog\Logger->error()
#5 /var/www/html/ims/vendor/illuminate/log/Logger.php(87): Illuminate\Log\Logger->writeLog()
#6 /var/www/html/...PHP message: PHP Fatal error:  Uncaught UnexpectedValueException: The stream or file "/var/www/html/ims/storage/logs/lumen-2022-02-08.log" could not be opened in append mode: failed to open stream: Permission denied in /var/www/html/ims/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:111
Stack trace:
#0 /var/www/html/ims/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(121): Monolog\Handler\StreamHandler->write()
#1 /var/www/html/ims/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(42): Monolog\Handler\RotatingFileHandler->write()
#2 /var/www/html/ims/vendor/monolog/monolog/src/Monolog/Logger.php(317): Monolog\Handler\AbstractProcessingHandler->handle()
#3 /var/www/html/ims/vendor/monolog/monolog/src/Monolog/Logger.php(539): Monolog\Logger->addRecord()
#4 /var/www/html/ims/vendor/illuminate/log/Logger.php(174): Monolog\Logger->error()
#5 /var/www/html/ims/vendor/illuminate/log/Logger.php(87):

This is my permission and user list in my storage and logs

root@mighty-app-1:/var/www/html/ims# ll
total 444
drwxr-xr-x 14 root www-data   4096 Feb  7 23:30 ./
drwxr-xr-x  4 root root       4096 Feb  3 17:17 ../
-rw-r--r--  1 root root        220 Feb  7 07:14 .editorconfig
-rwxr-xr-x  1 root root        586 Feb  7 23:30 .env*
-rw-r--r--  1 root root        293 Feb  7 07:14 .env.example
drwxr-xr-x  8 root www-data   4096 Feb  8 07:12 .git/
-rw-r--r--  1 root root         87 Feb  7 07:14 .gitignore
-rw-r--r--  1 root root         71 Feb  7 07:14 .styleci.yml
drwxr-xr-x  2 root www-data   4096 Feb  7 07:14 .vscode/
-rw-r--r--  1 root root        121 Feb  7 07:14 Procfile
-rw-r--r--  1 root root       1585 Feb  7 07:14 README.md
drwxr-xr-x 13 root www-data   4096 Feb  7 07:14 app/
-rwxr-xr-x  1 root root       1094 Feb  7 07:14 artisan*
drwxr-xrwx  2 root www-data   4096 Feb  7 07:14 bootstrap/
-rw-r--r--  1 root root       1463 Feb  7 07:14 composer.json
-rw-r--r--  1 root root     355645 Feb  7 07:14 composer.lock
drwxr-xr-x  2 root www-data   4096 Feb  7 07:14 config/
drwxr-xr-x  5 root www-data   4096 Feb  3 17:01 database/
-rw-r--r--  1 root root        756 Feb  7 07:14 phpunit.xml
drwxr-xr-x  3 root www-data   4096 Feb  8 07:12 public/
drwxr-xr-x  3 root www-data   4096 Feb  3 17:01 resources/
drwxr-xr-x  2 root www-data   4096 Feb  7 07:14 routes/
drwxr-xrwx  5 root www-data   4096 Feb  7 07:14 storage/
drwxr-xr-x  2 root www-data   4096 Feb  7 07:14 tests/
drwxr-xr-x 60 root root       4096 Feb  4 03:49 vendor/
root@mighty-app-1:/var/www/html/ims# cd storage
root@mighty-app-1:/var/www/html/ims/storage# ll
total 36
drwxr-xrwx  5 root www-data 4096 Feb  7 07:14 ./
drwxr-xr-x 14 root www-data 4096 Feb  7 23:30 ../
-rw-r--r--  1 root root     6148 Feb  7 07:14 .DS_Store
drwxr-xrwx  2 root www-data 4096 Feb  7 07:14 app/
drwxr-xrwx  4 root www-data 4096 Feb  7 07:14 framework/
drwxr-xrwx  2 root www-data 4096 Feb  7 10:24 logs/
-rw-r--r--  1 root root     3292 Feb  7 23:30 oauth-private.key
-rw-r--r--  1 root root      812 Feb  7 23:30 oauth-public.key
root@mighty-app-1:/var/www/html/ims/storage# cd logs/
root@mighty-app-1:/var/www/html/ims/storage/logs# ll
total 108
drwxr-xrwx 2 root www-data  4096 Feb  7 10:24 ./
drwxr-xrwx 5 root www-data  4096 Feb  7 07:14 ../
-rw-r--r-- 1 root root        14 Feb  7 07:14 .gitignore
-rwxr-xrwx 1 root root     93743 Feb  4 11:13 lumen-2022-02-04.log*
root@mighty-app-1:/var/www/html/ims/storage/logs# 

I've tried some ways to check whether the responses couldn't be read or something, but it seems that there's something missing that I couldn't get.

I didn’t find other similar cases with my case other than this Lumen Passport return 500 error instead 401 error but this also couldn’t solve my problem.

Can anyone give me some tips for me? Thanks in advance

Averlyn
  • 11
  • 4
  • `500` response is very generic. You'll need to review your web server error log file to find out further information. – Peppermintology Feb 07 '22 at 11:32
  • Unfortunately, it didn't write the lumen log at all where It supposedly and it does write other bad responses except this 500 login in lumen log. – Averlyn Feb 07 '22 at 22:30
  • You need to look at the sever error logs, not the the lumen one. – Peppermintology Feb 08 '22 at 06:56
  • I've edited my post and show my errors from server. From those errors, should i change permission in my vendor? Or do you have any other solutions? – Averlyn Feb 08 '22 at 09:25
  • You need to change the permissions on the log folder for your application. Something is failing and Lumen is trying to write to the log file but being denied. – Peppermintology Feb 08 '22 at 10:21
  • I've added my permission and user list in my storage and logs. From those, which one should i change or what command should i execute? https://stackoverflow.com/questions/46649150/lumen-failed-to-open-stream-permission-denied-in-vendor-monolog-monolog-sr I have tried that one with "sudo chown -R root storage" command and failed. Do you have any other suggestions? – Averlyn Feb 09 '22 at 09:11
  • You don't want `root`. You want the `user` and `group` to be able to write. The exact command depends on your OS and sometimes the web server. For example, on Ubuntu it is commonly `sudo chown -R www-data:www-data /var/www/project-name`. – Peppermintology Feb 09 '22 at 09:22
  • Should I execute that command in /var/www/project-name or in /var/www/project-name/storage? My OS is ubuntu – Averlyn Feb 09 '22 at 09:55
  • @Peppermintology thank you so much man, I've solved this problem with that command – Averlyn Feb 13 '22 at 22:32
  • Pleased you got it resolved. [: – Peppermintology Feb 14 '22 at 09:32

1 Answers1

0

After some time searching for answer, the problem was because my storage folder had root user permission. And it solved by giving the user and group permission to www-data with command like this

sudo chown -R www-data:www-data /var/www/<project_name>/storage

Special thanks to @Peppermintology for giving me many hints

Averlyn
  • 11
  • 4