Mahir
Mahir

Reputation: 81

Save email parameter for resend function - Laravel

I have resend email function and it works allright but it requires users to put email first for the token for verification to be sent if its not send after registration. I want to make it without input resend verification, the problem is if they are not verificated they cant login so i cant use Auth::$user->email to check his email and to send the link. How can i get his email parameter after he registers (it automatically redirects it to a page where says verify your account and there is a button to resend email if the first time didnt get sent.

This is how my registration function looks like:

protected function create(array $data)
    {
        $user = Account::create([
            'login' => $data['login'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'verifyToken'=> Str::random(40),
            'active' => (env('CONFIRM_EMAIL', true)) ? 0 : 1
        ]);
        $thisUser = Account::findOrFail($user->id);

        if(env('CONFIRM_EMAIL') == true){
            $this->sendEmail($thisUser);
        }

        return $user;
    }

And this is how i want to resend email with resend button:

protected function resend(Request $request, array $data)
    {
        $user = Account::where('email', $data['email'])->first();
        if($user){
        $user->verifyToken = Str::random(40);
        $user->save();
        $this->sendEmail($user);
        return back()->with('success', 'A link has been sent to your email');
        }else{
            return back()->with('error', 'Email does not exist');
        }
    }

But it doesnt work, where am i wrong?

Too few arguments to function App\Http\Controllers\Auth\RegisterController::resend(), 1 passed and exactly 2 expected

Upvotes: 0

Views: 540

Answers (2)

Developer
Developer

Reputation: 474

step 1

Make a form for fill email from user and call the create function

protected function create(array $data)
{
    $user = Account::create([
        'login' => $data['login'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'verifyToken'=> Str::random(40),
        'active' => (env('CONFIRM_EMAIL', true)) ? 0 : 1
    ]);
    $thisUser = Account::findOrFail($user->id);

    if(env('CONFIRM_EMAIL') == true){
        $this->sendEmail($thisUser);
    }

    return $user;
}

and give one more button there resend

<form method="POST" action="{!! route(verification.resend) !!}">
 @csrf
 <input type="hidden" name="email" value="{{ $user->email }}">
 <button type="submit">Resend email</button>
</form>

protected function resend(Request $request)
{
    $user = Account::where('email', $request->email)->first();
    if($user){
    $user->verifyToken = Str::random(40);
    $user->save();
    $this->sendEmail($user);
    return back()->with('user',$user)->with('success', 'A link has been sent to your email');
    }else{
        return back()->with('error', 'Email does not exist');
    }
}

Upvotes: 0

Alberto
Alberto

Reputation: 12929

If you only need to have that resend button in other pages, you just need a form like this:

<form method="POST" action="{!! route(verification.resend) !!}">
     @csrf
     <button type="submit">Resend email</button>
</form>

UPDATE:
if the user is not logged in, there is no way for you to know his email. You can use some cookie-based solution to store it when he register, however there is no way for you to know if that is the actual email he wants to use

Upvotes: 0

Related Questions