How To Create A Custom Login System In Laravel?

Asked 8 years ago Modified 13 hours ago 3704 Views
0
Anonymous Post Date: Mar 05, 2018

Answers (1)

1

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.

Jiwan Thapa Answered: Mar 05, 2018
Comments

Sign in to help the community by answering this question.

Log In to Answer