简体   繁体   中英

how to make custom login in laravel 5.6?

I have used my own custom login without using auth. Here is my code

 public function userLogin(Request $request){

  if($request->isMethod('post')){
    $data = $request->input();

    $this->validate($request, [
        'uemail'=> 'required|email',
        'user_type'=> 'required',
        'user_id' => 'required',
        'upassword' => 'required|min:6',
      ],
      [
        'uemail.email' => 'Email must be valid',
        'uemail.required' => 'Email is required',
        'user_type.required' => 'Type is required',
        'user_id.required' => 'Name is required',
        'upassword.required' => 'Password is required',
        'upassword.min' => 'Password must be at least 6 characters',
      ]);

      $user_type = $data['user_type'];
      $user_id = $data['user_id'];
      $uemail = $data['uemail'];
      $upassword = $data['upassword'];
      $hashPass = bcrypt($upassword);

      DB::enableQueryLog();

      $user1 =  User::where('type',$user_type)->where('user_id',$user_id)->where('email',$uemail)
      ->where('status',1)->where('deleted_at',null)->firstOrFail();

      $user =  DB::table('users')->where('type',$user_type)->where('user_id',$user_id)->where('email',$uemail)
      ->where('status',1)->where('deleted_at',null);

    //  $query = DB::getQueryLog();
     // $query = end($query);

      $isPasswordCorrect = Hash::check($upassword, $user1->password);

      if($user == null){
        echo "Failed"; die;
      }

       if($user->exists() && $isPasswordCorrect){
          echo "Success"; die;
          Session::put('userSession',$user1->email);
          Session::put('loginSession',$user_type);
          Session::put('idSession',$user1->user_id);

         return redirect('/user/dashboard');
    } else {
       return redirect('/user')->>with('flash_message_error','Invalid Login Credentials..');
    }

  }

    return view('death_notice.user_login');
}

This is my login function. But its not working. When the credentials is right it redirects to dashboard ie that's correct, but when the email or password or other credentials is wrong it showing error message. But when if the user enter such data which is not in the database then it must show message that user doesn't exists. but its going to page not found error...

I want to have the solution of this problem.

好的,首先确认,您在web.php中定义了类似“ / user”的路由

You haven't used get() in $user eloquent

$user =  DB::table('users')->where('type',$user_type)->where('user_id',$user_id)->where('email',$uemail)
  ->where('status',1)->where('deleted_at',null)->get();

And it shows 404 not found error because you have used firstorFail() it will fail on no record matching and will show 404 not found error so

$user1 =  User::where('type',$user_type)->where('user_id',$user_id)->where('email',$uemail)
  ->where('status',1)->where('deleted_at',null)->first();

if(empty($user1))
{
   echo "user doesn't exists";exit;
}

As per my understanding the query $user and $user1 is same so my suggestion would be to use only one

I made the following corrections:

  1. $data = $request; is sufficient, you don't need to do $data = $request->input();
  2. Check the count on the Eloquent query like this count($users->get()) , to check if user was found.

     public function userLogin(Request $request){ if($request->isMethod('post')){ $data = $request->input(); $this->validate($request, [ 'uemail'=> 'required|email', 'user_type'=> 'required', 'user_id' => 'required', 'upassword' => 'required|min:6', ], [ 'uemail.email' => 'Email must be valid', 'uemail.required' => 'Email is required', 'user_type.required' => 'Type is required', 'user_id.required' => 'Name is required', 'upassword.required' => 'Password is required', 'upassword.min' => 'Password must be at least 6 characters', ]); $user_type = $data['user_type']; $user_id = $data['user_id']; $uemail = $data['uemail']; $upassword = $data['upassword']; $hashPass = bcrypt($upassword); DB::enableQueryLog(); $user1 = User::where('type',$user_type)->where('user_id',$user_id)->where('email',$uemail) ->where('status',1)->where('deleted_at',null)->firstOrFail(); $users = DB::table('users')->where('type',$user_type)->where('user_id',$user_id)->where('email',$uemail) ->where('status',1)->where('deleted_at',null); // $query = DB::getQueryLog(); // $query = end($query); $isPasswordCorrect = Hash::check($upassword, $user1->password); if(count($users->get()) == 1){ $user1 = $users->first(); $isPasswordCorrect = Hash::check($upassword, $user1->password); echo "Success"; die; Session::put('userSession',$user1->email); Session::put('loginSession',$user_type); Session::put('idSession',$user1->user_id); return redirect('/user/dashboard'); } else { return redirect('/user')->>with('flash_message_error','Invalid Login Credentials..'); } } return view('death_notice.user_login'); } 

I have not tested the code, but the idea should be clear.

Use following code to custom login. it's for, Ajax login, you can modify according to your requirements.

function userLogin(Request $request)
{
    $auth = false;
    $credentials = $request->only('email', 'password');
    // if user login credentials are correct then users logged in 
    if (Auth::attempt($credentials, $request->has('remember')))
    {
        $auth = true; // Success
    }
    // returns json response if login credentials are correct.
    if ($auth == true)
    {
        return response()->json([
            'auth' => $auth,
            'intended' => URL::previous(),
            'msg'=>1,
            'name' =>  Auth::user()->name                
        ]);

    } else { // returns json response if login credentials are incorrect
        return response()->json(['msg' => 2]);            
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM