Skip to content

Instantly share code, notes, and snippets.

@jackabox
Created November 13, 2017 11:48
Show Gist options
  • Select an option

  • Save jackabox/99648ebcf714130cfab3d3d13040416e to your computer and use it in GitHub Desktop.

Select an option

Save jackabox/99648ebcf714130cfab3d3d13040416e to your computer and use it in GitHub Desktop.
Laravel - Sending reset to new User automatically
<?php
'aliases' => [
...
...
'auth.password.tokens' => Illuminate\Auth\Passwords\TokenRepositoryInterface::class
];
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class ContactController extends Controller
{
public function create(Request $request)
{
$request->validate([
'name' => 'required|max:255',
'email' => 'required|email|unique:users',
]);
//generate a password for the new users
$pw = User::generatePassword();
//add new user to database
$user = new User;
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = $pw;
$user->save();
User::sendWelcomeEmail($user);
return redirect()->to('account/contacts');
}
}
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Mail;
class User extends Authenticatable
{
public static function generatePassword()
{
// Generate random string and encrypt it.
return bcrypt(str_random(35));
}
public static function sendWelcomeEmail($user)
{
// Generate a new reset password token
$token = app('auth.password.broker')->createToken($user);
// Send email
Mail::send('emails.welcome', ['user' => $user, 'token' => $token], function ($m) use ($user) {
$m->from('hello@appsite.com', 'Your App Name');
$m->to($user->email, $user->name)->subject('Welcome to APP');
});
}
}
?>
<h3>Hi {{ $user->username }},</h3>
<p>We've set up an account for you so that you can see your orders. Before you can do this, you'll need to set a password, please use the link below.</p>
<p><a href="{{ url('password/reset/' . $token)}}">Set Password</a></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment