Skip to content

Instantly share code, notes, and snippets.

@edgrosvenor
Last active July 22, 2021 07:02
Show Gist options
  • Save edgrosvenor/de3c368466ad16efb302efe1f3b10144 to your computer and use it in GitHub Desktop.
Save edgrosvenor/de3c368466ad16efb302efe1f3b10144 to your computer and use it in GitHub Desktop.
A quick and easy way to let authenticated users share the current page using a temporary signed url.
<div>
@auth
<div class="rounded-md bg-blue-50 p-4 my-4">
<div class="flex">
<div class="flex-shrink-0">
<!-- Heroicon name: solid/information-circle -->
<svg class="h-5 w-5 text-blue-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd" />
</svg>
</div>
<div class="ml-3 flex-1 md:flex md:justify-between">
<p class="text-sm text-blue-700">
You can give anyone access to this page by sending them a special link that works for one day.
</p>
<p class="mt-3 text-sm md:mt-0 md:ml-6">
<button id="copy" data-clipboard-text="{{ \Illuminate\Support\Facades\URL::temporarySignedRoute(request()->route()->getName(), now()->addDay(), request()->route()->parameters) }}" class="whitespace-nowrap font-medium text-blue-700 hover:text-blue-600">Copy Link <span aria-hidden="true">&rarr;</span></button>
</p>
</div>
</div>
</div>
@endauth
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
<script>
var clipboard = new ClipboardJS('#copy');
clipboard.on('success', function (e) {
document.getElementById('copy').innerText = 'Copied!';
e.clearSelection();
alert('The link has been copied to your clipboard. Just paste it into a text or email to grant a guest user access to this page for 24 hours.');
});
</script>
</div>
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class SignedOrLoggedIn
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (! auth('web')->check() && ! $request->hasValidSignature(true)) {
abort(403);
}
return $next($request);
}
}
@edgrosvenor
Copy link
Author

SignedOrLoggedIn is the middleware I use to protect the routes on which I use this component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment