Skip to content

Instantly share code, notes, and snippets.

@TheMadav
Created May 21, 2014 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheMadav/a1d541257feb818df48d to your computer and use it in GitHub Desktop.
Save TheMadav/a1d541257feb818df48d to your computer and use it in GitHub Desktop.
User Controller for Laravel, using Sentry
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
Route::get('/', function()
{
return View::make('default');
});
Route::get('account/logout', array('as' => 'logout', 'uses' => 'UserController@getLogout'));
Route::get('account/login', array('as' => 'login', 'uses' => 'UserController@getLogin'));
Route::post('account/login', array('as' => 'login.post', 'uses' => 'UserController@postLogin'));
Route::get('account/register', array('as' => 'register', 'uses' => 'UserController@getRegister'));
Route::post('account/register', array('as' => 'register.post', 'uses' => 'UserController@postRegister'));
Route::get('/account/activate/{id}/{ac}', array('as' => 'activate', 'uses' => 'UserController@getActivate'));
Route::post('/account/requestReset/', array('as' => 'requestReset.post', 'uses' => 'UserController@postRequestPasswordReset'));
Route::get('/account/reset/{id}/{resetCode}', array('as' => 'reset', 'uses' => 'UserController@getResetPassword'));
Route::post('/account/reset/', array('as' => 'reset.post', 'uses' => 'UserController@postResetPassword'));
<?php
use Auth, BaseController, Form, Input, Redirect, Sentry, View;
class UserController extends BaseController {
/**
* Display the login page
* @return View
*/
public function getLogin()
{
return View::make('user.login');
}
/**
* Login action
* @return Redirect
*/
public function postLogin()
{
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
try
{
$user = Sentry::authenticate($credentials, false);
if ($user)
{
return Redirect::route('/');
}
}
catch(\Exception $e)
{
return Redirect::route('login')->withErrors(array('login' => $e->getMessage()));
}
}
/**
* Logout action
* @return Redirect
*/
public function getLogout()
{
Sentry::logout();
return Redirect::route('login');
}
public function getRegister(){
return View::make('user.register');
}
public function postRegister()
{
try
{
// Let's register a user.
$user = Sentry::register(array(
'email' => Input::get('email'),
'password' => Input::get('password')
));
// Let's get the activation code
$data['user'] = $user;
// Send activation code to the user so he can activate the account
Mail::send('emails.auth.welcome', $data , function($message) use ($user)
{
$message->to($user->email)->subject('Welcome!');
});
}
catch(\Exception $e)
{
return Redirect::route('register')->withErrors(array('register' => $e->getMessage()));
}
return Redirect::route('login')->with('info', 'Activation pending');
}
public function getActivate($id = null, $ac = null)
{
try
{
// Find the user using the user id
$user = Sentry::findUserById( $id );
// Attempt to activate the user
if ($user->attemptActivation( $ac ) )
{
return Redirect::route('login')->with('success', 'Activation succesful, please login');
}
else
{
return Redirect::route('login')->with('error', 'Activation failed, please try again');
}
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
return Redirect::route('register')->withErrors(array('register' => $e->getMessage()));
}
catch (Cartalyst\Sentry\Users\UserAlreadyActivatedException $e)
{
return Redirect::route('login')->with('warning', 'Error already active');
}
}
public function postRequestPasswordReset()
{
try
{
// Find the user using the user email address
$user = Sentry::findUserByLogin( Input::get('email') );
// Get the password reset code
$data['user'] = $user;
// Now you can send this code to your user via email for example.
Mail::send('emails.auth.reset', $data , function($message) use ($user)
{
$message->to($user->email)->subject('Reset Password!');
});
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
return Redirect::route('login')->withErrors('Error, user not found');
}
return Redirect::route('login')->with('info', 'Request send');
}
public function getResetPassword($id = null, $resetCode = null)
{
return View::make('user.reset', array('id'=>$id, 'resetCode'=>$resetCode) );
}
public function postResetPassword()
{
$id = Input::get('id');
$resetCode = Input::get('resetCode');
try
{
// Find the user using the user id
$user = Sentry::findUserById(Input::get('id'));
// Check if the reset password code is valid
if ($user->checkResetPasswordCode(Input::get('resetCode')))
{
// Attempt to reset the user password
if ($user->attemptResetPassword( Input::get('resetCode'), Input::get('password') ) )
{
return Redirect::route('login')->with('success', 'Successful');
}
else
{
return View::make('user.reset', array('id'=>$id, 'resetCode'=>$resetCode))->withErrors(array('reset' => 'Password reset failed' )) ;
}
}
else
{
// The provided password reset code is Invalid
return View::make('user.reset', array('id'=>$id, 'resetCode'=>$resetCode))->withErrors(array('reset' => 'Reset Code wrong, please try again' )) ;
}
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
return Redirect::route('reset', array('id'=>$id, 'resetCode'=>$resetCode))->withErrors(array('reset' => 'User not found' ));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment