12

I have made until now an app with login/register and it works fine. After the registration a welcome email is sent.

But what i would like to do is to send a link, within that mail, that only after clicking on it, it is possible to login.

Like the common registration email for forum etc..

Someone can help me please?

This is the postRegister method:

public function postRegister()
{
    $input = Input::all();

    $rules = array(
        'username' => 'required',
        'password' => 'required');

    $validation = Validator::make($input, $rules);

    if ($validation->passes()) {

        $password = $input['password'];
        $password = Hash::make($password);

        $user = new User;
        $user->username = $input['username'];
        $user->email = $input['email'];
        $user->password = $password;

            $mailer = new Mailers\UserMailer($user);

                 // var_dump($mailer);

                    $mailer->welcomeMail()->deliver();

                    $user->save();

        return Redirect::to('afterRegister');
    }

    return Redirect::back()->withInput()->withErrors($validation)->with('message', 'Validation Errors!');
} 

Thank you

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
FrancescoMussi
  • 20,760
  • 39
  • 126
  • 178

1 Answers1

26

Here are a few clues (not gonna write the code for you).

  • Add two fields to your user table: confirmation, confirmed.
  • Create a route in Laravel like registration/verify/{confirmation}, in which you try and find a user in your DB with the given confirmation code (if found, set user's confirmed field to 1).
  • Upon user registration, generate a unique confirmation code (you can use the str_random() helper function for this).
  • Set DB entry of new user accordingly (confirmation = the random code, confirmed = 0)
  • Include a verification link (built according to your verification route) with the generated confirmation code in your email to your new user.

Auth attempts can now be done like this:

$user = array(
        'username' => Input::get('username'),
        'password' => Input::get('password'),
        'confirmed' => 1
);

if (Auth::attempt($user)) {
    // success!
    return Redirect::route('restricted/area');
}
ciruvan
  • 5,143
  • 1
  • 26
  • 32
  • Is it possible to set a custom message if only confirmed is false. so you get a error message like the account is not registered yet. – Sven van den Boogaart May 28 '14 at 14:05
  • 1
    @Sven B you can do Redirect::route('restricted/area')->with('message', 'Account is not registered'); and use {{ $message }} in your view. – Unnawut Jun 20 '14 at 03:18
  • 2
    I prefer not using the 'confirmed' column. When the user is activated I just remove the confirmation code. This way, all users without confirmation_token are activated. – Bruno Rodrigues Jun 21 '14 at 14:34