Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Send activation link to user by email (Invitation system) - with working code #20

Closed
RokSiEu opened this issue Nov 5, 2013 · 1 comment

Comments

@RokSiEu
Copy link
Contributor

RokSiEu commented Nov 5, 2013

Hi,

I needed invitation system that will send activation email to user, after user visits the link his profile will be activated.

I thought I should share my code if someone will found it useful.

Blade:

{{ Form::open(array('url' => 'user/invite')) }}
    <input type="hidden" name="csrf_token" id="csrf_token" value="{{{ Session::getToken() }}}" />

    <div class="{{{ $errors->has('first_name') ? 'error' : '' }}}">
        <label for="first_name">Name</label>
        <input type="text" name="first_name" id="first_name" value="" />
        {{{ $errors->first('first_name') }}}
    </div>

    <div class="{{{ $errors->has('email') ? 'error' : '' }}}">
        <label for="email">Email</label>
        <input type="text" name="email" id="email" value="" />
        {{{ $errors->first('email') }}}
    </div>

    <div class="{{{ $errors->has('password') ? 'error' : '' }}}">
        <label for="password">Password</label>
        <input type="password" name="password" id="password" value="" />
        {{{ $errors->first('password') }}}
    </div>

<input type="hidden" name="activation" id="activation" value="1" />
    {{ Form::submit('Povabi',array('class' => 'button'));}}
{{ Form::close() }} 

UserController@postInvite

public function postInvite()
    {
         // Declare the rules for the form validation
                $rules = array(
                        'first_name'       => 'required|min:3',                       
                        'email'            => 'required|email|unique:users',                      
                        'password'         => 'required|between:3,32',
                );

                // Create a new validator instance from our validation rules
                $validator = Validator::make(Input::all(), $rules);

                // If validation fails, we'll exit the operation now.
                if ($validator->fails())
                {
                        // Ooops.. something went wrong
                        return Redirect::back()->withInput()->withErrors($validator);
                }

                try
                {
                        // Register the user
                        $user = Sentry::register(array(
                                'username'   => Input::get('first_name'),
                                'first_name' => Input::get('first_name'),
                                'email'      => Input::get('email'),
                                'password'   => Input::get('password'),
                        ));

                        // Data to be used on the email view
                        $data = array(
                                'user'          => $user,
                                'activationUrl' => URL::route('activate', $user->getActivationCode()),
                        );

                        // Send the activation code through email
                        Mail::send('emails.auth.invite', $data, function($m) use ($user)
                        {
                                $m->from('testingmail@gmail.com','test');
                                $m->to($user->email, $user->first_name . ' ' . $user->last_name);
                                $m->subject('Hello' . $user->first_name);
                        });

                        // Redirect to the register page
                        return Redirect::back()->with('success','Activation mail was sent!');
                }
                catch (Cartalyst\Sentry\Users\UserExistsException $e)
                {
                        $this->messageBag->add('email','Something went wrong!');
                }

                // Ooops.. something went wrong
                return Redirect::back()->withInput()->withErrors($this->messageBag);
    }

userController@getActivate

public function getActivate($activationCode = null)
        {
                // Is the user logged in?
                if (Sentry::check())
                {
                        return Redirect::to('projects');
                }

                try
                {
                        // Get the user we are trying to activate
                        $user = Sentry::getUserProvider()->findByActivationCode($activationCode);

                        // Try to activate this user account
                        if ($user->attemptActivation($activationCode))
                        {
                                // Redirect to the login page
                                return Redirect::to('user/login')->with('success', 'Your profile is activated. Now you can log in!');
                        }

                        // The activation failed.
                        $error = 'Something went wrong!';
                }
                catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
                {
                        $error = $e;
                }

                // Ooops.. something went wrong
                return Redirect::to('user/login')->with('error', $error);
        }

@RokSiEu
Copy link
Contributor Author

RokSiEu commented Nov 5, 2013

Duplicated

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant