Skip to content

Instantly share code, notes, and snippets.

@svenluijten
Last active January 7, 2016 11:13
Show Gist options
  • Save svenluijten/9679f5d6e7940147dce6 to your computer and use it in GitHub Desktop.
Save svenluijten/9679f5d6e7940147dce6 to your computer and use it in GitHub Desktop.
My authentication
<?php
/**
* Log a player in.
*
* @param LoginPlayerRequest $request
* @return \Illuminate\Http\Response
*/
public function auth(LoginPlayerRequest $request)
{
// The user supplied the wrong credentials.
if ( ! $this->authenticatePlayer($request)) {
return $this->authenticationFailed();
}
// Authentication successful!
return $this->authenticationSuccessful();
}
/**
* Log the player out.
*
* @return Illuminate\Http\Response
*/
public function logout()
{
$this->auth->logout();
return redirect()->route('index');
}
/**
* Attempt to authenticate the player.
*
* @param \App\Http\Requests\LoginPlayerRequest $request
* @return bool
*/
protected function authenticatePlayer($request)
{
// The user authorized with their username.
$authWithUsername = $this->auth->attempt([
'username' => $request->get('user'),
'password' => $request->get('password'),
], $request->remember);
// The user authorized with their email.
$authWithEmail = $this->auth->attempt([
'email' => $request->get('user'),
'password' => $request->get('password'),
], $request->get('remember'));
return (bool) $authWithEmail || $authWithUsername;
}
/**
* Redirect the player back to the login page with a message.
*
* @return \Illuminate\Http\Response
*/
protected function authenticationFailed()
{
return redirect()->route('auth.login')->withInput()->with('message', [
'title' => 'Whoopsie!',
'body' => 'Couldn\'t log you in with that username &amp; password combination. Please try again!',
'severity' => 'warning',
]);
}
/**
* Redirect the player upon successful authentication.
*
* @return \Illuminate\Http\Response
*/
protected function authenticationSuccessful()
{
return redirect()->intended()->with('message', [
'title' => 'Success!',
'body' => 'You were logged in. Feel free to create a new character or edit an existing one.',
'severity' => 'success',
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment