Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit 3800a0e

Browse files
author
Holger Lösken
committed
Use with protected content
1 parent 913f39a commit 3800a0e

8 files changed

Lines changed: 66 additions & 17 deletions

File tree

DOCUMENTATION.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## Protecting content
2+
3+
You can use magic links with Statamics [Protecting Content](https://statamic.dev/protecting-content) feature.
4+
When [defining a password form](https://statamic.dev/protecting-content#password-form) just use one of these tags to
5+
show a magic link login below the normal password login form:
6+
7+
* `{{ magiclink:login-link }}`: Displays a completely rendered `a` tag. For customizing the html, see [Customization](#Customization).
8+
* `{{ magiclink:login-route }}`: Return the route to the magic link form. Use this with your own markup.
9+
110
## Permissions
211

312
The access to settings of MagicLink can be restricted to certain users. When navigation to _Permissions_ inside the

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/views/auth/login.blade.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,7 @@
6464
{{ __('Forgot password?') }}
6565
</a>
6666
</div>
67-
<div class="w-full text-center mt-2">
68-
<a href="{{ route('magiclink.show-send-link-form')}}" class="forgot-password-link text-sm opacity-75 hover:opacity-100">
69-
<svg class="w-4 h-4 align-middle" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
70-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 3v4M3 5h4M6 17v4m-2-2h4m5-16l2.286 6.857L21 12l-5.714 2.143L13 21l-2.286-6.857L5 12l5.714-2.143L13 3z"></path>
71-
</svg>
72-
{{ __('magiclink::web.login_magic_link') }}
73-
</a>
74-
</div>
67+
@include('magiclink::partials.login-link')
7568
@endif
7669

7770
@endsection

resources/views/magiclink/send-link-form.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</div>
1818

1919
<div class="w-full text-center mt-2">
20-
<a class="text-white text-sm opacity-75 hover:opacity-100" href="{{ cp_route('login') }}">
20+
<a class="text-white text-sm opacity-75 hover:opacity-100" href="{{ $referer }}">
2121
<svg class="w-4 h-4 align-middle" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
2222
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12.066 11.2a1 1 0 000 1.6l5.334 4A1 1 0 0019 16V8a1 1 0 00-1.6-.8l-5.333 4zM4.066 11.2a1 1 0 000 1.6l5.334 4A1 1 0 0011 16V8a1 1 0 00-1.6-.8l-5.334 4z"></path>
2323
</svg>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="w-full text-center mt-2">
2+
<a href="{{ route('magiclink.show-send-link-form')}}" class="forgot-password-link text-sm opacity-75 hover:opacity-100">
3+
<svg class="w-4 h-4 align-middle" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
4+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 3v4M3 5h4M6 17v4m-2-2h4m5-16l2.286 6.857L21 12l-5.714 2.143L13 21l-2.286-6.857L5 12l5.714-2.143L13 3z"></path>
5+
</svg>
6+
{{ __('magiclink::web.login_magic_link') }}
7+
</a>
8+
</div>

src/Http/Controllers/MagicLinkController.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
use Codedge\MagicLink\Events\LinkCreated;
88
use Codedge\MagicLink\MagicLinkManager;
99
use Illuminate\Http\Request;
10+
use Illuminate\Support\Facades\Session;
1011
use Statamic\Facades\User;
1112

1213
final class MagicLinkController extends BaseWebController
1314
{
15+
private const LOGIN_FORM_REFERER_SESSION_KEY = 'login-form-referer';
16+
1417
protected MagicLinkManager $magicLinkRepository;
1518
protected User $user;
1619

@@ -21,7 +24,11 @@ public function __construct(MagicLinkManager $magicLinkRepository)
2124

2225
public function showSendLinkForm()
2326
{
24-
return view('magiclink::magiclink.send-link-form');
27+
Session::put(self::LOGIN_FORM_REFERER_SESSION_KEY, \request()->headers->get('referer'));
28+
29+
return view('magiclink::magiclink.send-link-form', [
30+
'referer' => Session::get('login-form-referer'),
31+
]);
2532
}
2633

2734
public function sendLink(Request $request)
@@ -40,7 +47,7 @@ public function sendLink(Request $request)
4047
session()->flash('success', __('magiclink::web.address_exists_then_email'));
4148

4249
return [
43-
'redirect' => cp_route('login'),
50+
'redirect' => Session::get(self::LOGIN_FORM_REFERER_SESSION_KEY),
4451
];
4552
}
4653
}

src/ServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ final class ServiceProvider extends AddonServiceProvider
2929
__DIR__.'/../public/js/statamic-magiclink.js',
3030
];
3131

32+
protected $tags = [
33+
\Codedge\MagicLink\Tags\Magiclink::class,
34+
];
35+
3236
protected $listen = [
3337
LinkCreated::class => [
3438
SendLinkNotification::class,

src/Tags/Magiclink.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codedge\MagicLink\Tags;
6+
7+
use Codedge\MagicLink\Repositories\SettingsRepository;
8+
use Statamic\Tags\Tags;
9+
10+
class Magiclink extends Tags
11+
{
12+
protected SettingsRepository $settingsRepository;
13+
14+
public function __construct(SettingsRepository $settingsRepository)
15+
{
16+
$this->settingsRepository = $settingsRepository;
17+
}
18+
19+
public function loginLink()
20+
{
21+
return ($this->settingsRepository->isEnabled() ? view('magiclink::partials.login-link') : '');
22+
}
23+
24+
public function loginRoute(): string
25+
{
26+
return ($this->settingsRepository->isEnabled() ? route('magiclink.show-send-link-form') : '');
27+
}
28+
}

0 commit comments

Comments
 (0)