Telexx's avatar
Level 5

Issues with authentication

I’m in a situation where one database serves two web applications: one older (not built with the Laravel framework) and one newer, which I’m currently developing using Laravel v11. The older version uses an outdated encryption algorithm for authentication. I need to be careful not to disrupt the existing system since both projects need to continue working properly. In this situation, I’ve run into two issues.

The older project uses the password column in the users table for authentication. To avoid interfering with it, I decided to create a new column called new_password, where I will store the correctly hashed passwords for the newer system. I planned to use this column for user authentication in the new Laravel project. I tried using the attempt function like this:

Auth::attempt(['username' => 'xy', 'new_password' => 'cv'])

However, it seems that the attempt function still expects the password key, because I encountered an "Undefined array key 'password'" error, even after modifying the getAuthPassword() function in the User model. I couldn’t find any documentation on how to change the password column, so I decided to drop this approach.

Next, I tried implementing my own authentication logic, like this:

   public function authenticate(LoginRequest $request)
    {
        $user = User::where('username', $request->username)->first();
        if ($user && Hash::check($request->new_password, $user->new_password)) {
            Auth::login($user);
            return redirect()->intended();
        }

        return back()->withErrors([
            'name' => 'The provided credentials do not match our records.',
        ])->onlyInput('name');
    }

At first glance, this seemed to work. The user was authenticated during the request, but the authentication state didn’t persist between requests. The user appears to be logged out on the next request, and I’m not sure why this is happening.

Do you have any suggestions?

0 likes
0 replies

Please or to participate in this conversation.