Commit f2279c02 authored by Taylor Otwell's avatar Taylor Otwell

Scaffold authentication as default example.

parent c6722869
<?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('/');
}
}
<?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('/');
}
}
}
<?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;
}
}
<?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;
}
}
<?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');
}
}
<?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');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment