I have a web admin panel that is using admins table where as frontend is using api and users table for authentication
Admin and User models are somewhat same with just table names different as
class Admin extends Authenticatable
{
use HasFactory, Notifiable;
protected $table = 'admins';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
}
My config/auth.php is defined as
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'sanctum',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => AppModelsUser::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => AppModelsAdmin::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_reset_tokens',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
My routes/web.php is defined as
Route::middleware('web')->group(function () {
Route::Group(['prefix' => 'admin'], function () {
Route::get('signin', function () {
return view('admin.signin');
})->name('admin.signin');
where as my routes/api.php is defined as
Route::middleware('auth:sanctum')->group(function () {
Route::post('/login', [ApiController::class, 'login']);
Route::post('/register', [ApiController::class, 'register']);
Route::post('/forgot-password', [ApiController::class, 'sendResetLinkEmail']);
Route::post('/search-donations', [ApiController::class, 'searchDonations']);
Route::get('/get-categories', [ApiController::class, 'getCategories']);
Route::get('/test', [ApiController::class, 'test']);
My ApiController is defined as
class ApiController extends Controller {
public function __construct() {
$this->middleware('auth:sanctum', ['except' => ['test', 'login', 'register', 'searchDonations', 'getCategories', 'sendResetLinkEmail']]);
}
public function login(Request $request) {
// Enable query logging
DB::enableQueryLog();
// Validate the request data
$validatedData = $request->validate([
'email' => 'required|email',
'password' => 'required',
'device_type' => 'sometimes|string',
'device_token' => 'sometimes|string',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication successful
....
}
My web logins work fine however apis are still looking at admins table when try to authenticate, what is wrong with my above configuration?