BigJobbies
BigJobbies

Reputation: 4043

Username/password Authentication Rules in Laravel

Currently, when a user logs into my Laravel app I use the following rules...

// Validation rules
$rules = array(
    'email' => 'required|email|exists:users,email',
    'password' => 'required'
);

What I'm looking for is a validation rule for checking the password against the user.

Upvotes: 3

Views: 4952

Answers (1)

Valery Viktorovsky
Valery Viktorovsky

Reputation: 6726

From docs:

if (Auth::attempt(array('email' => $email, 'password' => $password))) {
    return Redirect::intended('dashboard');
}

Example:

$userdata = array(
    'email'    => Input::get('email'),
    'password' => Input::get('password')
);

$rules = array(
    'email' => 'required|email|exists:users,email',
    'password' => 'required'
);

// Validate the inputs.
$validator = Validator::make($userdata, $rules);

// Check if the form validates with success.
if ($validator->passes()) {
    // Try to log the user in.
    if (Auth::attempt($userdata)) {
        // Redirect to homepage
        return Redirect::to('')->with('success', 'You have logged in successfully');
    } else {
        // Redirect to the login page.
        return Redirect::to('login')->withErrors(array('password' => 'Password invalid'))->withInput(Input::except('password'));
    }
}

Upvotes: 5

Related Questions