-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lesson 4: adding ability to require email verification
- Loading branch information
Showing
13 changed files
with
180 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' | ||
import Token from 'App/Models/Token' | ||
|
||
export default class VerifyEmailController { | ||
public async index({ view, auth }: HttpContextContract) { | ||
await auth.user?.sendVerifyEmail() | ||
return view.render('emails/verify') | ||
} | ||
|
||
public async verify({ response, session, params, auth }: HttpContextContract) { | ||
const user = await Token.getTokenUser(params.token, 'VERIFY_EMAIL') | ||
const isMatch = user?.id === auth.user?.id | ||
|
||
// if token is valid and bound to a user, but user is not authenticated | ||
if (user && !auth.user) { | ||
// return to login page & verify email after successful login | ||
session.put('isVerifyingEmail', true) | ||
return response.redirect().toPath('/') | ||
} | ||
|
||
// if token is invalid, not bound to a user, or does not match the auth user | ||
if (!user || !isMatch) { | ||
// handle invalid token | ||
session.flash('token', 'Your token is invalid or expired') | ||
return response.redirect().toRoute('verify.email') | ||
} | ||
|
||
user.isEmailVerified = true | ||
await user.save() | ||
await Token.expireTokens(user, 'verifyEmailTokens') | ||
|
||
return response.redirect().toPath('/') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { BaseMailer, MessageContract } from '@ioc:Adonis/Addons/Mail' | ||
import User from 'App/Models/User' | ||
import Env from '@ioc:Adonis/Core/Env' | ||
import Route from '@ioc:Adonis/Core/Route' | ||
|
||
export default class VerifyEmail extends BaseMailer { | ||
constructor(private user: User, private token: string) { | ||
super() | ||
} | ||
/** | ||
* WANT TO USE A DIFFERENT MAILER? | ||
* | ||
* Uncomment the following line of code to use a different | ||
* mailer and chain the ".options" method to pass custom | ||
* options to the send method | ||
*/ | ||
// public mailer = this.mail.use() | ||
|
||
/** | ||
* The prepare method is invoked automatically when you run | ||
* "VerifyEmail.send". | ||
* | ||
* Use this method to prepare the email message. The method can | ||
* also be async. | ||
*/ | ||
public prepare(message: MessageContract) { | ||
const domain = Env.get('DOMAIN') | ||
const path = Route.makeUrl('verify.email.verify', [this.token]) | ||
const url = domain + path | ||
message | ||
.subject('Please Verify Your Email') | ||
.from('[email protected]') | ||
.to(this.user.email) | ||
.html(` | ||
Please click the following link to verify your email | ||
<a href="${url}">Verify email</a> | ||
`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' | ||
|
||
export default class VerifiedEmail { | ||
public async handle({ auth, view }: HttpContextContract, next: () => Promise<void>) { | ||
if (auth.user && !auth.user.isEmailVerified) { | ||
view.share({ nonVerifiedEmail: true }) | ||
} | ||
|
||
// code for middleware goes here. ABOVE THE NEXT CALL | ||
await next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@layout('layouts/app') | ||
|
||
@section('content') | ||
|
||
<div class="column"> | ||
{{ inspect(flashMessages.all()) }} | ||
|
||
<h1>Please Verify Your Email</h1> | ||
<p> | ||
We've send you an email with a link to verify your email, please click that link to continue. | ||
</p> | ||
</div> | ||
|
||
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@layout('layouts/app') | ||
|
||
@section('content') | ||
|
||
<div class="column"> | ||
{{ inspect(flashMessages.all()) }} | ||
|
||
@if (auth.user) | ||
@if (nonVerifiedEmail) | ||
<h1>You're Email Is NOT Verified</h1> | ||
@else | ||
<h1>You're Email Is Verified</h1> | ||
@endif | ||
@else | ||
<h1>You're Not Authenticated</h1> | ||
@endif | ||
</div> | ||
|
||
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters