Laravel - Attempting auth - laravel

I'm trying to manually auth my user like this :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
use Log;
use Hash;
class UsersController extends Controller
{
public function authenticate(Request $request){
$email = $request->input('email');
$password = $request->input('password');
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication passed...
return response()->json(User::find($email));
}
return response()->json(null);
}
}
I verify my request data, my database data, all is ok. I've also done a Hash::check with my user password and my request password, all is ok.
But the attempt always returning false.
Thanks !

Finally, I found it.
This was because I was attending a result but
return response()->json(User::find($email));
didn't find a user. So I change it for the following
return response()->json(User::where('email', $email)->first());
Thanks !

Related

Admin Login in Laravel 8

How to set email and password to admin login using guards??
If I have to login for the 1st time in admin login portal what email and password is it going to verify with.
I tried adding record to database directly and logging in but that doesn't work.
when i try to login with email and password in the database, i get the following error
Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Models\Admin given, called in C:\xampp\htdocs\Alumni datatable - Copy (2) - Copy\vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php on line 434
AdminAuthController
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class AdminAuthController extends Controller
{
public function getLogin(){
return view('admin.auth.login');
}
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
if(auth()->guard('admin')->attempt(['email' => $request->input('email'), 'password' => $request->input('password')])){
$user = auth()->guard('admin')->user();
if($user->is_admin == 1){
return redirect()->route('adminDashboard')->with('success','You are Logged in sucessfully.');
}
}else {
return back()->with('error','Whoops! invalid email and password.');
}
}
public function adminLogout(Request $request)
{
auth()->guard('admin')->logout();
Session::flush();
Session::put('success', 'You are logout sucessfully');
return redirect(route('adminLogin'));
}
}
you have to extends Authenticatable in your Admin Model
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
}

Giving controller error on query line. Laravel

This is my controller's code, where i am using a query to find an email address from the table "user" against an email address that comes from a form. But it gives the error on the query's line "App\Http\Controllers\user".
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\usern;
use App\Models\post;
use Illuminate\Support\Facades\DB;
class homeController extends Controller{
public function userLogin(Request $request) {
$data= $request->input();
$userEmail= user::where('Email',$request->email)->first;
echo $userEmail;
}
}
Why you're using $request->input() ? What you can do to successfully create a login function is by attempting to log in through eloquent function
public function login(Request $request){
$validate = $this->validate($request, [
'email' => 'required|email|max:255',
'password' => 'required|min:5',
]);
$credentials = $request->only('email', 'password');
if (Auth::guard('admin')->attempt($credentials)) {
$session = $request->session()->regenerate();
return Redirect::route('admin.dashboard')->with('success','Login success.');
}
return back()->with('error','The provided credentials do not match our records.');
}
This will check if the user's email address is valid or not and if the credentials are not correct it'll return back with error that these credentials don't match.
Import correct class use App\Models\user; not use App\Models\usern;
and first letter of class name should be uppercase.

How to remove Laravel Auth Hashing (to replace it by mysql hashing)?

I added registration, and I don't want to using laravels hash but mysql Hash (because I want existing users to still be able to connect).
So i do it step by step and for now I just try to register and then login without any hashing. The credentials are correct in my table but I get
"message":"The given data was invalid.","errors":{"email":["These credentials do not match our records."]}
I tried setting it in LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
public function username()
{
return 'email';
}
public function password()
{
return 'email';
}
public function setPasswordAttribute($password){
$this->attributes['password'] = $password;
}
public function Login(Request $request)
{
if(Auth::attempt(['email' => $request->email, 'pwd' => $request->password, 'password' => $request->password])){
$user = Auth::user();
$username = $user->nom;
return response()->json([
'status' => 'success',
'user' => $username,
]);
} else {
return response()->json([
'status' => 'error',
'user' => 'Unauthorized Access'
]);
}
}
}
I guess I should overwrite another function, but can't find out which one.
Could you please give me some help?
Altough what you're trying to achieve is considered unsecure, to remove Laravel's hashing for password, you need to add this to your User model :
public function setPasswordAttribute($password){
$this->attributes['password'] = $password;
}
and not in your controller, and be sure to remove the brcypt() methods in your RegisterController
To add your MySQL own hashing methods, update your controller to insert a RAW query while creating a user upon registration

Laravel custom login losses Session after redirect

i need a two way Login. First check database one if user exists and if not check database two.
So i build a custom Login Controller:
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Redirect;
use App\Http\Controllers\Controller;
class CustomLoginController extends Controller
{
public function login(Request $request)
{
if($request->email) {
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
return redirect()->intended('/');
} else {
DB::setDefaultConnection('otherdb');
if (Auth::attempt($credentials)) {
// The Login credentials will be found and user will be logged in
but after Redirect to home user isn't logged in anymore.
return redirect()->intended('/');
} else {
return redirect()->to('/login')
->withInput($request->only($credentials['email'], 'remember'))
->withErrors([
'email' => Lang::get('auth.failed'),
]);
}
}
} else {
return view('auth.login');
}
}
}
After i changed the database Connection with "DB::setDefaultConnection('otherdb');", the second login works but after Redirect to any page user isn't logged in anymore.
What am I doing wrong?
Any ideas?
Laravel trying to find a user in DB in every request. So after redirect there in no user in your default database. DB connection doesn't stored in session.
I think you need to create a custom auth guard with another user model like
class OtherUser extends Eloquent
{
protected $connection = 'otherdb';
}
and work with it.

Custom login laravel 5.5

I have problem with custom login laravel. This is code for authenticate.
This code don't work. have you idea?
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\Users;
use DB;
class LoginController extends Controller {
public function dologin(Request $request){
$email = $request->input('u_email');
$password = $request->input('pwd1');
// Check validation
$checkLogin = DB::table('users')->where(['u_email'=>$email,'password'=>$password])->get();
if(count($checkLogin) >0){
echo "Login SuccessFull<br/>";;
}else{
echo "Login Faield Wrong Data Passed";
}
}
}
You can't do that because passwords are hashed in Laravel. Use the attempt() method instead:
// Check validation
if (auth()->attempt(['email' => $email, 'password' => $password])) {
echo "Login SuccessFull<br/>";;
} else {
echo "Login Failed Wrong Data Passed";
}

Resources