Question

I'm new to Laravel, Trying to make a very simple login form.

This form has a "Remember Me" Check box. I tried to implement its functionality using Cookie::make() but it turned out that i need to return a Response in order to save it.

When i check cookies stored from localhost in my browser, I don't find a cookie named username. I've did some research and it turned out that i must attach the cookie into a Response and then return it.

The problem is, I don't want to return a Response !!

I haven't reached Auth class yet in my learning process. So a solution without using this class would be more suitable.

Here's my code :

public function processForm(){
    $data = Input::all();
    if($data['username'] == "rafael" & $data['password'] == "123456"){
        if(Input::has('rememberme')){
            $cookie = Cookie::make('username', $data['username'], 20);
        }
        Session::put('username', $data['username']);
        return Redirect::to('result');
    } else {
        $message_arr = array('message' => 'Invalid username or password!');
        return View::make('signup', $message_arr);
    }
}

My signup.blade.php :

@extends('layout')

@section('content')
    @if(isset($message))
        <p>Invalid username or password.</p>
    @endif
    <form action="{{ URL::current() }}" method="post">
        <input type="text" name="username"/>
        <br>
        <input type="text" name="password"/>
        <br>
        <input type="checkbox" name="rememberme" value="true"/>
        <input type="submit" name="submit" value="Submit" />
    </form>
@stop

routes.php :

Route::get('signup', 'ActionController@showForm');

Route::post('signup', 'ActionController@processForm');

Route::get('result', 'ActionController@showResult');
Was it helpful?

Solution

You should check Laravel 4's documentation on authenticating users, which can be found at:

http://laravel.com/docs/security#authenticating-users

Basically you can authenticate a user by passing $data to Auth::attempt(). Pass true as the second parameter to Auth::attempt() to remember the user for future logins:

$data = Input::all();

if (Auth::attempt($data, ($data['rememberme'] == 'on') ? true : false)
    return Redirect::to('result');
else
{
    $message_arr = array('message' => 'Invalid username or password!');
    return View::make('signup', $message_arr);
}

You should use Laravel's method for authentication, as it takes care of password reminders and more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top