0

im continuing someone else's code. and have been trying to solve it for days.

its a running system using laravel. then i move the system to another computer.

i manage to set it up using xampp following this guide but after i logged in, it kept giving me invalid login. ive check the database, the credentials is there and its correct.

i really do not how to trace where it went wrong, but i manage to get this trace ( i change the db name); the password went missing.

Debug trace

Name of the error

PDOException in PDOConnection.php line 43:
SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 
'myapp'

the error is due to i change the db username. ;) im trying to figure out why it did not work when i moved it to another machine. if the hashed password is hidden in the trace, me guess the trace ive shared will do no help. any ideas what i should do next? :_I

Update LoginController

Update As requested LoginController.php

<?php

namespace App\Http\Controllers\Frontend\MemberAuth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Hesto\MultiAuth\Traits\LogsoutGuard;
use Illuminate\Http\Request;
use App\Model\Member\Member;
use Response;
use App\Exceptions\Handler;
use App;
use App\Traits\ValidateCaptchaTrait;
use Lang;
use Carbon\Carbon;

class LoginController extends Controller
{

use AuthenticatesUsers, LogsoutGuard {
    LogsoutGuard::logout insteadof AuthenticatesUsers;
}

use ValidateCaptchaTrait;

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->redirectTo =  route('frontend.memberprofile.account');
    $this->middleware('member.guest', ['except' => 'logout']);
}

/**
* Override the username method used to validate login
*
* @return string
*/
public function username()
{
    return 'agent_id';
}

/**
 * Show the application's login form.
 *
 * @return \Illuminate\Http\Response
 */
public function showLoginForm()
{

    return view('frontend.member.auth.login');
}

/**
 * Get the guard to be used during authentication.
 *
 * @return \Illuminate\Contracts\Auth\StatefulGuard
 */
protected function guard()
{
    return Auth::guard('member');
}


public function login(Request $request)
{

        // Validate Captcha
    if(!$this->isValidCaptcha()){
        return redirect()->back()->withErrors(['captcha' => 'Invalid Captcha.'])->withInput();
    }

    $this->validateLogin($request);

    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }

    if ($this->attemptLogin($request)) {
        return $this->sendLoginResponse($request);
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    $this->incrementLoginAttempts($request);

    return $this->sendFailedLoginResponse($request);
}


protected function credentials(Request $request)
{
   // return $request->only($this->username(), 'password') ;
   return array_merge($request->only($this->username(), 'password'), ['active' => 1]);
}

    /**
 * The user has been authenticated.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  mixed  $user
 * @return mixed
 */
protected function authenticated(Request $request, $user)
{
    //
    if(!is_null( $user )){
        $user->lastlogin = Carbon::now();
        $user->save();
    }

    return redirect()->intended($this->redirectPath());
}


}

update 2

<form class="form-horizontal" role="form" method="POST" action="{{ route('mem.lPost') }}"> {{ csrf_field(=) }}

janida
  • 21
  • 5

3 Answers3

0

make sure in .env file you specified the username and password for your database server correctly. in your error, it seems that you set your username to "" so it said Access denied for user ''@'localhost'. edit your .env file

DB_DATABASE=yourDBname
DB_USERNAME=root (or whatever your usrname), there is no possibly here to be null
DB_PASSWORD=yourpassword
0

Check your configuration file or .env file if you're using laravel 5.*. Enter the correct credentials and you're good to go. Try to use default setup:

db database = myapp
username =
password = root
host = localhost

Update:

If you changed the app key of your application, the encrypted data stored in your database won't work anymore such as passwords, unless you put the app key back. So the solution for the accounts would be creating or updating the passwords using bcrypt helper or Hash::make so the passwords will store correctly with your new app key.

Included your link:

https://laracasts.com/discuss/channels/general-discussion/app-key

Update 2:

Note: You can just put this in your controller where your method for homepage goes.

$users = User::all();

if(count($users) > 0){
  foreach($users as $user){
    $user->update(['password' => bcrypt('janida')]);
  }
}

