Skip to content

Commit

Permalink
Add custom views to required access code (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesargb committed Apr 24, 2023
1 parent 8465ed3 commit 1ff50d9
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ $magiclink->protectWithAccessCode('secret');
$urlToSend = $magiclink->url;
```

### Custom view for access code

You can customize the view of the access code form with the config file `magiclink.php`:

```php
'access_code' => [
'view' => 'magiclink::access-code', // Change with your view
],
```

This is the [default view](/resources/views/ask-for-access-code-form.blade.php)

## Lifetime

By default a link will be available for 72 hours after your creation. We can
Expand Down
3 changes: 3 additions & 0 deletions config/magiclink.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@
*/
'disable_default_route' => false,

'access_code' => [
'view' => 'magiclink::ask-for-access-code-form',
]
];
5 changes: 4 additions & 1 deletion src/AccessCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ private function getResponseAccessCodeFromCookie()
}
}

return response()->view('magiclink::ask-for-access-code-form', [], 403);
return response()->view(
config('magiclink.access-code.view', 'magiclink::ask-for-access-code-form'),
[],
403);
}

return null;
Expand Down
15 changes: 15 additions & 0 deletions tests/AccessCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,19 @@ public function test_forbidden_if_provide_access_code_of_other_link()
->get($magiclinkOther->url)
->assertStatus(403);
}

public function test_forbidden_if_protected_with_access_code_custmo()
{
$magiclink = MagicLink::create(new ResponseAction(function () {
return 'the big secret';
}));

config(['magiclink.access-code.view' => 'access-code-custom']);

$magiclink->protectWithAccessCode('1234');

$this->get($magiclink->url)
->assertStatus(403)
->assertViewIs('access-code-custom');
}
}
75 changes: 75 additions & 0 deletions tests/stubs/resources/views/access-code-custom.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!DOCTYPE html>
<html>
<head>
<title>Password protected</title>

<link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">

<style>
html, body {
height: 100%;
}
body {
margin: 0;
padding: 0;
width: 100%;
display: table;
font-weight: 100;
font-family: 'Lato';
}
.container {
text-align: center;
display: table-cell;
vertical-align: middle;
}
.content {
text-align: center;
display: inline-block;
}
.title {
font-size: 36px;
}
.form-control {
border: 1px solid #ccc;
padding: 10px 20px;
}
.hidden {
display: none;
}
.text-danger {
color: #d9534f;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">Enter access code</div>

<form method="GET">
{{ csrf_field() }}

<div class="form-group">

<input type="password" name="access-code" placeholder="Please enter access code" class="form-control" tabindex="1" autofocus />
@if (Request::get('access-code'))
<div class="text-danger">Access code is wrong</div>
@else
<div class="small help-block">And press enter</div>
@endif
</div>

<input type="submit" class="hidden" />

</form>
</div>
</div>
</body>
</html>

0 comments on commit 1ff50d9

Please sign in to comment.