Skip to content

Commit

Permalink
Scaffold authentication as default example.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 12, 2014
1 parent c672286 commit f2279c0
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 0 deletions.
107 changes: 107 additions & 0 deletions app/Http/Controllers/Auth/AuthController.php
@@ -0,0 +1,107 @@
<?php namespace App\Http\Controllers\Auth;

use Illuminate\Contracts\Auth\Authenticator;

use App\Http\Requests\Auth\LoginRequest;
use App\Http\Requests\Auth\RegisterRequest;

/**
* @Middleware("csrf")
* @Middleware("guest", except={"logout"})
*/
class AuthController {

/**
* The authenticator implementation.
*
* @var Authenticator
*/
protected $auth;

/**
* Create a new authentication controller instance.
*
* @param Authenticator $auth
* @return void
*/
public function __construct(Authenticator $auth)
{
$this->auth = $auth;
}

/**
* Show the application registration form.
*
* @Get("auth/register")
*
* @return Response
*/
public function showRegistrationForm()
{
return view('auth.register');
}

/**
* Handle a registration request for the application.
*
* @Post("auth/register")
*
* @param RegisterRequest $request
* @return Response
*/
public function register(RegisterRequest $request)
{
// Registration form is valid, create user...

$this->auth->login($user);

return redirect('/');
}

/**
* Show the application login form.
*
* @Get("auth/login")
*
* @return Response
*/
public function showLoginForm()
{
return view('auth.login');
}

/**
* Handle a login request to the application.
*
* @Post("auth/login")
*
* @param LoginRequest $request
* @return Response
*/
public function login(LoginRequest $request)
{
if ($this->auth->attempt($request->only('email', 'password')))
{
return redirect('/');
}

return redirect('/login')->withErrors([
'email' => 'The credentials you entered did not match our records. Try again?',
]);
}

/**
* Log the user out of the application.
*
* @Get("auth/logout")
*
* @return Response
*/
public function logout()
{
$this->auth->logout();

return redirect('/');
}

}
114 changes: 114 additions & 0 deletions app/Http/Controllers/Auth/RemindersController.php
@@ -0,0 +1,114 @@
<?php namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\PasswordBroker;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* @Middleware("csrf")
* @Middleware("guest")
*/
class RemindersController {

/**
* The password reminder implementation.
*
* @var PasswordBroker
*/
protected $passwords;

/**
* Create a new password reminder controller instance.
*
* @param PasswordBroker $passwords
* @return void
*/
public function __construct(PasswordBroker $passwords)
{
$this->passwords = $passwords;
}

/**
* Display the password reminder view.
*
* @Get("password/remind")
*
* @return Response
*/
public function showReminderForm()
{
return view('password.remind');
}

/**
* Handle a POST request to remind a user of their password.
*
* @Post("password/remind")
*
* @param Request $request
* @return Response
*/
public function sendPasswordResetEmail(Request $request)
{
switch ($response = $this->passwords->remind($request->only('email')))
{
case PasswordBroker::INVALID_USER:
return redirect()->back()->with('error', trans($response));

case PasswordBroker::REMINDER_SENT:
return redirect()->back()->with('status', trans($response));
}
}

/**
* Display the password reset view for the given token.
*
* @Get("password/reset")
*
* @param string $token
* @return Response
*/
public function showPasswordResetForm($token = null)
{
if (is_null($token))
{
throw new NotFoundHttpException;
}

return view('password.reset')->with('token', $token);
}

/**
* Handle a POST request to reset a user's password.
*
* @Post("password/reset")
*
* @param Request $request
* @return Response
*/
public function resetPassword(Request $request)
{
$credentials = $request->only(
'email', 'password', 'password_confirmation', 'token'
);

$response = $this->passwords->reset($credentials, function($user, $password)
{
$user->password = bcrypt($password);

$user->save();
});

switch ($response)
{
case PasswordBroker::INVALID_PASSWORD:
case PasswordBroker::INVALID_TOKEN:
case PasswordBroker::INVALID_USER:
return redirect()->back()->with('error', trans($response));

case PasswordBroker::PASSWORD_RESET:
return redirect()->to('/');
}
}

}
29 changes: 29 additions & 0 deletions app/Http/Requests/Auth/LoginRequest.php
@@ -0,0 +1,29 @@
<?php namespace App\Http\Requests\Auth;

use Illuminate\Foundation\Http\FormRequest;

class LoginRequest extends FormRequest {

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required', 'password' => 'required',
];
}

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

}
30 changes: 30 additions & 0 deletions app/Http/Requests/Auth/RegisterRequest.php
@@ -0,0 +1,30 @@
<?php namespace App\Http\Requests\Auth;

use Illuminate\Foundation\Http\FormRequest;

class RegisterRequest extends FormRequest {

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|email|unique:users',
'password' => 'required|confirmed|min:8',
];
}

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

}
34 changes: 34 additions & 0 deletions database/migrations/2014_10_12_000000_create_users_table.php
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('password', 60);
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}

}
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePasswordRemindersTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('password_reminders', function(Blueprint $table)
{
$table->string('email')->index();
$table->string('token')->index();
$table->timestamp('created_at');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('password_reminders');
}

}

4 comments on commit f2279c0

@jenssegers
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know how I feel about this. I think a lot of people use Sentry/Entrust/... because the support for permissions and roles. Do you think you will add roles/permissions to Laravel? Maybe using a first party package?

@devonzara
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how this is any different than the HomeController, InspireCommand, .env.example and the default Middleware... They're just examples, you don't have to use them. However, they give insight into how things work and provide a nice boilerplate to get started if you do wish to use them.

@taylorotwell
Copy link
Member Author

@taylorotwell taylorotwell commented on f2279c0 Oct 12, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedroborges
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I guess the fresh command will help a lot experienced developers to setup Laravel the way they want while offering newcomers a good starting point. Great solution!

Please sign in to comment.