Skip to content

Commit 05c0451

Browse files
authored
Merge pull request #107 from leafsphp/staging
Staging
2 parents b84b5c8 + 2ef622e commit 05c0451

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

src/docs/auth/protected-routes.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,46 @@ app()->get('/login', ['middleware' => 'auth.guest', function () {
102102

103103
If a logged in user tries to access a route protected by the `auth.guest` middleware, they will be redirected to the `/dashboard` route by default.
104104

105+
## Email verification middleware <Badge>NEW</Badge>
106+
107+
Leaf Auth provides middleware to protect routes that should only be accessible to only users with a certain email verification status. The `auth.verified` middleware ensures that only verified users can access certain routes.
108+
109+
```php
110+
app()->group('/dashboard', [
111+
'middleware' => 'auth.verified',
112+
function () {
113+
// dashboard routes will only be accessible to verified users
114+
}
115+
]);
116+
117+
app()->get('/some-route', [
118+
'middleware' => 'auth.verified',
119+
function () {
120+
// route will only be accessible to verified users
121+
}
122+
]);
123+
```
124+
125+
While the `auth.unverified` middleware which ensures that only unverified users can access certain routes.
126+
127+
```php
128+
app()->group('/verify', [
129+
'middleware' => 'auth.unverified',
130+
function () {
131+
// verify routes will only be accessible to unverified users
132+
}
133+
]);
134+
135+
app()->get('/some-route', [
136+
'middleware' => 'auth.verified',
137+
function () {
138+
// route will only be accessible to unverified users
139+
}
140+
]);
141+
```
142+
143+
By default, the `auth.verified` middleware will redirect unverified users to the `/auth/verify` route if they are not verified, and the `auth.unverified` middleware will redirect verified users to the `/dashboard` route if they are verified. You can customize this behaviour by defining your own function that should be called when the middleware fails. You can follow the instructions in the next section to learn how to customize the auth middleware.
144+
105145
## Customizing auth middleware
106146

107147
Your application may need you to return different responses for the `auth.required` and `auth.guest` middleware. You can customize the middleware by defining your own function that should be called when the middleware fails.

src/docs/auth/user.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,92 @@ $secretField = auth()->user()->secret_field;
3131

3232
While this may seem like a lot of work, it's a good way to ensure that your user's data is secure and only accessible where needed.
3333

34+
## Email verification <Badge>NEW</Badge>
35+
36+
Email verification is a very important feature in most applications. It allows you to verify that the email address provided by a user is valid and that they have access to it. Leaf Auth by default does not incorporate email verification into the authentication process, but you can easily add it to your application using handy functions added in Auth v3.4.0.
37+
38+
::: tip Database considerations
39+
Leaf does not enforce any database schema on you, however, Leaf will automatically create a nullable `email_verified_at` column in your users table if you do not have this column. No setup is required on your part.
40+
:::
41+
42+
### Generating a verification token
43+
44+
After you have registered a user, you can generate a verification token for the user using the `generateVerificationToken()` method. This method generates a JWT which you can send to the user's email address as part of a link.
45+
46+
```php
47+
$token = auth()->user()->generateVerificationToken();
48+
49+
$verificationLink = "https://example.com/verify?token=$token";
50+
```
51+
52+
You can also pass an expiration time to the `generateVerificationToken()` method. The default expiration time is 10 minutes.
53+
54+
```php:no-line-numbers
55+
$token = auth()->user()->generateVerificationToken(time() + 3600); // 1 hour
56+
```
57+
58+
### Verifying a user
59+
60+
When a user clicks on the verification link, you first need to verify the token. You can do this using the `verifyToken()` method. This method returns `true` if the token is valid and `false` if the token is invalid.
61+
62+
```php
63+
$token = request()->get('token');
64+
$isValid = auth()->verifyToken($token);
65+
66+
if ($isValid) {
67+
// Token is valid
68+
} else {
69+
// Token is invalid
70+
}
71+
```
72+
73+
If the token is valid, you can then update the user's `email_verified_at` column to the current time. You can do this using the `verifyEmail()` method. This method returns `true` if the update is successful and `false` if the update is not successful.
74+
75+
```php
76+
$token = request()->get('token');
77+
$isValid = auth()->verifyToken($token);
78+
79+
if ($isValid && auth()->user()->verifyEmail()) {
80+
// Email is verified
81+
} else {
82+
// Could not verify email, missing or invalid token
83+
}
84+
```
85+
86+
### Verification middleware
87+
88+
You can also add a middleware to your routes and route groups to ensure that only users of a certain verification status can access certain routes.
89+
90+
```php
91+
app()->get('/some-route', [
92+
'middleware' => 'auth.verified',
93+
function () {
94+
// route will only be accessible to verified users
95+
}
96+
]);
97+
98+
app()->get('/some-route', [
99+
'middleware' => 'auth.unverified',
100+
function () {
101+
// route will only be accessible to unverified users
102+
}
103+
]);
104+
```
105+
106+
You can view the full documentation on the [middleware page](/docs/auth/protected-routes.html#email-verification-middleware-new).
107+
108+
### Checking verification status
109+
110+
You can check if a user's email is verified using the `isVerified()` method. This method returns `true` if the user's email is verified and `false` if the user's email is not verified.
111+
112+
```php
113+
if (auth()->user()->isVerified()) {
114+
// Email is verified
115+
} else {
116+
// Email is not verified
117+
}
118+
```
119+
34120
## User relationships
35121

36122
Leaf auth comes with a very basic model system that allows you to get/set data related to the current user. For instance, you may want to get all posts by the current user or all transactions by the current user, or maybe add a new purchase to the current user. All these can be done using the user method.

0 commit comments

Comments
 (0)