davy_yg's avatar
Level 27

Trying to login

Hello,

I am in the middle of modifying an ecommerce website. When trying to login instead of carrying to the admin_dashboard, it carries me to the front page.

admin/login.blade.php

<div class="container vertical-align">
      <h1 class="text-center">Gionda CMS</h1>

      @if ($errors->any())
        @include('layouts.errors')
      @endif

        <form method="POST" action="{{ url('cpages/login') }}">
          {{ csrf_field() }}

          <div class="form-group">
            <label>Email:</label>
            <input name="email" type="text" class="form-control" />
          </div>

          <div class="form-group">
            <label>Password:</label>
            <input name="password" type="password" class="form-control" />
          </div>

          <div class="form-group">
            <br />
            <input type="submit" value="Login" class="btn btn-primary pull-right" />
          </div>
        </form>
</div>

CpagesLoginController.php

public function login_view()
    {
    return view('admin.login');
    }

    public function login()
    {
    if (auth()->attempt(['email' => request('email'), 'password' => request('password'), 'role' => 0])) {
        return redirect()->route('admin_dashboard');
    } elseif (auth()->attempt(['email' => request('email'), 'password' => request('password'), 'role' => 1])) {
        return redirect()->route('admin_dashboard');
    }


    return back()->withErrors([
    'message' => 'Email or Password is incorrect or Your Account cannot access this page.'
    ]);
    }

routes/web.php

//Dashboard Login (For Admin & Staff)
Route::get('/cpages/login', 'CPagesLoginController@login_view');
Route::post('/cpages/login', 'CPagesLoginController@login');

Whenever I type: http://localhost/eliteshop/public/cpages/login it keeps carries me to http://localhost/eliteshop/public/home.

I cannot figure out why?

0 likes
3 replies
Snapey's avatar

you are using route command which expects a named route. Have you named the route?

Do you have middleware wrapped around the admin dashboard or in the controller constructor?

Have you opened your network inspector in your browser and checked what redirects are occurring?

davy_yg's avatar
Level 27

This route is named :

Route::get('/cpages', 'DashboardController@index')->name('admin_dashboard');

I have this middleware:

class DashboardController extends Controller
{
    
public function __construct()
    {
        $this->middleware('staff');
        }

In the Network Inspector I only see these:

Request URL: http://localhost/eliteshop/public/cpages/login
Request Method: GET

Request URL: http://localhost/eliteshop/public/home
Request Method: GET

I dont' know what else I need to check.

Please or to participate in this conversation.