0

I have to build a new system with a kind of old-style pre-existing MySQL database tables with Laravel 8 with Breeze. Already have a user data table that is totally different from the basic Auth system in Laravel 8. I am trying to figure out to make Auth system but it doesn't work right.

This is my User model class.

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $table = 'tb_user_info';

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        //'passwd',
        'remember_token',
    ];

    public function setPasswordAttribute($password)
    {
        $this->attributes['passwd'] = \DB::raw("password('$password')");
    }
    public function getAuthPassword()
    {
        return $this->passwd;
    }
}

And this is my store method in AuthenticatedSessionController, which I figured it is using in login.

public function store(LoginRequest $request)
    {
        $credentials = $request->validate([
            'mem_id' => ['required', 'email'],
            'passwd' => ['required'],
        ]);

       $user = User::select('mem_id', 'passwd')->where('mem_id', $request->mem_id)->first();
        if ($user->mem_id == $request->mem_id && $user->passwd == $this->sql_password($request->passwd)) {
            $request->session()->regenerate();

            return redirect('/dashboard');
        }

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

I thought it simply works, but after I try to login, it doesn't work at all, just keep redirecting back to the login page. I looked up the Network tab in Chrome, it seems like the dashboard page was called but it shows 302 Found and called the login page again.

Which part should I lookup for this situation? I am working on this for 3 days, nothing came up for a good solution. Please, please help me, I would be appreciated just a few tips.

Ps. Please don't answer like stop using the MySQL password for password or why don't you make a new database table? because it's meaningless answers. I would do that already if I can. This is a very limited situation at work. Please understand.

Jiwon
  • 366
  • 2
  • 17
  • is it redirecting back to login with errors? also make sure your sessions are working propertly – lagbox Mar 27 '22 at 23:17
  • @lagbox Actually there are no errors. And how I can check my sessions are working properly? – Jiwon Mar 29 '22 at 00:51

1 Answers1

0

A similar problem happens to me once.

if you check dd() in store function and it does not work I think. I think 302 reason is not in current store method check LoginRequest $request method. before hit to the store method function need to fullfill the LoginRequest $request therefore problem should be there

go to the LoginRequest method and there should be validation or usecase with password and you are using passwd instead of using password try to fix that error

DreX7001
  • 1
  • 1
  • I already tried the ``dd()`` in the `store` method and it works. And it's the same result I changed the `LoginRequest $request` to just `Request $request`. – Jiwon Mar 27 '22 at 06:25