Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scaffold authentication as default example.
- Loading branch information
1 parent
c672286
commit f2279c0
Showing
6 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('/'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('/'); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
34
database/migrations/2014_10_12_000000_create_users_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
database/migrations/2014_10_12_100000_create_password_reminders_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
|
||
} |
f2279c0
There was a problem hiding this comment.
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?
f2279c0
There was a problem hiding this comment.
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.
f2279c0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f2279c0
There was a problem hiding this comment.
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!