I have this error in Laravel latest for authentication
Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must
be an instance of Illuminate\Contracts\Auth\UserProvider, null given,
called in
G:\xampp\htdocs\newrestaurantcrm\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php
can anyone give me an idea about this error why this error is occurring?
I am using below code for authentication in my Auth\AuthController.php file
protected function login(Request $request) {
$email = $request->email;
$password = bcrypt($request->password);
if (Auth::login(['email' => $email, 'password' => $password])) {
return redirect()->intended('/admin/dashboard');
}
}
Change your code to
public function login(Request $request) {
$email = $request->get('email');
$password = $request->get('password');
if (Auth::attempt(['email' => $email, 'password' => $password])) {
return redirect()->intended('/admin/dashboard');
}
}
I changed the protected to public, Auth::login() to Auth::attempt(). If you use login, you will actually have to pass the User object you like to login as. You do not need to encrypt the password to pass to attempt and. To make this simpler you can write
public function login(Request $request) {
if (Auth::attempt($request->only('email', 'password'))) {
return redirect()->intended('/admin/dashboard');
}
}
This of course assumes that your form has correct name for fields, email and password and also has same field email and password in users table as well.
When you are authenticate user against email and password then use Auth::attempt or Auth::once (For single request). When we have user instance and we want to login with that user instance then we use Auth::login. For your case use Auth::attempt like this
public function login(Request $request) {
$email = $request->email;
$password = bcrypt($request->password);
if (Auth::attempt(['email' => $email, 'password' => $password])) {
return redirect()->intended('/admin/dashboard');
}
}
Details: https://laravel.com/docs/5.6/authentication#included-authenticating
Related
how to make correct validation if i have input from where i can sign in with username or email, i want to if user will text username in field, it check if this username doesnt exist, then fail, also if he will choose to use email to sign in, then it will check if email doesnt exist then fail. (login form have only 2 inputs, login and password, in login u can text username or email)
my rule =
public function rules()
{
return [
'user' => 'required|min:3|exists:users,username,email]',
'password' => 'required',
];
}
You can use custom login as :
public function login(Request $request)
{
$request->validate([
'user' => 'required|min:3]',
'password' => 'required',
]);
}
$userName_or_Email = $request->user;
$password = $request->password;
$user = User::where('username',$userName_or_Email)->first();
if(empty($user)){
$user = User::where('email',$userName_or_Email)->first();
}
if(empty($user)){
return new Response()->with('error','Username or email not available in database');
}
if(Hash::check($password, $user->password)){
// Login successful code
}else{
return new Response()->with('error','Username or email not available in database');
}
I'm new to Lumen. How can I implement login?
I've tried this code but I got an error. And I found out in the documentation that Lumen does not support session. So Auth::attempt() is not available.
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|string|email',
'password' => 'required|string'
]);
$credentials = $request->only('email', 'password');
if( !Auth::attempt($credentials) ) {
return response()->json([
'message' => 'Unauthorized'
], 401);
}
return response()->json(['message' => 'Successfully login'], 200);
}
How can I authenticate user using login method? Since Auth::attempt() is not working are there any alternatives? Thanks!
Take email and password and check in the database manually.
$email = $request->input('email');
$password = $request->input('password');
$result = DB::table('users')->where('email', $email)->first();
if (!is_null($result)) {
if($password == $result->password) {
return response(200);
}
}
Checking in table users, in columns email and password.
In my laravel app, at the start I had decided to create my own custom login controller, rather than use the base one.
public function postSignin(Request $request, AppMailer $mailer) {
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
if (!Auth::attempt($request->only(['email', 'password']), $request->has('remember'))) {
return redirect()->back()->with('info', 'Could not sign you in with those details.');
}
if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password'), 'verified' => 0]))
{
$mailer->sendEmailConfirmationTo(Auth::user());
Auth::logout();
return redirect()->back()->with('info', 'Email verification required.');
}
Auth::user()->last_login = new DateTime();
Auth::user()->save();
return redirect()->back()->with('info', 'You are now signed in.');
}
And now I want to edit this so that users can also login with their usernames and not just their emails, using the same field. However, the attempt method is confusing. It seems to expect an email even after I switch the values around.
The Auth documentation isn't very helpful in this case either. It asks me to add this:
public function username()
{
return 'username';
}
in the login controller, but obviously this is for a default setup.
You can use the 'FILTER_VALIDATE_EMAIL' checker.
$username = $request->get('username');
$password = $request->get('password');
$remember_me = $request->get('remember_me','1');
$field = filter_var($username,FILTER_VALIDATE_EMAIL)? 'email': 'username';
if(Auth::attempt([$field => $username,'password' => $password],$remember_me)){
//Auth successful here
}
Meaning FILTER_VALIDATE_EMAIL do check the string whether it is in email format or not.
I hope this sample code helps you.
-ken
I want to login using username or password in laravel 5.4, I tried some thing but nothing worked for me.
public function login(Request $request) {
$field = filter_var($request->input('login'), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
$request->merge([$field => $request->input('login')]);
if ($this->auth->attempt($request->only($field, 'password')))
{
return redirect('/');
}
return redirect('/login')->withErrors([
'error' => 'These credentials do not match our records.sssss',
]);
}
I added this function in LoginController.php file, but I think it is not hitting this function, So how to do it ?
Try Something like this. You should override this code in LoginController.php
public function login(Request $request){
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|string',
]);
if (Auth::guard()->attempt(['email'=>$request->email,'password'=>$request-password], $request->remember)) {
if successfull, then redirect to intended location
return view('level1');
else
return view('manager');
}
//if successfull, then redirect back to login with the form data
return redirect()->back()->withInput($request->only('email','remember'));
}
return $this->sendFailedLoginResponse($request);
}
Laravel already provides authentication by default. In fact everything is configured already by Laravel. You just need to do the following for setting up authentication correctly in Laravel :
php artisan make:auth (this will create all the routes and view you need for authentication)
When a user is successfully authenticated, they will be redirected to the /home URI. You can customize the post-authentication redirect location by defining a redirectTo property on the LoginController, RegisterController, and ResetPasswordController:
protected $redirectTo = '/';
You can get the authenticated user in any controller by including the below code
use Illuminate\Support\Facades\Auth;
// Get the currently authenticated user...
$user = Auth::user();
// Get the currently authenticated user's ID...
$id = Auth::id();
You can check if user is authenticated or not by using the below code
use Illuminate\Support\Facades\Auth;
if (Auth::check()) {
// The user is logged in...
}
Add the below code to your Login Controller
public function username() {
return 'username';
}
if(filter_var($username, FILTER_VALIDATE_EMAIL)) {
//user sent their email
Auth::attempt(['email' => $username, 'password' => $password]);
} else {
//they sent their username instead
Auth::attempt(['username' => $username, 'password' => $password]);
}
You can read more about laravel authentication and its customization's at https://laravel.com/docs/5.4/authentication
As #Gaurav Roy mentioned, you must first create Laravel's authentication routes by typing on console php artisan make:auth. You will notice that in Controllers directory exists a directory named Auth with some Controllers in it. Open LoginController and override username() function and return the column you wish to authenticate with the password. In your case:
private $value = 'username';
public function username()
{
return $this->value;
}
Now override attemptLogin(Request $request) function and try to login with username or email:
protected function attemptLogin(Request $request)
{
// try to login with username
$loginWithUsername = $this->guard()->attempt(
$this->credentials($request), $request->has('remember')
);
// if credentials with username are not valid, try with email
if (!$loginWithUsername) {
// replace username with email
$this->value = 'email';
$this->username();
// add input value to email
$request['email'] = $request['username'];
unset($request['username']);
return $this->guard()->attempt(
$this->credentials($request), $request->has('remember')
);
}
return $loginWithUsername;
}
Now go to resources->views->auth, open login file and replace email's input so it can accept the username. For example:
From:
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
To:
<input id="username" type="text" class="form-control" name="username" value="{{ old('username') }}" required autofocus>
Now you can login with username or email!
If you want both username and email then in your LoginController you can try something like:
return property_exists($this, 'username') ? $this->username : 'email';
Now I've followed the Laravel documentation on how to allow usernames during authentication, but it takes away the ability to use the email. I want to allow users to use their username or email to login. How do I go about this?
I've added this code to the LoginController as per Laravel's Documentation and it only allows username for login. I want it to accept username or email for login.
public function username () {
return 'username';
}
I think a simpler way is to just override the username method in LoginController:
public function username()
{
$login = request()->input('login');
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$field => $login]);
return $field;
}
Follow instructions from this link: https://laravel.com/docs/5.4/authentication#authenticating-users
Then you can check for the user input like this
$username = $request->username; //the input field has name='username' in form
if(filter_var($username, FILTER_VALIDATE_EMAIL)) {
//user sent their email
Auth::attempt(['email' => $username, 'password' => $password]);
} else {
//they sent their username instead
Auth::attempt(['username' => $username, 'password' => $password]);
}
//was any of those correct ?
if ( Auth::check() ) {
//send them where they are going
return redirect()->intended('dashboard');
}
//Nope, something wrong during authentication
return redirect()->back()->withErrors([
'credentials' => 'Please, check your credentials'
]);
This is just a sample. THere are countless various approaches you can take to accomplish the same.
Open your LoginController.php file.
Add this reference
use Illuminate\Http\Request;
And override the credentials method
protected function credentials(Request $request)
{
$field = filter_var($request->get($this->username()), FILTER_VALIDATE_EMAIL)
? 'email'
: 'username';
return [
$field => $request->get($this->username()),
'password' => $request->password,
];
}
Successfully tested in Laravel 5.7.11
You need to override protected function attemptLogin(Request $request) method from \Illuminate\Foundation\Auth\AuthenticatesUsers Trait in your LoginController
i.e. in my LoginController class
protected function attemptLogin(Request $request) {
$identity = $request->get("usernameOrEmail");
$password = $request->get("password");
return \Auth::attempt([
filter_var($identity, FILTER_VALIDATE_EMAIL) ? 'email' : 'username' => $identity,
'password' => $password
]);
}
Your LoginController class should use Trait \Illuminate\Foundation\Auth\AuthenticatesUsers in order to override attemptLogin method i.e.
class LoginController extends Controller {
use \Illuminate\Foundation\Auth\AuthenticatesUsers;
.......
.......
}
I think its even more simple, just override the method from AuthenticatesUsers traits, credentials method in your LoginController. Here I have implemented to login with either email or phone. You can change it to fit your needs.
LoginController.php
protected function credentials(Request $request)
{
if(is_numeric($request->get('email'))){
return ['phone'=>$request->get('email'),'password'=>$request->get('password')];
}
return $request->only($this->username(), 'password');
}
This is the way I do it:
// get value of input from form (email or username in the same input)
$email_or_username = $request->input('email_or_username');
// check if $email_or_username is an email
if(filter_var($email_or_username, FILTER_VALIDATE_EMAIL)) { // user sent his email
// check if user email exists in database
$user_email = User::where('email', '=', $request->input('email_or_username'))->first();
if ($user_email) { // email exists in database
if (Auth::attempt(['email' => $email_or_username, 'password' => $request->input('password')])) {
// success
} else {
// error password
}
} else {
// error: user not found
}
} else { // user sent his username
// check if username exists in database
$username = User::where('name', '=', $request->input('email_or_username'))->first();
if ($username) { // username exists in database
if (Auth::attempt(['name' => $email_or_username, 'password' => $request->input('password')])) {
// success
} else {
// error password
}
} else {
// error: user not found
}
}
I believe there is a shorter way to do that, but for me this works and is easy to understand.
public function username()
{
//return ‘identity’;
$login = request()->input('identity');
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'phone';
request()->merge([$field => $login]);
return $field;
}
protected function validateLogin(Request $request)
{
$messages = [
'identity.required' => 'Email or username cannot be empty',
'email.exists' => 'Email or username already registered',
'phone.exists' => 'Phone No is already registered',
'password.required' => 'Password cannot be empty',
];
$request->validate([
'identity' => 'required|string',
'password' => 'required|string',
'email' => 'string|exists:users',
'phone' => 'numeric|exists:users',
], $messages);
}
https://dev.to/pramanadiputra/laravel-how-to-let-user-login-with-email-or-username-j2h
This solution of "Rabah G" works for me in Laravel 5.2. I modified a litle but is the same
$loginType = request()->input('useroremail');
$this->username = filter_var($loginType, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$this->username => $loginType]);
return property_exists($this, 'username') ? $this->username : 'email';
Thanks, this is the solution I got thanks to yours.
protected function credentials(Request $request) {
$login = request()->input('email');
// Check whether username or email is being used
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'user_name';
return [
$field => $request->get('email'),
'password' => $request->password,
'verified' => 1
];
}