0
votes

I want to Make Admin Page and User page with different table and different page for login authentication, I already make 2 models, Admin Model and User Model by default. And add Guard for Admin in /config/auth.php

This is my auth.php

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
    'admin' => [
      'driver' => 'session',
      'provider' => 'admin',
    ],
    'admin-api' => [
      'driver' => 'token',
      'provider' => 'admin',
    ],
],
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],
],
'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'admin' => [
        'provider' => 'admin',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

This is my admin model

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    use Notifiable;

    protected $guard = "admin";
    protected $table = 'admins';

    protected $fillable = [
        'name', 'email', 'jabatan','password'
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

And this is my auth logic in Controller

// Login Auth
public function auth()
{
  if (auth()->check()) {
    return redirect('/pendaftar');
  }
  else {
    if (auth()->attempt([
        'email'     => request('email'),
        'password'  => request('password')
      ]))
    {
      return redirect('/pendaftar');
    }
    else
    {
      return back()->withErrors([
        'message' => Lang::get('auth.failed')
      ]);
    }
   }
 }

This is my route:list for this Controller

| | POST | login-admin | |App\Http\Controllers\AdminController@auth | web  |

The Problem:

The problem I cannot login using this admin model, and always throw false condition in auth()->attempt([]) even I Hardcode the email and password. Please give me some advice and what wrong in my code.

NOTE

I Already using auth('admin')->attempt([]) for make guard specification for admin, but the result still same, I cannot Login.

I Already Change the defaults auth config to admin from web, and the result still same.

I Already change guard provider driver to database and pointing table to table admins in /config/auth.php, but nothing happen.

I Already Test with php thinker in cmd to get the data, and data is available but auth controller still throw false condition.

2

2 Answers

0
votes

there is a minor spelling mistake in your config/auth.php

your guard name is "admin" , and your provider name is same as your table name "admins"

'guards' => [
 ...

'admin' => [
      'driver' => 'session',
      'provider' => 'admins',  // replace 'admin' by 'admins'
    ],

],


'providers' => [
     ...
    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],
],

 'passwords' => [
        ...

        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],
0
votes

Try this

ADD -> use Illuminate\Http\Request;

public function auth(Request $request)

$credentials = $request->only(['email','password']);

Auth::guard('admin')->attempt($credentials);