How to create a custom login system in Laravel?

Create a table for users as you want or you can use the same table as well which has all the required fields already there.

Create method to add, edit and delete users. it's better to create a separate controller file for that one.

Use hash::make() function to encrypt the password value for security.

The login function can be written as shown here.

public function userlogin(){
    
    $data = Input::all();
    $user = $data['username'];
    $password = $data['password'];
    
    if($data = DB::table('users')->where('name',$user)->first()){

    	$record = $data->password;

	   	if(hash::decrypt($record,$password)){
	   		session::put('user',$user);
	   		return redirect('/home');
	   	}else{
	   		session::flash('success','Invalid User Credentials');
	       	return redirect()->back()->with('message');
	   	}
    }else{
    	session::flash('success','Invalid User Credentials');
	    return redirect()->back()->with('message');
    }
}	

Once this userlogin method is created, you can check for the user name in session before processing any of the methods.

public function admin(){
    if(session()->get('user')){    		
    	return view ('backend.index');        
    }else{
    	return redirect('login');
    }
}	

You can also set remember cookie for the user using if condition once the user is verified. For more details you can check AUTHENTICATION OOP page for more details.

Send Your Answer

The comment/answer feature from this section has been removed. Please visit our new section How to create a custom login system in Laravel? to add comments or answer. Thankyou.