Unable to get complete URL in Password Reset in Laravel - laravel

I am using the Laravel 5 ResetsPasswords trait to implement password reset. In the file resources/views/emails/password.blade.php., I have the following code.
Click To Reset Password
I am using a queue to send the email to the user. When the user clicks the link received in email, I get something like below:
http://localhost/password/reset/2aafe5381baa67f9d1fe2f476c0f1395b21e71fafe181b2980fb838fc9e0b2fc
I was rather expecting:
http://localhost/oap/public/password/reset/2aafe5381baa67f9d1fe2f476c0f1395b21e71fafe181b2980fb838fc9e0b2fc
Why is oap/public/ not in the url? I have used the url() function in other views and they have worked just well except for this email view where I am using queue to send the message. When a queue is not used to send the message, the link is okay. Any idea how I can resolve this issue.

Use URL::to. Like,
Click To Reset Password

for the url helpers it will generate the full path for your inputs
{{ url('/password/reset/'.$token) }}
will generate
http://localhost/password/reset/2aafe5381baa67f9d1fe2f476c0f1395b21e71fafe181b2980fb838fc9e0b2fc
and
{{ url('/oap/public/password/reset/'.$token) }}
will generate
http://localhost/oap/public/password/reset/2aafe5381baa67f9d1fe2f476c0f1395b21e71fafe181b2980fb838fc9e0b2fc
you can also get around this
Route::get('oap/public/password/reset/{confirmcode}', ['as' => 'resetpassword', 'HomeController#resetmethod']);
then using this like that
{{route('resetpassword', ['confirmcode' => $token])}}

Related

Trying to show the current signed in users profile. What am i doing wrong | Laravel 9

Trying to show the current signed in user's profile. What am i doing wrong.This is the function on the controller. I'm using Laravel 9
public function show(User $user)
{
return view('users.index', with('user', $user));
}
This is the routes
Route::resource('users', UsersController::class)->middleware('auth');
And my generic layout page
<x-dropdown-item href="/users/{{ auth()->user()->username }}" >Account</x-dropdown-item>
When i click the link i get user not found.
You're utilising route model binding which unless configured otherwise, requires you to provide a route with a model id. You're providing it with a username, so Laravel is throwing a 404 because it can't locate the relevant record in the database.
If you replace username with id, the binding should work.
<x-dropdown-item href="/users/{{ auth()->user()->id }}">
Account
</x-dropdown-item>
The code is fine. Are you sure you have a route with username, resource routes are using id.
To access model like that you need to specify id in the route like:
/users/{id}
However what you are trying to do here is access the logged in user model which you can access with the facade Auth::user();
You can access the user any time any where in the code and there is no need to pass it to an anchor link unless sending to an external system.

URL generator putting "localhost" before domain in Laravel

