1
votes

I'm very new to Laravel and PHP 5.4, coming from CI. Please forgive this stupid question about very basic authentication.

I am beginning by implementing login/registration (authentication), following the docs.

My migration is in place:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('email')->unique();
    $table->string('password', 60);
    $table->string('first_name');
    $table->string('last_name');
    $table->rememberToken();
    ...
});

Here's my config\auth.php:

'driver' => 'eloquent',
'model' => App\User::class,
'table' => 'users',
'password' => [
    'email' => 'emails.password',
    'table' => 'password_resets',
    'expire' => 60,
],

app\User.php:

protected $table = 'users';
protected $fillable = ['first_name', 'last_name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];

app\Http\Controllers\Auth\AuthController.php:

protected function validator(array $data) {
    return Validator::make($data, [
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ]);
}

protected function create(array $data) {
   return User::create([
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

public function getLogin() {
    return view('admin/login');
}

public function getRegister() {
    return view('admin/register');
}

app\Http\routes.php:

Route::get('admin/login', 'Auth\AuthController@getLogin');
Route::post('admin/login', 'Auth\AuthController@postLogin');
Route::get('admin/register', 'Auth\AuthController@getRegister');
Route::post('admin/register', 'Auth\AuthController@postRegister');

My views are displaying properly:

resources/views/admin/login.blade.php
resources/views/admin/register.blade.php

with forms:

<form action="/admin/login" method="POST">
<form action="/admin/register" method="POST">

This is as far as I get. The Register form, when submitted, just redirects to itself, no errors. No entries are created in the users database table.

What am I missing?

EDIT

I don't think I need to add a postRegister() because it's already defined in the RegistersUsers trait used by AuthenticatesAndRegistersUsers used by AuthController.

Also, not sure if this helps but I have 2 virtual hosts pointing to the same project directory.

3
Have you checked application logs ? you can check the application logs in storage/logs/laravel.log, make sure you have set error_reporting(E_ALL), also check server logs - Ganesh Ghalame
Should probably add your postRegister() method considering thats the method your posting to.. or are you using the defaults? Also, check the network tab of your browsers developer tools, should tell you the response you got from the post request, which should hint at why you were redirected. - Jeemusu
@GaneshGhalame I cleared the laravel.log and resubmitted the form. Nothing in the logs. - Obay
@Jeemusu I checked the Network tab, it seems the form is submitted using GET instead of POST, even though the <form action="/admin/register" method="POST">. Any idea why this is so? - Obay
@Jeemusu I just found that it actually does a POST, but status is 302 Found. Then it makes the GET - Obay

3 Answers

4
votes

It should be definitively a validation error.

Put this code in your register view and try again:

<!--  Error handle -->
        @if($errors->any())
        <div class="row collapse">
            <ul class="alert-box warning radius">
                @foreach($errors->all() as $error)
                    <li> {{ $error }} </li>
                @endforeach
            </ul>
        </div>
        @endif
0
votes

You should add postRegister to your AuthController:

public function postRegister(Request $request) {
    $this->validate($request, [
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ]);

    return $this->create($request->input());
}

Also note that the validator function isn't really required. In 5.1, a Controller object has the validate method, which will return failures automatically.

Oh, and you should probably add postLogin, too. I'll leave that as an exercise for you.

0
votes

With a basic installation, a validation rule is running when registering. With no further error messages, it's a little confusing.

It's the password validation rule in AuthController.php, that wants a password length of minimum 6 characters:

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ]);
}

So just make sure you enter 6 or more characters :)