the password reset link lifetime not expires in laravel - php

I created a localized copy of ResetPassword.php, redefined the sendPasswordResetNotification () method in app\user. In general, I did everything according to the rule (I think so))). All the rules, sends a letter to the mail but there is only one problem. This link does not expire. by default there is 60 minutes, after which the link should be destroyed. But this does not happen. Below my code will be grateful for any help.
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class mytelresetpassword extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($token)
{
//
$this->token = $token;
}
/**
* The callback that should be used to build the mail message.
*
* #var \Closure|null
*/
public static $toMailCallback;
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
if (static::$toMailCallback) {
return call_user_func(static::$toMailCallback, $notifiable, $this->token);
}
$time = ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')];
$time = $time['count'];
return (new MailMessage)
->subject('Şifrənin Dəyişdirilməsi')
->line('Hesabınızdan şifrə yeniləmə tələbi aldığımız üçün bu mail sizə göndərilib.')
->action('Şifrəni Yenilə', url(config('app.url').route('password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
->line('Bu link '.$time.' dəqiqə müddətində keçərlidir, bu müddət bitdikdən sonra deaktiv olunacaq!')
->line('Şifrə dəyişməyi tələb etməmişsinizsə, "Şifrəni Yenilə" düyməsini klikləməyə ehtiyac yoxdur.');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
/**
* Set a callback that should be used when building the notification mail message.
*
* #param \Closure $callback
* #return void
*/
public static function toMailUsing($callback)
{
static::$toMailCallback = $callback;
}
}
in app\user:
public function sendPasswordResetNotification($token)
{
$this->notify(new mytelresetpassword($token));
}

I propose adding a few lines in your ResetPasswordController.php:
public function showResetForm(Request $request, $token = null)
{
$email = $request->email;
if (!$token)
return redirect()->route('login');
if (!$email)
return redirect()->route('login');
$reset = PasswordReset::where('email', $request->input('email'))->first();
if (!$reset)
return redirect()->route('login');
$expiry = Carbon::now()->subMinutes( 60 );
if ($reset->created_at <= $expiry) {
return redirect()->route('login');
}
return view('auth.passwords.reset')->with(
['token' => $token, 'email' => $request->email]
);
}

Related

Argument 1 passed to App\Notifications\NewInstalasi::__construct() must be an instance of App\Models\User, instance of App\Models\Instalasi given

i am doing project with notification real time use pusher in laravel 8, and then i have got this error "Argument 1 passed to App\Notifications\NewInstalasi::__construct() must be an instance of App\Models\User, instance of App\Models\Instalasi given, called in C:\xampp\htdocs\rsud\laporan_teknisi_improvement\app\Observers\InstalasiObserver.php on line 15".
in my Notification/NewInstalasi.php codes :
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Models\User;
use App\Models\Instalasi;
class NewInstalasi extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
protected $user, $instalasi;
public function __construct(User $users, Instalasi $instalasi)
{
$this->instalasi = $instalasi;
$this->users = $users;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['database', 'broadcast'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
'user_id' => $this->users->id,
'user_name' => $this->users->name,
'instalasi' => $this->instalasi->nama,
];
}
}
and in Observer/InstalasiObserver.php codes :
<?php
namespace App\Observers;
use App\Notifications\NewInstalasi;
use App\Models\User;
use App\Models\Instalasi;
class InstalasiObserver
{
public function created(Instalasi $instalasi)
{
$author = $instalasi->user;
$users = User::all();
foreach ($users as $user) {
$user->notify(new NewInstalasi($instalasi,$author));
}
}
}

Laravel - How to add redis rate limit with queued notification?

I tried to set rate limit for my notifications queue (called email)
Here is the documentation I checked here. I use laravel 5.6.
Here is the code I tried
class MyController extends Controller{
function myControllerFunction(){
Redis::throttle('email')->allow(1)->every(120)->then(function () use ($user, $userReport) {
$user->notify(new ReportStandard($userReport));
}, function ($error) {
Log::error($error);
return $this->release(120);
});
}
}
I got an error on $this->release(120) because the $this in the closure refer to the MyController class instead of a Job class.
I do not know how I can rate limit my email queue used by notifications?
Notes: ReportStandard is defined this way
class ReportStandard extends Notification implements ShouldQueue
{
use Queueable;
public $userReport;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($userReport)
{
$this->queue = "email";
$this->userReport = $userReport;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
Log::info(now() . " report-standard");
return (new MailMessage)
->subject('Reçu pour votre paiement à Autorigin')
->view('front.mails.cross-sell.report-standard', [
'notifiable' => $notifiable,
'userReport' => $this->userReport
]);
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

How t solve Call to undefined method stdClass::notify()

laravel notify() undefined method. how to solved it....help me..
Error is:-
Call to undefined method stdClass::notify()
My Controller Code Here:
use Illuminate\Http\Request;
use \App\Notifications\ResetLink;
use Auth;
use App\User;
use DB;
use App\Password;
public function forgot(){
return view('forgot');
}
public function forgotPw(Request $request){
if($user = User::where('email',$request->email)->first()){
DB::table('password_resets')->insert([
'token' => $this->token(),
'email' => $user->email
]);
$pr = DB::table('password_resets')->where('email',$user->email)- >first();
$pr->notify(new ResetLink($pr));
request()->session()->flash('success', "Forgot Link Successfully Sent...");
return redirect('login');
} else {
request()->session()->flash('error', "Forgot Link Not Sent...");
return redirect('forgot');
}
}
My Notification Code Here:
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class ResetLink extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct()
{
public $pr;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('Click the Button and Reset Password!')
->action('Password Reset', url('/reset'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
I tried to last 2 hours but No send mail in mailtrap....
Thank You Advance for Help me..

laravel 5.6 Undefined variable: user when trying to send email

I am trying to send an email using Notification class in laravel 5.6 which I am new to.
I am trying to pass user and book information but every time I get the following :
Undefined variable: user
here is my successEmail.php:
<?php
namespace BOOK_DONATION\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class successEmail extends Notification
{
use Queueable;
public $user;
public $book;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($user,$book)
{
//
$this->user=$user;
$this->book=$book;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Dear'.$user->name.'thak you for creating donation for '.$book->name);
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
and here is the function that call the class successEmail:
public function doBook(Request $request){
$validatedData = $request->validate([
'title' => 'string|required|max:255',
'Author'=> 'string|required|max:255',
'Cover_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'Book_description'=>'string|required|max:255',
]);
$path= $request->Cover_image->store('images');
$uid=Auth::user()->id;
$country=Auth::user()->country;
$city=Auth::user()->city;
$book=Book::create(['title'=>$request->input('title'),
'Author'=>$request->input('Author'),
'user_id'=>$uid,
'country'=>$country,
'city' =>$city,
'Book_description'=>$request->input('Book_description'),
'path'=>$path,
]);
$user = Auth::user();
$user->notify(new successEmail($user,$book));
return redirect('/donation/create')->with('status', 'Thank you for your donation');
}
as you see I passed the user and the book variable to the constructor and declared them as public so what is that I am missing?
Those variables do not exist in the function scope but they do exist as class properties so they should be treated as such.
return (new MailMessage)
->greeting('Dear '.$this->user->name.' thank you for creating donation for '.$this->book->name);
You need to make the call to $this->user to access the variable in the class scope.
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Dear'.$this->user->name.'thak you for creating donation for '.$this->book->name);
}
You should try this:
use Auth;
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Dear'.$this->user->name.'thak you for creating donation for '.$this->book->name);
}
That happened to me too. In the function toMail you need to redeclare the variables like this
$user= $this->user;
And then you call the variable like this
->line('user:'.''.$user,'')

Password Reset Not working in Laravel 5.4

I am new to laravel. I want to implement the login and registeration using laravel auth. I have implemented the login and registration flow successfully but I am not able to implement reset password.
I am facing the following issue:
1. After submitting the username/email some processing happens but I am not able to send the password reset link using the email. It always got a screen which says we have emailed your password reset link but nothing is happening. The entries are being made in the password_resets table.
Any help will be much appreciated.
I am using the gmail to send the password reset emails. After doing a lot of debugging everything looks fine apart from ResetPassword.php file which is executed till
public function via($notifiable)
{
// echo "<pre>".print_r($notifiable,1)."</pre>";exit("<br/>sdds");
return ['mail'];
}
There is a function in this file which is created to send the emails but this function is not going called. The function is given below :
/**
* Build the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
echo "<pre>".print_r($notifiable,1)."</pre>";exit();
return (new MailMessage)
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', route('password.reset', $this->token))
->line('If you did not request a password reset, no further action is required.');
}
Here are the main file used in this
Authorisation model class RetailUserAuth.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Auth\Passwords\CanResetPassword;
class RetailUserAuth extends Authenticatable
{
use Notifiable;
//protected $primaryKey = 'user_id';
protected $table = 'retail_user_auth';
public $timestamps = false;
protected $fillable = ['username', 'user_id', 'password','source','user_status'];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
SendsPasswordResetEmails.php
<?php
namespace Illuminate\Foundation\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
trait SendsPasswordResetEmails
{
/**
* Display the form to request a password reset link.
*
* #return \Illuminate\Http\Response
*/
public function showLinkRequestForm()
{
return view('auth.passwords.email');
}
/**
* Send a reset link to the given user.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\RedirectResponse
*/
public function sendResetLinkEmail(Request $request)
{
$this->validate($request, ['username' => 'required|email']);
// We will send the password reset link to this user. Once we have attempted
// to send the link, we will examine the response then see the message we
// need to show to the user. Finally, we'll send out a proper response.
$response = $this->broker()->sendResetLink(
$request->only('username')
);
//echo "</pre>".print_r($response,1)."</pre>";exit();
return $response == Password::RESET_LINK_SENT
? $this->sendResetLinkResponse($response)
: $this->sendResetLinkFailedResponse($request, $response);
}
/**
* Get the response for a successful password reset link.
*
* #param string $response
* #return \Illuminate\Http\RedirectResponse
*/
protected function sendResetLinkResponse($response)
{
return back()->with('status', trans($response));
}
/**
* Get the response for a failed password reset link.
*
* #param \Illuminate\Http\Request
* #param string $response
* #return \Illuminate\Http\RedirectResponse
*/
protected function sendResetLinkFailedResponse(Request $request, $response)
{
return back()->withErrors(
['username' => trans($response)]
);
}
/**
* Get the broker to be used during password reset.
*
* #return \Illuminate\Contracts\Auth\PasswordBroker
*/
public function broker()
{
return Password::broker();
}
}
CanResetPassword.php
<?php
namespace Illuminate\Auth\Passwords;
use Illuminate\Auth\Notifications\ResetPassword as
ResetPasswordNotification;
trait CanResetPassword
{
/**
* Get the e-mail address where password reset links are sent.
*
* #return string
*/
public function getEmailForPasswordReset()
{
return $this->username;
}
/**
* Send the password reset notification.
*
* #param string $token
* #return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
}
ResetPassword.php
<?php
namespace Illuminate\Auth\Notifications;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class ResetPassword extends Notification
{
/**
* The password reset token.
*
* #var string
*/
public $token;
/**
* Create a notification instance.
*
* #param string $token
* #return void
*/
public function __construct($token)
{
//echo $token;exit('<br/>fdfdffd');
$this->token = $token;
}
/**
* Get the notification's channels.
*
* #param mixed $notifiable
* #return array|string
*/
public function via($notifiable)
{
// echo "<pre>".print_r($notifiable,1)."</pre>";exit("<br/>sdds");
return ['mail'];
}
/**
* Build the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
echo "<pre>".print_r($notifiable,1)."</pre>";exit();
return (new MailMessage)
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', route('password.reset', $this->token))
->line('If you did not request a password reset, no further action is required.');
}
}

Categories