项目作者: sayed-ahmed

项目描述 :
Email activation and reset Password
高级语言: PHP
项目地址: git://github.com/sayed-ahmed/Email-Password.git
创建时间: 2018-06-12T10:27:45Z
项目社区:https://github.com/sayed-ahmed/Email-Password

开源协议:

下载


Email-Password

Email activation and reset Password

To activate the email :::

1 - You need to set the email and password in .env file to ur mailtrap account if u use it.

2 -

  1. php artisan make:auth

3 - add 2 columns in user table

  1. $table->string('token')->nullable();
  2. $table->boolean('active')->default(false);

4 - don’t forget to add active and token to ur user moel

5 -
php artisan migrate

6 - add this method in Controller\Auth\RegisterController.php

  1. protected function registered(Request $request, $user)
  2. {
  3. $this -> guard() -> logout();
  4. return redirect('login') -> with('success', 'You registered successfully, Please Check your Email to activate you account.');
  5. }

7 - add this to auth\loginblade.php to mange the massege

  1. @if ($errors->has('email'))
  2. <span class="invalid-feedback">
  3. <strong>{{ $errors->first('email') }}</strong>
  4. </span>
  5. @endif

8 - add in create method at RegisterController.php

  1. 'token'->str_random(100)

9 - then go to Controller\Auth\LoginController.php and add this method

  1. protected function validateLogin(Request $request)
  2. {
  3. $this->validate($request, [
  4. $this->username() =>
  5. Rule::exists('users')->where(function ($query){
  6. $query->where('active', true);
  7. })
  8. ], [
  9. 'Invaled E-mail or Password or You need to activate your email.'
  10. ]);
  11. }

10 -
php artisan make:event EventMailActivate

11 - open it and add

  1. public $user;
  2. public function __construct($user)
  3. {
  4. $this->user = $user;
  5. }

12 - add this at RegisterController.php at registered method

  1. event(new EventMailActivate($user));

13 -
php artisan make:listener ListenerMailActivate —event=EventMailActivate

14 - then replace the EventServiceProvider.php protected $listen with

  1. protected $listen = [
  2. 'App\Events\EventMailActivate' => [
  3. 'App\Listeners\ListenerMailActivate',
  4. ],
  5. ];

15 -
php artisan make:mail SendMail —markdown=mails.mail

16 - add in SendMail.php

  1. public $user;
  2. public function __construct($user)
  3. {
  4. $this->user = $user;
  5. }

17 - add this method at ListenerMailActivate.php

  1. public function handle(EventActivateEmail $event)
  2. {
  3. if ($event->user->active){
  4. return;
  5. }
  6. Mail::to($event->user->email) -> send(new SendMail($event->user));
  7. }

18 - now u can check ur mailtrap after registeration and u must find the massege.

19 - to active the button in the massege do that

  1. php artisan make:controller Auth\ActiveController

20 - and add this method to ActiveController

  1. public function activate(Request $req){
  2. $user = User::where('token', $req->token) -> where('email', $req->email) -> firstOrFail();
  3. $user -> update([
  4. 'active' => true,
  5. 'token' => null
  6. ]);
  7. Auth::loginUsingId($user->id);
  8. return redirect('home') -> with('success', 'Your account acivated successfully, Welcome '.$user->name);
  9. }

21 - open route.php or web.php and make ur url like that

  1. Route::get('activate', 'Auth\ActivateController@activate')->name('activate');

22 - now go to resources\views\mails\mail.blade.php and set ur route like that

  1. ['url' => route('activate', [
  2. 'token' => $user -> token,
  3. 'email' => $user -> email
  4. ])
  5. ]

23 - when u go to ur mailtrap and click on the button it must open new tap to ur page home
but if u have error take the url and change the localhost to ur project url


To reset password::

actually, when u make auth and set ur email at .env it work without any errors