Override core - reset password file in laravel - laravel-5

I need to send success email for after resetting password in laravel-5.4
I write code like this :
File path : mylaravel/vendor/laravel/framework/src/Illuminate/Foundation/Auth/ResetsPasswords.php
Mycode :
protected function resetPassword($user, $password)
{
$user->password = Hash::make($password);
$user->setRememberToken(Str::random(60));
$user->save();
$mail_data = array('name' => $user->first_name);
Mail::send('mails.forgotpassword', $mail_data, function ($message) use($user) {
$message->from(Config::get('globalvars.CONTACT_EMAIL'), 'Its Your Skills');
$message->to($user->email);
$message->subject('Forgot Password regarding');
});
event(new PasswordReset($user));
$this->guard()->login($user);
}
i writed that mail code , This one working fine . but for updating composer that code will automatically delete. so i need to know how to doo this action in mylaravel/app folder. any help?

yes you can not make change in any core file for that you have to make controller in your app/html/comtrollers/auth and then make same function as given in core file and override that function and change the functionality according that.
but do not try to make change in core file as you said in your question when ever change in composer and update laravel with newer version make trouble for you.
May i hope this helps you.

Related

Why signedRoute is always false on hasValidSignature in Laravel 8.*

I'm trying to add users only by single unique url on register.
The problem is that hasValidSignature() in register function always returns false, no metter if i would go directly from /register or generated url.
My RegisterController:
public function register(Request $request) {
dump($request);
dump($request->_token);
dd($request->hasValidSignature());
abort_unless($request->hasValidSignature(), 403, 'That link has expired or is no longer valid!');
}
My index where I generate unique URL for registration:
public function index()
{
Gate::authorize('admin-level');
$users = User::where('admin', '0')->get();
$url = URL::temporarySignedRoute(
'register',
now()->addMinutes(30)
);
return view('backend/user/index', ['users' => $users], ['url' => $url]);
}
or just generate url:
$url = URL::temporarySignedRoute(
'register',
now()->addMinutes(30)
);
Everything is done just like in documentation, that's why I'm so mad about this. What can I do to make urls good.
Its either something wrong with my hasValidSignature(which I didnt ever change)
or with my URL.
I tried many different methods, always with same result.
Fun fact is that it worked just after implementing. Now, it doesnt.
I remade all auth with php artisan ui:auth. Whole registration works well unless it goes with hasValidSignature.
My users have to register only from unique URL, i cannot bypass that.
Sometimes there are other parameters in the query included...
Therefore you must ignore them:
$request->hasValidSignatureWhileIgnoring(['param1', 'paramX'])

Lumen : 1s, Laravel : 5s for a simple api call?

So I started a project not long ago for an API on Laravel, and I thought why not give Lumen a shot. But in the end, I want to use Sanctum, Socialite, etc... And I read pretty much everywhere that the performance difference is not that big nowadays anyway.
So I migrated my code from Lumen to Laravel, and after a few tweaks, everything works as before... Except that now a very simple API call takes 5s. Granted, it might be my setup - wsl2 isn't particularly fast. But still, the same call in Lumen was taking ~1000ms.
Route::post('register', [AuthController::class, 'register']);
Controller:
public function register(Request $request): JsonResponse {
$this->validate($request, [
'phone' => 'required|string|phone',
'phone_country' => 'required_with:phone',
]);
$phone = phone($request->get('phone'), [$request->get('phone_country')]);
try {
$user = User::createByPhone($phone);
return response()->json(['user' => $user->id, 'message' => 'SMS_SENT'], 201);
} catch (\Exception $e) {
return response()->json(['message' => 'User Registration Failed - ', 'error' => $e], 409);
}
}
Function in model:
public static function createByPhone($phone) {
return DB::transaction(function () use ($phone) {
$user = User::create();
$user->phoneNumbers()->create([
'did' => $phone
]);
return $user;
});
}
So, pretty simple stuff. Now, why is that taking so long? ~6000ms. Am I missing something?
(On a more general note, is there a way to cut from Laravel things that aren't needed for an API only?)
Thanks ahead.
I don't see anything really wrong with your code. I guess this has something to do with the speed wsl2 can read files. We had issues with windows machines and Laravel in Docker. We added Swoole to our project and this helped alot on WSL2.
Laravel now has a first party package called Octane to add Swoole to your project. You can try and install that to see if it helps.

Laravel 5.1 Mail::send doesn't work - gives 500 internal server error

I took an example from the Laravel 5.1 documentation for Mail and replaced it with my send and receiver email ids.
Mail::raw works in the controller and if I use Mail::send in tinker it works. However, if I use Mail::send in the controller it doesn't work.
Everything is set up as described on the Laravel 5.1 mail page. https://laravel.com/docs/5.1/mail#sending-mail . I have also cleared app cache, config cache and view cache.
public function sendEmailReminder(Request $request, $id)
{
$user = User::findOrFail($id);
Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
$m->from('hello#app.com', 'Your Application');
$m->to($user->email, $user->name)->subject('Your Reminder!');
});
}
The error could be from the email you are trying to send from, in order to send the email successfully this email hello#app.com has to be a valid email address and the one register as your MAIL_USERNAME
It was a permissions issue for the storage/frameworks directory. Once I changed the permissions it worked fine..

