I am not able to make a user login in Laravel. I am using username
instead of email
.
Below is my Routes
use App\Message;
Route::get('/', function () {
return view('welcome');
});
//
Route::post('/contact','MessageController@store');
Route::get('/admin9419/dashboard','AdminDashboardController@index')->name('dash');
Route::get('/admin9419/login','SessionsController@create')->name('home');
Route::post('/admin9419/login','SessionsController@store');
Route::post('/admin9419/logout','SessionsController@destroy');
This is my SessionController File
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class SessionsController extends Controller
{
public function __construct()
{
$this->middleware('guest');
}
public function create(){
return view('sessions.create');
}
public function destroy(){
auth()->logout();
return redirect()->home();
}
public function store(){
if(!auth()->attempt(['username'=>request('username'),'password'=>request('password')])){
return back();
}
return redirect('/admin9419/dashboard');
}
}
Error:
Type error: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable
User class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
//
}
Can anyone help what i missed?
The code seems correct.
See the Class User that it should be something like:
use App\Notifications\MailResetPasswordToken;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
....
}
So can you paste the User class?
By default, Laravel uses the email field for authentication. If you would like to customize this, you may define a username method on your LoginController:
public function username()
{
return 'username';
}
public function store(Request $request){
$username= $request->input('username');
$password= $request->input('password');
if (Auth::guard('web')->attempt(['username' => $username , 'password'=> $password ])) {
return redirect()->intended('/admin9419/dashboard');
}
else{
return redirect('/login');
}
}