Laravel Auth::attempt() is not working? - laravel

I have added my code and problem..no one is available there to respond me .. please help me to solve this !
http://laravel.io/forum/10-25-2015-laravel-5120-lts-fails-to-authenticate
I dono what's wrong with laravel,
I have the following functions in same controller
public function signup()
{
$user=new User();
$user->email=Input::get('email');
$user->password=Hash::make(Input::get('Password'));
$user->username=$username;
$user->save();
// self authenticating user
Auth::attempt(['email'=>Input::get('email'),'password'=>Input::get('Password')],true);
return Auth::user();
}
the above Auth::attempt() is working fine but the next method to this signup is
public function login()
{
if( Auth::attempt(['email'=>Input::get('email'),'password'=>Input::get('password')],true))
{
return Auth::user();
}
else
{
echo "failed";
}
}
here the Auth is not working i dono y.. no errors with form

Did you make sure to give your inputs a name attribute? Laravels Input/Request Facades accesses them by the name, not id or class.
Have you tried the default AuthController ?

Related

I have problem when i logout from admin "Trying to get property 'id' of non-object" using laravel

I am trying to logout from admin but unfortunately, I face error Trying to get property 'id' of non-object How to fix this error? please help me thanks.
public function index(){
$user_permission = Users_Permissions::with('user')
->Where('user_id',Auth::user()->id)
->paginate(5);
return view('index',compact('user_permission'));
}
The problem is clear. When you log out, then Auth::user() is null. so there is no id . You can solve the issue like this.
public function index()
{
if (Auth::check()) {
$user_permission = Users_Permissions::with('user')->Where('user_id',Auth::user()->id)
->paginate(5);
return view('index',compact('user_permission'));
} else {
// The condition when no user logged in
// For an example
return redirect('login'); // This is just an example
}
For that, use try & catch in every function, and use auth middleware for specific routes while you are working with auth users.
so that specific routes are required to use auth, so you don't need to check in every function. auth is always present.
Route::middleware('auth')->group(function () {
Route::get('logout', 'AuthController#logout');
});
or else you can use middleware in a controller too,
class AuthController extends Controller
{
public function __construct(){
$this->middleware(['guest'])->except('logout');
}
}
It's because after logout you don't have user ID, so in this situation the User is null, and you want to get the if from null object. you can change this part of code like below
Users_Permissions::with('user')->Where('user_id',Auth::user()->id ?? 0)
then you pass the error. you can put every number or null in query based on your needs.

Laravel Nova how to overwrite the nova LoginController

My project requires a username rather than email. I had this working in Laravel 5.8 and Nova v2.1.0. After upgrading to L 6.x N 2.6.1 everything broke. So I started over with clean L 6.x and N 2.6.1 install.
Now I want to customize the login but I do not want to edit any Nova Package scripts as before.
I've added this code to nova/Http/Controllers/LoginController.php and all works as expected.
public function username()
{
return 'username';
}
When I add the code to App/Nova/Http/Controller/LoginController.php (a copy of the original) the login still requires an email address. Or is using the original file in nova.
this is what i do on my end
i override the App\Http\Controllers\Auth\LoginController.php from
class LoginController extends Controller
to
class LoginController extends \Laravel\Nova\Http\Controllers\LoginController
if you want to use username or email on the login page you have to add this.
this method will determine how the user input they credential
public function username()
{
$login = \request()->input("email");
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
\request()->merge([$field => $login]);
return $field;
}
because the user can login using email or username but the default login from nova only have 1 input box. have to add this to display if the user input wrong username or that username did not exist
protected function sendFailedLoginResponse(Request $request)
{
throw ValidationException::withMessages([
'email' => [trans('auth.failed')],
]);
}
on my controller i have add other method to determine if the user is admin or able to access the backend or if the user is still active.
protected function authenticated(Request $request, $user)
{
if($user->isSuperAdmin()) {
return redirect(config('nova.path'));
}
if($user->can('backend')) {
return redirect(config('nova.path'));
}
return redirect('/');
}
by adding method to check user is active i need to add this method to check if the user can login
private function activeUser($username)
{
$user = User::where($this->username(), $username)->first();
if($user) {
return $user->active;
}
return false;
}
public function login(Request $request)
{
$active = $this->activeUser($request->only($this->username()));
if(! $active) {
return $this->sendFailedLoginResponse($request);
}
return parent::login($request);
}
hope this helps

how do i use index_number and date_of_birth for frontend login in laravel with the user having to register first

I am creating a student project in Laravel. I have created all my models including the student model. I want to use the student_number(index_number) and the students date_of_birth from the student model as login credential for the front end view instead of the default user login(email and password). I do not want users to register before they can login as in the default laravel welcome page.
There are so many tricks to do this.
Laravel ships with Authentication
In your Login Controller you can do this:
public function login() {
$user = User::where('index_number', request('index_number'))->first();
if($user->birthday != request('birthday')) {
return 'Invalid Credentials';
}
\Auth::login($user);
return 'login success'
}
Try Auth::attempt() method,
pass your credentials same as your user table.
for example;
public function login(Request $request)
{
$credentials = ['student_number' => $request->student_number,'date_of_birth' => $request->date_of_birth,'active' => 'Y'];//Database to check in User table
if (Auth::attempt($credentials)) {
return redirect('student/dashboard');
}
else
{
Session::flash('mess', "Invalid Credentials , Please try again.");
return redirect()->back();
}
}
Don't forgot to use
use Auth;
Hope this works

How to print Last Executed Query on controller in Laravel 5.4?

I have created following method to verify admin in
AdminModel.php
public function verifyAdmin($where){
$admin = AdminModel::where($where)->get();
if($admin->isEmpty()){
$emptyArray=array();
return $emptyArray;
}else{
return $admin;
}
}
Now this model's method is used in controller as follows AdminController.php
public function adminLogin(AdminLoginRequest $request)
{
$adminModel = new AdminModel();
$email=$request->email;
$password=$request->password;
$where=array('email'=>$email,'password'=>md5($password));
$getUser=$adminModel->verifyAdmin($where);
if(!empty($getUser)){
session()->put('email',$email);
return redirect('admin-dashboard');
}else{
return redirect('admin')->with('error', 'Invalid email or password !');
}
}
Now I want to echo last executed query in the controller after the models method call .
How can I do this please suggest me, I am using Laravel 5.4

Codeigniter access function in different controller

I am pretty new to codeigniter. I am facing an inconvenient issue. I have code like this in profile.php (controller):
public function edit()
{
$post = $this->input->post();
if($post)
{
//checking session username (if logged in)
if(isset($this->session->userdata('username') && !empty($this->session->userdata('username')))
{
}else{
/***************************REFERENCE DIFFERENT CONTROLLER HERE*********/
$this->load->view('login'); //should I write membership/index?
}
}else{
}
}
So I check if user logged in, if yes, code executes. if not, I wish to redirect to a function in different controller i.e. membership.php and it is the index() function of that controller. How can I reference that?
use redirect("membership/index"); for redirection in codeigniter
Use redirect variable
public function edit()
{
$post = $this->input->post();
if($post)
{
//checking session username (if logged in)
if(isset($this->session->userdata('username') && !empty($this->session->userdata('username')))
{
redirect('membership');
}else{
/***************************REFERENCE DIFFERENT CONTROLLER HERE*********/
$this->load->view('login'); //should I write membership/index?
redirect('membership'); // you can use this where u want to refrence/redirect
}
}else{
}
}

Resources