skip to Main Content

Why does a 403 error occur when I log in and am forwarded to a route? However, if I enter the URL manually, the URL will be accessible

This is a middleware role with identification 1, it should be accessible with user role 1

here is the code

php artisan route:clear && php artisan route:cache
composer dump-autoload

is not working ,

public function login(Request $request)
    {
        $input = $request->all();
        $this->validate($request, [
            'identifier' => 'required',
            'password' => 'required'
        ]);

        $credentials = [
            'password' => $input['password']
        ];

        if (strpos($input['identifier'], '@') !== false) {
            $credentials['email'] = $input['identifier'];
        } else {
            $credentials['username'] = $input['identifier'];
        }

        if (auth()->attempt($credentials)) {
            if (auth()->user()->role === 1) {
                return redirect()->route('seller.dashboard');
            } else {
                abort(403, 'Anda tidak memiliki akses.');
            }
        } else {
            return redirect()->route('seller.login')->with('error', 'Email atau Password tidak sesuai.');
        }
    }

It should be directly forwarded to route(‘seller.dashboard’) without a 403 statement because the person logging in is role 1

2

Answers


  1. Chosen as BEST ANSWER

    I didn't expect that from the political parties in Indonesia, you would venture into the world of code, btw TQ Anas Urbaningrum


  2. The auth attempt method not working properly try to troubleshoot the the attempt method.

    auth()->attempt($credentials)
    

    check this line what is returning ?

    dd(auth()->attempt($credentials));
    

    if auth attempt return true then check the auth of user. which user is authenticated?

    dd(auth()->user());
    

    here you can achive you answer if user authenticated what is the role and if not
    so try to resolve the auth attempt.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search