How To Create A Custom Login System In Laravel?

Asked by Anonymous on 05 Mar 2018 1672 Views

Anonymous on 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.


You must be logged in to answer a question. or