Secure & Temporary Sharing in Laravel Using Laravel ShareLink

 Secure & Temporary Sharing in Laravel Using Laravel ShareLink

Secure & Temporary Sharing in Laravel Using Laravel ShareLink

In major applications we usually generates files, images, invoices and other files which we usually share with clients/customers but sharing these file URLs publicly is not safe. Not only files but on other aspects like sharing any route link or adding password to Api's etc.

And it makes such links:

❌ permanently public is insecure

❌ protected by login adds friction

❌ custom-built every time is error-prone

This is where Laravel ShareLink shines ✨

It provides a clean, secure, and scalable way to generate temporary and revocable share links for files, routes, apis and data.

So in this article we will see how to generate temporary urls for files, route and other purposes in Laravel

🧠 What We’ll Cover in This Blog

📌 What Laravel ShareLink is ?

📌 Real-life use cases

📌 Password-protected share links

📌 Revoking links programmatically

📌 Auto-cleanup using scheduler

📌 Why this matters in production

📌 Best practices & final thoughts

📦 What Is Laravel ShareLink?

Laravel ShareLink is an open-source Laravel package that allows you to:

✅ Generate temporary shareable links

✅ Set expiration & click limits

✅ Protect links with passwords

✅ Revoke links anytime

✅ Restrict access via IPs

✅ Automatically clean expired links

In simple terms — it gives you secure access control without authentication overhead.

🏗️ Installation (Quick Overview)

composer require grazulex/laravel-sharelink
php artisan vendor:publish --tag="sharelink-migrations"
php artisan migrate

Optional config publish

php artisan vendor:publish --tag="sharelink-config"

🚀 Real-World Use Cases & Examples

Let's see how we can implement different use cases in real life projects.

🧾 1. Generate File sharing Link (Invoice & Billing PDF Sharing)

📌 Scenario

You want to share invoices with clients without login, but still keep them secure.

💡 Implementation

use Grazulex\ShareLink\Facades\ShareLink;
public function generateLink()
{
    $link = ShareLink::create(
        storage_path("app/invoices/INV-{$invoice->id}.pdf")
    )
    ->expiresIn(1440) // 24 hours
    ->maxClicks(5)
    ->metadata([
        'invoice_id' => $invoice->id,
        'client_id' => $invoice->client_id,
    ])
    ->generate();

    return response()->json([
        'share_link' => $link->url,
        'expires_at' => $link->expires_at,
        'max_clicks' => $link->max_clicks,
    ]);
}

🔐 2. Generate Share Link With Password Protection

📌 Scenario

Some documents need extra securitysalary slips, offer letters, legal files.

use Grazulex\ShareLink\Facades\ShareLink;
public function generateLink()
{
    $link = ShareLink::create('/storage/hr/offer-letter.pdf')
    ->expiresIn(48 * 60) // 48 hours
    ->withPassword('secret') // simple verification
    ->maxClicks(2)
    ->generate();

    return response()->json([
        'share_link' => $link->url,
        'expires_at' => $link->expires_at,
        'max_clicks' => $link->max_clicks,
    ]);
}

🔐 User must enter a password before accessing the resource. Following is an example that how you can pass password on request to access the encrypted link.

Secure & Temporary Sharing in Laravel Using Laravel ShareLink


🎯 Best Used For


❌ 3. Revoking a Share Link Instantly

📌 Scenario

A document was accessed, or access should be stopped immediately.

💡 Implementation

use Grazulex\ShareLink\Facades\ShareLink;
public function revokeAccess()
{
    $link = ShareLink::findByHash($hash);

    if ($link) {
        $link->revoke();
    }

    return response()->json([
        'message' => 'Share link revoked successfully.',
    ]);
}


🚫 Once revoked:

  • Link becomes invalid instantly
  • No further access possible

🌐 4. Guest Order Tracking (E-commerce)

📌 Scenario

Allow customers to track orders without account creation.

use Grazulex\ShareLink\Facades\ShareLink;
public function generateLink()
{
    $link = ShareLink::create([
        'type' => 'route',
        'route' => 'orders.track',
        'parameters' => ['order' => $order->uuid],
    ])
    ->expiresIn(72 * 60)
    ->generate();


    return response()->json([
        'share_link' => $link->url,
        'expires_at' => $link->expires_at,
        'max_clicks' => $link->max_clicks,
    ]);
}

👨‍💼 5. HR Offer Letter Sharing Flow

use Grazulex\ShareLink\Facades\ShareLink;
public function generateLink()
{
    $link = ShareLink::create($offerLetterPath)
    ->withPassword($candidate->dob)
    ->expiresIn(48 * 60)
    ->maxClicks(1)
    ->generate();

    return response()->json([
        'share_link' => $link->url,
        'expires_at' => $link->expires_at,
        'max_clicks' => $link->max_clicks,
    ]);
}

🧠 Auto-revoke after acceptance for best security.

🧪 6 Client / QA Preview Access

use Grazulex\ShareLink\Facades\ShareLink;
public function generateLink()
{
    $link = ShareLink::create([
        'type' => 'route',
        'route' => 'feature.preview',
        'parameters' => ['feature' => 'new-dashboard'],
    ])
    ->expiresIn(120)
    ->generate();

    return response()->json([
        'share_link' => $link->url,
        'expires_at' => $link->expires_at,
        'max_clicks' => $link->max_clicks,
    ]);
}

🧹 7. Schedule Auto Cleanup Using Prune Command

📌 Why This Is Critical

Expired links should never live forever in production systems.

💡 Scheduler Setup

Open App\Console\Kernel.php:

use Grazulex\ShareLink\Facades\ShareLink;
protected function schedule(Schedule $schedule)
{
    $schedule->command('sharelink:prune')->daily();
}

🧼 This:

🧠 Best Practices for Production

✔ Always set expiration

✔ Use password for sensitive data

✔ Limit clicks when possible

✔ Revoke after usage

✔ Enable scheduled pruning

✔ Log metadata for auditing

❓ Why Laravel ShareLink Is Necessary

Without it:

❌ Public URLs

❌ No access control

❌ No audit logs

❌ Custom fragile logic

With ShareLink:

Secure by default

✅ Minimal code

✅ Reusable patterns

Enterprise-ready

It replaces multiple custom hacks with one reliable solution.

💭 Final Thoughts

Laravel ShareLink is more than a sharing tool. Tt’s a secure access layer you can reuse across:

If you care about security, UX, and scalability, this package deserves a place in your Laravel stack ⭐

Thank you for reading this article 😊

For any query do not hesitate to comment 💬


Previous Post Next Post

Contact Form