Created
November 13, 2017 11:48
-
-
Save jackabox/99648ebcf714130cfab3d3d13040416e to your computer and use it in GitHub Desktop.
Laravel - Sending reset to new User automatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| 'aliases' => [ | |
| ... | |
| ... | |
| 'auth.password.tokens' => Illuminate\Auth\Passwords\TokenRepositoryInterface::class | |
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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'); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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'); | |
| }); | |
| } | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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