pollux
pollux

Reputation: 181

laravel 5.4, adding second authentication (admin panel)

i want to create a second authentication in laravel 5.4 for an administration page.


First of all let me describe my problem: I have a functionable user login (default laravel auth) via 'web'-guard. Now i want to create a second authentication for the admin panel. I have another table which is storing the name, a token (which is something like a password) and an authority level.

The second/separate table is a dependency given by the system the page is developed for so i can't change that.

I have the login page for the administration panel but when i try to authenticate i get redirected back to the login everytime.


I already googled the whole thing and came across some good examples:

  1. https://jamesmcfadden.co.uk/custom-authentication-in-laravel-with-guards-and-user-service-providers

    • other links are in the controller paste on pastebin (link down below)

But i wasn't able to figure it out.


Here's what i did already:


So the question i need an answer to is:

  1. Where did i miss something? Where did i mess up?

I hope you can help me with this & thanks for your help!

Upvotes: 0

Views: 2333

Answers (2)

pollux
pollux

Reputation: 181

I fixed that with the following custom login method:

public function elevate(Request $request)
{
    // login
    $this->validateLogin($request);
    $admin = Admin::where('mAccount', '=', Auth::guard('web')->user()->login)
       ->where('mToken', '=', $request->input('mToken'))->first();
    if($admin){
       Auth::guard('admin')->login($admin);
        return redirect('/admin/dashboard');
    }
    else{
        throw new \ErrorException('Elevation failed!');
    }
}

protected function validateLogin(Request $request)
{
    $this->validate($request, [
        'mToken' => 'required|string|min:8',
        'g-recaptcha-response' => 'required|captcha'
    ]);
}

Upvotes: 1

tompec
tompec

Reputation: 1230

I'm sorry if I doesn't answer your question, but can't you add a simple column in your user table, like is_admin and authorize only users where is_admin = 1 to access the administration panel with a middleware, instead of login twice?

Upvotes: 1

Related Questions