Anonymous
05 Mar, 2018

How To Create A Custom Login System In Laravel?

1 Answer         2807 Views

Jiwan Thapa
05 Mar, 2018

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.


61 Likes         0 Dislike         0 Comment        


Leave a comment