Moe default requests.
This commit is contained in:
104
app/Http/Controllers/Auth/AuthController.php
Normal file
104
app/Http/Controllers/Auth/AuthController.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\User;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use App\Http\Requests\Auth\LoginRequest;
|
||||
use App\Http\Requests\Auth\RegisterRequest;
|
||||
|
||||
class AuthController extends Controller {
|
||||
|
||||
/**
|
||||
* The Guard implementation.
|
||||
*
|
||||
* @var Guard
|
||||
*/
|
||||
protected $auth;
|
||||
|
||||
/**
|
||||
* Create a new authentication controller instance.
|
||||
*
|
||||
* @param Guard $auth
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Guard $auth)
|
||||
{
|
||||
$this->auth = $auth;
|
||||
|
||||
$this->middleware('guest', ['except' => 'getLogout']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the application registration form.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function getRegister()
|
||||
{
|
||||
return view('auth.register');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a registration request for the application.
|
||||
*
|
||||
* @param RegisterRequest $request
|
||||
* @return Response
|
||||
*/
|
||||
public function postRegister(RegisterRequest $request)
|
||||
{
|
||||
$user = User::forceCreate([
|
||||
'name' => $request->name,
|
||||
'email' => $request->email,
|
||||
'password' => bcrypt($request->password),
|
||||
]);
|
||||
|
||||
$this->auth->login($user);
|
||||
|
||||
return redirect('/dashboard');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the application login form.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function getLogin()
|
||||
{
|
||||
return view('auth.login');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a login request to the application.
|
||||
*
|
||||
* @param LoginRequest $request
|
||||
* @return Response
|
||||
*/
|
||||
public function postLogin(LoginRequest $request)
|
||||
{
|
||||
$credentials = $request->only('email', 'password');
|
||||
|
||||
if ($this->auth->attempt($credentials, $request->has('remember')))
|
||||
{
|
||||
return redirect('/dashboard');
|
||||
}
|
||||
|
||||
return redirect('/auth/login')
|
||||
->withInput($request->only('email'))
|
||||
->withErrors([
|
||||
'email' => 'These credentials do not match our records.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the user out of the application.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function getLogout()
|
||||
{
|
||||
$this->auth->logout();
|
||||
|
||||
return redirect('/');
|
||||
}
|
||||
|
||||
}
|
||||
132
app/Http/Controllers/Auth/PasswordController.php
Normal file
132
app/Http/Controllers/Auth/PasswordController.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\User;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use Illuminate\Contracts\Auth\PasswordBroker;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class PasswordController extends Controller {
|
||||
|
||||
/**
|
||||
* The Guard implementation.
|
||||
*
|
||||
* @var Guard
|
||||
*/
|
||||
protected $auth;
|
||||
|
||||
/**
|
||||
* The password broker implementation.
|
||||
*
|
||||
* @var PasswordBroker
|
||||
*/
|
||||
protected $passwords;
|
||||
|
||||
/**
|
||||
* Create a new password controller instance.
|
||||
*
|
||||
* @param PasswordBroker $passwords
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Guard $auth, PasswordBroker $passwords)
|
||||
{
|
||||
$this->auth = $auth;
|
||||
$this->passwords = $passwords;
|
||||
|
||||
$this->middleware('guest');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the form to request a password reset link.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function getEmail()
|
||||
{
|
||||
return view('auth.password');
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a reset link to the given user.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function postEmail(Request $request)
|
||||
{
|
||||
$this->validate($request, ['email' => 'required']);
|
||||
|
||||
switch ($response = $this->passwords->sendResetLink($request->only('email')))
|
||||
{
|
||||
case PasswordBroker::INVALID_USER:
|
||||
return redirect()->back()->withErrors(['email' =>trans($response)]);
|
||||
|
||||
case PasswordBroker::RESET_LINK_SENT:
|
||||
return redirect()->back()->with('status', trans($response));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the password reset view for the given token.
|
||||
*
|
||||
* @param string $token
|
||||
* @return Response
|
||||
*/
|
||||
public function getReset($token = null)
|
||||
{
|
||||
if (is_null($token))
|
||||
{
|
||||
throw new NotFoundHttpException;
|
||||
}
|
||||
|
||||
return view('auth.reset')->with('token', $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the given user's password.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function postReset(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()
|
||||
->withInput($request->only('email'))
|
||||
->withErrors(['email' => trans($response)]);
|
||||
|
||||
case PasswordBroker::PASSWORD_RESET:
|
||||
return $this->loginAndRedirect($request->email);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Login the user with the given e-mail address and redirect home.
|
||||
*
|
||||
* @param string $email
|
||||
* @return Response
|
||||
*/
|
||||
protected function loginAndRedirect($email)
|
||||
{
|
||||
$this->auth->login(User::where('email', $email)->firstOrFail());
|
||||
|
||||
return redirect('/dashboard');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user