Unable to send emails in laravel 5 app

Hello I have a laravel 5 app which is working perfectly in local environment. But in production emails are not getting sent instead I get the exception below:
1/1 FatalErrorException in AstAnalyzer.php line 125:
Cannot instantiate interface PhpParser\Parser
Path to file: /vendor/jeremeamia/SuperClosure/src/Analyzer/AstAnalyzer.php line 125
I don't get it because right now I am testing same function in local and is working. Every other path of the app is working except this one.
Below is the function:
public function update_password(Request $request, $id)
{
$this->validate($request, [
'new_password' => 'required|confirmed|min:6',
'new_password_confirmation' => 'required',
]);
$user = $this->user->get_user_by_id($id);
$password = $request->get('new_password');
$this->user->save_password($password, $id);
// Send an email informing user that we have updated his password.
Mail::queue('emails.password_update', ['user' => $user, 'password' => $password], function($message) use ($user){
$message->to($user->email, $user->name)->subject('Account Password Updated');
});
$target_location = 'users/'. $id. '/profile';
flash()->success('Password Updated Successfully');
return redirect($target_location);
}
I finally solved my problem. I ran a composer update which installed the lastest version of nikic/php-parser and equally the latest version jeremeamia/superclosure, but somehow, the class Parser which was formally use in nickic's package was now an Interface. class Multiple was now implementing the interface. So in AstAnalyzer.php in jeremeamia's package, that change was not made and instead use PhpParser\Parser as CodeParser; was used which is logical as an interface cannot be instantiated unless some binding is done. So as a quick fix, I used the previous version of nikic/php-parser.

creating a folder once user registred laravel 5

I would like to create a folder inside Storage folder in Laravel 5, once you register and pick your username, a folder with that user will be created for you.
If you created user : john5500 a folder inside Storage will be created with 'john5500' and will belong only to that user.
Mark, see the code below.
This code I use to create a new user in my database.
Information about ManagementCreateRequest $Request can be found via this URL.
Laravel Controller Validation
In short I'm validating my input via Controller Validation in Laravel.
After the validation passes I get all the data from the validation in the variable $Request.
After that I create the user as below. After creating the user I send a redirect to the management page. This page contains an overview of all the users in the database.
public function store(ManagementCreateRequest $Request)
{
// Create user
Management::create($Request->all());
// Return view
return redirect('management')
->with('Success', 'User created.');
}
If I would to create a directory I would do it like this.
public function store(ManagementCreateRequest $Request)
{
// Create user
Management::create($Request->all());
// Create directory
File::MakeDirectory('/path/to/directory' . $Request->username);
// Return view
return redirect('management')
->with('Success', 'User created.');
}
Replace /path/to/directory with the actual path to your storage directory.
For example: Under CentOS my storage directory would be.
/var/www/Site Name/storage
Don't forget to replace 'Site Name' with the name of your Laravel site.
More detailed information about File:makeDirectory can be found via this link:
Laravel Creating Directory
Lravel 5 comes with an excellent filesystem. You could simply do:
Storage::makeDirectory($directory);
See the documentation for more details: http://laravel.com/docs/5.0/filesystem#basic-usage
You can use Laravel File Facade:
File::makeDirectory($path, $mode = 0755, $recursive = false, $force = false);
Ensure storage is writable
public function create(array $data)
{
//return
$user = User::create([
'name' => $data['name'],
'username'=>$data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
\File::makeDirectory(storage_path($data['username']));
return $user;
}

Resources