Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sentry Bundle Example (see here http://forums.laravel.com/viewtopic.php?pid=20075)
/*
|--------------------------------------------------------------------------
| Register
|--------------------------------------------------------------------------
*/
Route::get('register', function()
{
return View::make('register');
});
Route::post('register', function()
{
$input = Input::get(); //receive input
try
{
//create some validation rules
$rules = array(
'email' => 'required|email|unique:users',
'password' => 'same:confirm'
);
//validate the inputs
$validation = Validator::make($input, $rules);
if($validation->fails())
{
return Redirect::to('register')->with_errors($validation);
}
else
{
unset($input['confirm']);
$user = Sentry::user()->create($input);
if($user)
{
Return Redirect::to('login');
}
}
}
catch (Sentry\SentryException $e)
{
//create a real Laravel\Messages object from a sentry error.
$errors = new Laravel\Messages();
$errors->add('sentry', $e->getMessage());
Return Redirect::to('register')->with_errors($errors);
}
});
/*
|--------------------------------------------------------------------------
| Login
|--------------------------------------------------------------------------
*/
Route::get('login', function()
{
return View::make('login');
});
Route::post('login', function()
{
try
{
$valid_login = Sentry::login(Input::get('email'), Input::get('password'), true);
if ($valid_login)
{
Return Redirect::to('test');
}
}
catch (Sentry\SentryException $e)
{
$errors = new Laravel\Messages();
$errors->add('sentry', $e->getMessage());
Return Redirect::to('login')->with_errors($errors);
}
});
/*
|--------------------------------------------------------------------------
| Logout
|--------------------------------------------------------------------------
*/
Route::get('logout', function()
{
Sentry::logout();
return Redirect::to('home');
});
/*
|--------------------------------------------------------------------------
| Test
|--------------------------------------------------------------------------
*/
Route::get('test', array('before' => 'sentry', 'do' => function()
{
Return View::make('test');
}));
/*
|--------------------------------------------------------------------------
| Filter
|--------------------------------------------------------------------------
*/
Route::filter('sentry', function()
{
if (!Sentry::check()) return Redirect::to('login');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.