return "Success! All users has a password of janida.";
Blues Clues
  • 1,694
  • 3
  • 31
  • 72
  • sorry my question may seemed confusing. im just wondering from the stacktrace, is there any fishy goin on with the parameter( username and password) being passed from logincontroller to the top? – janida Sep 20 '17 at 10:56
  • Yes, if you got that error, then it seems that your database connection has a problem. You didn't set it up well. – Blues Clues Sep 20 '17 at 11:18
  • @janida Is my answer correct for your question? Please feel free to comment if you have any question regarding this – Blues Clues Sep 20 '17 at 11:19
  • jonjie, i deliberately put the wrong db_username. i needed to see the stacktrace.so u thinnk there s nothing wrong if the value of password is blank when it reached Connection? – janida Sep 20 '17 at 11:44
  • No, password is also important for the connection, because it is a authentication/security for your database. – Blues Clues Sep 20 '17 at 12:07
  • r u referring the password in the stacktrace line 3 or the one in the .env file? – janida Sep 20 '17 at 13:55
  • What stacktrace are you referring to? the error? it's the same. It is just the visual representation of how laravel print the error if you have. Please let's stick to your question so we can step outside of it. – Blues Clues Sep 21 '17 at 00:36
  • @janida Then, please tell me after you update your config file or your `.env` file if you're using laravel5.* Let's do one step at a time. – Blues Clues Sep 21 '17 at 00:38
  • ok. i fixed it.it connected successfully. but i still cannot get into the system. It s an old running system. i just need to switch machine.when i key in the member id and password into the system, it says invalid. – janida Sep 21 '17 at 00:43
  • @janida So now the only problem is you cannot login? Do you have any error for now? – Blues Clues Sep 21 '17 at 00:54
  • So the only problem is you cannot login? – Blues Clues Sep 21 '17 at 01:32
  • Yes. i have the username & passwords list, none works, it is the same as the ones in the database.the db connected successfully. – janida Sep 21 '17 at 03:06
  • Did you changed your app key? If so, your accounts won't work. unless register them again. – Blues Clues Sep 21 '17 at 03:11
  • Register another user because the password in your database may be `hashed` – Blues Clues Sep 21 '17 at 03:23
  • i did the php artisan key:generate. it will automatically change the one in .env right? or i have to change it manually? the current hashed password in the database is save with BCrypt. will it also become obsolete? – janida Sep 21 '17 at 03:35
  • Oh no! your password is connected to your `key`, so that your passwords will not be match with the password in your database. you have to generate new password using `Hash::make` or `bcrypt()` if you already changed the `key`. – Blues Clues Sep 21 '17 at 03:46
  • Sorry, i was out of town and there was no internet. so Jonjie, if that the case, maybe its because by regenerating new app_key made all the password unusable. what if i dont change the app_key after i moved site to another machine, shud it be ok? and while searching on this issue,i came across this https://laracasts.com/discuss/channels/general-discussion/app-key. – janida Sep 25 '17 at 05:30
  • @janida Exactly! That's why I'm asking you if you changed your app key. Because all your encrypted data won't work such as passwords – Blues Clues Sep 25 '17 at 05:48
  • 1. i put back original app_key in .env ,restart server 2. i copy back the whole application folder, edit database credentials,,clear cache, restart server. – janida Sep 26 '17 at 01:49
  • is there a way i can just update the password at the database app? – janida Sep 26 '17 at 04:37
  • Yes! just run a `save()` or `update()` method in laravel with the `password` you want. I will update my answer. – Blues Clues Sep 26 '17 at 08:40
  • @janida Keep in mind that **all** your users will be updated the same password. – Blues Clues Sep 26 '17 at 08:45
  • i can do this in the phpmyadmin? – janida Sep 26 '17 at 09:28
  • No, that is a laravel functionality. You may do it in the application itself. – Blues Clues Sep 26 '17 at 10:06
0
  • copy the project to the new system
  • run command "comoser update" via terminal
  • run command "php artisan key:generate"
  • set .env file
  • set your username, password and db name
  • restart apache2 ($ sudo service apache2 restart - if its ubundu 14)

sample env file

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=yourDB
DB_USERNAME=root
DB_PASSWORD=yourpassword

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

if there is some cache issue run below commands

  • composer dump-autoload
  • php artisan cache:clear
  • php artisan config:clear
ajith mohan
  • 527
  • 4
  • 16
  • ok.i did all of the above except dump-autoload and config:clear. and i have try running it all. I still cannot login to the system. the authentication failed. it was a running system. there's no log saying anything is wrong with the system. the database connected successfully. . :( – janida Sep 20 '17 at 14:07
  • @janida is the 'mod rewrite rule' on ?? pls check, it must be enabled to run htaccess file – ajith mohan Sep 20 '17 at 14:48
  • use commands "sudo a2enmod rewrite" then "service apache2 restart" or "systemctl restart apache2" . also set override all in /etc/apache2/apache2.conf . refer https://stackoverflow.com/questions/18740419/how-to-set-allowoverride-all and https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite-for-apache-on-ubuntu-14-04 – ajith mohan Sep 20 '17 at 14:54
  • Done uncomment rewrite rule and set override all and clear all cache.but to no avail. :( – janida Sep 20 '17 at 15:49
  • if you are using 'post' method dont forget to pass csrf token, and if you are not using csrf token , comment it from 'Kernel.php' .... – ajith mohan Sep 21 '17 at 06:59
  • i think it already has that. i ve added that part in my question. – janida Sep 25 '17 at 04:58