I would like to understand more regarding Laravel signed URL. The idea is, in my Shopify Online Store, the user can click this link which redirect into a website I developed in Laravel. However I do not fully get it on how can I do this.
So far I did all this by referring to some tutorial I browse in google. This is my web.php:
<?php
use App\Http\Controllers\AuthController;
use App\Http\Controllers\RequestPointTransferController;
use Illuminate\Support\Facades\Route;
use App\Models\User;
use Illuminate\Http\Request;
Route::get('/', function () {
return redirect('request-form');
});
//Redirect to the website I develop for this specific customer (what i plan)
Route::get('request-form', function (Request $request) {
return view('modules.request_point_transfer.index');
})->name('managepoints')->middleware('signed');
Route::get('generate-url',RequestPointTransferController::class);
And my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use App\Services\RequestPointTransferService;
use App\Services\RequestPointTransferAPIService;
class RequestPointTransferController extends Controller
{
protected $RequestPointTransferService;
protected $RequestPointTransferAPIService;
public function __invoke(Request $request)
{
// dd($request->all());
$url = URL::signedRoute('managepoints',now()->addMinutes(20));
return $url;
}
I dont fully understand since the url has been generated, how can I use the url for my Shopify Online Store? Is it like in the Shopify, I can display the URL (probably in the account settings for the online store) example: http://127.0.0.1:8000/mywebsite/myemail.gmail.com and when they click it will go to my website? Sorry I am new to this. Thank you very much.
Here’s a breakdown of how to use Laravel signed URLs with your Shopify store:
1. Generating Signed URLs in Laravel:
Your RequestPointTransferController is on the right track:
PHP
public function __invoke(Request $request)
{
// Replace 'your-email@example.com' with the actual user's email
$userEmail = $request->user()->email; // Assuming you have user authentication
$url = URL::signedRoute('managepoints', ['userEmail' => $userEmail], now()->addMinutes(20));
return $url;
}
This code retrieves the user’s email (assuming you have user authentication) and signs a URL for the managepoints route with the email as a parameter.
The URL expires after 20 minutes (adjust as needed).
2. Passing the Signed URL to Shopify:
You’ll need to modify your Shopify theme or app to display the generated signed URL. This might involve:
Using a Liquid template tag in your Shopify theme to dynamically insert the URL. You’ll need to fetch the URL from your Laravel app using an API call or store it in a custom field associated with the user in Shopify.
Sending the URL as part of an API response from your Shopify app.
The specific method depends on your approach, but you essentially need to pass the URL to the Shopify environment where the user can access it.
3. Handling the Signed URL in Shopify:
When the user clicks the URL in Shopify, they’ll be redirected to your Laravel application’s managepoints route.
Laravel will verify the signature in the URL using the secret key configured in your .env file.
If the signature is valid, the user will be granted access to the managepoints route’s functionality (e.g., the request form).
4. Important Considerations:
Security: Ensure that the secret key used for signing URLs is kept confidential. Store it securely in your .env file and exclude it from version control.
URL Expiration: Choose an appropriate expiration time for your signed URLs to balance security and user experience.
User Authentication: Implement proper user authentication in your Laravel application to ensure only authorized users can access the managepoints route.
Additional Tips:
Explore Shopify’s documentation on Liquid tags and app development for guidance on integrating with your theme or app.
Consider using a Laravel package like spatie/laravel-signed-url for easier signed URL generation and management.
By following these steps, you can create a secure system where users in your Shopify store can be redirected to specific routes in your Laravel application using signed URLs.