I've been trying to email users a verification link using signed URLs in Laravel. It seems to work okay, but the link comes out incorrect when it is built using the URL facade.
public function toMail($notifiable)
{
$url = URL::signedRoute('confirm', ['user' => $this->user->id]);
return (new MailMessage)
->subject('Activate your email address')
->line('In order to use the application, please verify your email address.')
->action('Activate your account', $url)
->line('Thank you for using our application!');
}
In the email, the link looks like:
http://localhost/mydomain.com/confirm/14?signature=3ba4d86827717440f70a3b2f60c913b6e84d550cb9fce8de04a8ba359833ac7c
The "localhost" part should not be there. However, if I manually delete it in the URL bar, I believe the signed URL things I manipulated the URL and gives me a 401 error. I am running on a localhost environment but I use Laragon's auto virtual host so that I can still run it with a domain.
Any suggestions?
Sometimes if you are working in virtual environments or docker container, just setting APP_URL won't work. Try the following steps.
Step 1: Set your domain in your .env file. (Don't forget the double quotes)
APP_URL="http://yourdomain.com"
Step 2: Add following line before generating your signed route
URL::forceRootUrl(\config('app.url'));
Step 3: (Optional) Add following line, if you want to force https scheme
URL::forceScheme('https');
Change:
APP_URL=example.com
To:
APP_URL=http://example.com
I guess not specifying "http://" makes it append localhost to the front. Hope this helps someone!

Laravel - GET request download logout automaticlly

i have a download file link like this:
Download file
ROUTES
Route::group(['middleware' => ['web','auth','Admin','active'], 'prefix' => 'admin'], function(){
// USERS
Route::resource('user','UserController');
Route::post('user/permissions/update','UserController#update_permission')->name('update_user_permissions');
// MODULI
Route::resource('module','ModuleController');
// MODULISITICA
Route::resource('modulistica','ModulisticaController');
Route::post('modulistica_cliente','ModulisticaController#update_client_module')->name('modulistica_post_cliente');
Route::post('modulistica_prodotto','ModulisticaController#update_product_module')->name('modulistica_post_prodotto');
Route::get('modulistica/download/cliente/{file}','ModulisticaController#download_cliente')->name('modulistica_download_cliente');
Route::get('modulistica/download/{file}','ModulisticaController#download_module')->name('modulistica_download_module');
Route::get('modulistica/download/prodotto/{file}','ModulisticaController#download_prodotto')->name('modulistica_download_prodotto');
// UTILITY
Route::post('utility/become/client','UtilityController#become_client')->name('utility_become_client');
Route::resource('loan','LoanController');
Route::get('area_download/document/{file}', function ($file){
$path_file = storage_path().'/app/public/documents/'.$file;
return response()->download($path_file, $file);
})->name('download_document');
});
ERROR
Arrival at the "https://mysite.it/admin/loan" view without problems. When I click on the GET link it redirects me to the LOGIN, but being my user logged in by login redirects me to "https://mysite.it/home".
I did some tests getting the following information:
Request does not arrive at route "area_download / document / {file}"
The request does not arrive at the 'Admin', 'active' middlewares.
So my conclusions are that the problem is in the middleware "Web" or "Auth" but I can not understand why. Place the entire group of routes, if it can be useful. If you need more on the routes, I can attach screenshots!
I would appreciate your help thank you!
If you just allow downloading a file without any authentication then,
You can try this :
Blade File
Download file
From this user can directly download the file. Just need to add file path in href and download attribute.
or else remove the middleware AUTH if you don't want to Authenticate the user.
And you want to authenticate the user then need route:list and middleware details.
I found the solution! the problem was that my get request was made this way.
https://mysite.it/admin/area_download/document/example.pdf
the final PDF extension creates system error. Avoiding the final extension such as:
https://mysite.it/admin/area_download/document/example.pdf/go
Problem solved!

How to display a flash message along with redirect without using Session in laravel?

I have a part of code which checks for the file uploaded by user. If I find that is file is not a valid png/jpg/jpeg file then I am deactivating the user and logging him out from the site and deleting his session and then redirecting him to login page. I want to display the error to him that he/she is trying to upload an invalid file. I am not able to find anything on how to display it without using Session.
Any help will be appreciated.
You can create new session messages after flush the session, these messages will delete after shown
controller
$is_valid // boolean
if(!is_valid) {
// if file is not valid, do something
...
// clear all session
session()->flush()
// redirect user to login page and send session message
return redirect()->route("login")
->with("message","Your file is not valid")
->with("message_type","danger");
login.blade
#if(session()->has("message"))
<div class="alert alert-{{session("message_type")}}">
<p> {{session("message")}} </p>
</div>
#endif
For example, if you don't want to use session for some reason, this is a simple code to show the error message in a specific view and then we give to the user a link to login again.
UploadController.php
public function myUpload(){
//... some stuff and then logout the user, delete the session
return view('uploadError', ['uploadError' => 'for some reason...']);
}
uploadError.blade.php
Error uploading the file: {{ $uploadError }}
Login again
In your case, you can't use the error sessions system because when you delete it, in this moment the user haven't a session, and the response has not session. And if you redirect to the login page from the controller, this login request will generate the new session, not before.

Preview Laravel Mail before sending

I want to edit my mail and change everything, if I want, as shown here.
Ive imported my file and created a test route to view the page:
use Illuminate\Mail\Markdown;
Route::get('/mail/html', function () {
$markdown = new Markdown(view(), config('mail.markdown'));
return $markdown->render('vendor.mail.html.message'); // or ..markdown.message
});
However, Im having variable errors for #slot. How to view my change/see what the mail looks like before sending? Another package for that?
Thanks
To preview your email in browser please add below code to route
Route::get('preview-notification', function () {
$markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));
$data = "Your data to be use in blade file";
return $markdown->render("path-of-your-templete-blade-file", $data]);
});
and you will be access your templete using
http://your-application-url/preview-notification
This is the recommended way by the Laravel community
kunal has a nice quick solution and that's how I used to do it. But now I use mailtrap.io to test emails because it allows you to replicate the whole process of sending an email.
Create a (free) account with mailtrap.io.
Add your mailtrap username and password in .env file.
By the way, Laravel is already configured to use mailtrap by default, so it is their recommended way to test emails.
Watch how Jeffrey Ways does it in his lesson:
Laravel 5.4 From Scratch: Sending Email
https://laracasts.com/series/laravel-from-scratch-2017/episodes/26
If you want to test in locally. You can put echo command in blade file at end and put die; this way you can test.Suppose you have test-email.blade.php
test-email.blade.php // file name
This is tets mail
<?php echo "test"; die; ?>
Hope it helps!

Resources