Skip to content

Instantly share code, notes, and snippets.

@dannymichel
Created March 19, 2014 19:29
Show Gist options
  • Save dannymichel/9649421 to your computer and use it in GitHub Desktop.
Save dannymichel/9649421 to your computer and use it in GitHub Desktop.
<?php
class AccountController extends BaseController {
public function getSignIn(){
return View::make('account.signin');
}
public function postSignIn(){
$validator = Validator::make(Input::all(),
array(
'username' => 'required|max:20|min:3',
'password' => 'required|min:6',
)
);
if($validator->fails()){
Redirect::route('account-sign-in')
->withErrors($validator)
->withInput();
} else {
$remember = (Input::has('remember')) ? true : false;
$auth = Auth::attempt(array(
'username' => Input::get('username'),
'password' => Input::get('password'),
'active' => 1
), $remember);
if($auth){
return Redirect::intended('/');
} else {
return Redirect::route('account-sign-in')
->with('global', 'Email/password wrong, or not activated.');
}
}
return Redirect::route('account-sign-in')
->with('global', 'There was a problem signing in.');
}
public function getSignOut(){
Auth::logout();
return Redirect::route('home');
}
public function getCreate(){
return View::make('account.create');
}
public function postCreate(){
$validator = Validator::make(Input::all(),
array(
'email' => 'required|max:50|email|unique:users',
'username' => 'required|max:20|min:3|unique:users',
'password' => 'required|min:6',
'password_again' => 'required|same:password'
)
);
if($validator->fails()) {
return Redirect::route('account-create')
->withErrors($validator)
->withInput();
} else {
$email = Input::get('email');
$username = Input::get('username');
$password = Input::get('password');
$code = str_random(60);
$user = User::create(array(
'email' => $email,
'username' => $username,
'password' => Hash::make($password),
'code' => $code,
'active' => 0
));
if($user) {
Mail::send('emails.auth.activate', array('link' => URL::route('account-activate', $code), 'username' => $username), function($message) use ($user) {
$message->to($user->email, $user->username)->subject('Activate your account');
});
return Redirect::route('home')
->with('global', 'Your account has been created. We have sent you an e-mail to activate your account.');
}
}
}
public function getActivate($code){
$user = User::where('code', '=', $code)->where('active', '=', 0);
if($user->count()){
$user = $user->first();
$user->active = 1;
$user->code = '';
if($user->save()){
return Redirect::route('home')
->with('global', 'Activated. You can now sign in.');
}
}
return Redirect::route('home')
->with('global', 'We could not activate your account. Try again later.');
}
public function getChangePassword() {
return View::make('account.password');
}
public function postChangePassword() {
$validator = Validator::make(Input::all(),
array(
'old_password' => 'required',
'password' => 'required|min:6',
'password_again' => 'required|same:password'
)
);
if($validator->fails()){
return Redirect::route('account-change-password')
->withErrors($validator);
} else {
}
}
}
<?php
Route::get('/', array(
'as' => 'home',
'uses' => 'HomeController@home',
));
Route::group(array( 'before' => 'auth'), function(){
Route::group(array('before' => 'csrf'), function(){
Route::post('/account/change-password', array(
'as' => 'account-change-password-post',
'uses' => 'AccountController@postChangePassword'
));
});
Route::get('/account/change-password', array(
'as' => 'account-change-password',
'uses' => 'AccountController@getChangePassword'
));
Route::get('account/sign-out', array(
'as' => 'account-sign-out',
'uses' => 'AccountController@getSignOut'
));
});
Route::group(array('before' => 'guest'), function(){
Route::group(array('before' => 'csrf'), function(){
Route::post('/account/create', array(
'as' => 'account-create-post',
'uses' => 'AccountController@postCreate'
));
Route::post('/account/sign-in', array(
'as' => 'account-sign-in-post',
'uses' => 'AccountController@postSignIn'
));
});
Route::get('/account/sign-in', array(
'as' => 'account-sign-in',
'uses' => 'AccountController@getSignIn'
));
Route::get('/account/create', array(
'as' => 'account-create',
'uses' => 'AccountController@getCreate'
));
Route::get('/account/activate/{code}', array(
'as' => 'account-activate',
'uses' => 'AccountController@getActivate'
));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment