-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #365 from mitchdennett/email-verify
Email Verification Feature
- Loading branch information
Showing
13 changed files
with
532 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
""" Verify Email Module """ | ||
|
||
import time | ||
|
||
from masonite.auth.Sign import Sign | ||
|
||
|
||
class MustVerifyEmail: | ||
"""Class To Verify User Email | ||
""" | ||
|
||
def verify_email(self, mail_manager, request): | ||
mail = mail_manager.helper() | ||
sign = Sign() | ||
|
||
token = sign.sign('{0}::{1}'.format(self.id, time.time())) | ||
link = '{0}/email/verify/{1}'.format(request.environ['HTTP_HOST'], token) | ||
|
||
mail.to(self.email) \ | ||
.template('auth/verifymail', {'name': self.name, 'email': self.email, 'link': link}) \ | ||
.subject('Please Confirm Your Email').send() |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .Auth import Auth | ||
from .Csrf import Csrf | ||
from .Sign import Sign | ||
from .MustVerifyEmail import MustVerifyEmail |
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,86 @@ | ||
""" The ConfirmController Module """ | ||
import time | ||
import datetime | ||
|
||
from masonite.auth import Auth | ||
from masonite.auth.Sign import Sign | ||
from masonite.request import Request | ||
from masonite.view import View | ||
from masonite.auth import MustVerifyEmail | ||
from app.User import User | ||
|
||
|
||
class ConfirmController: | ||
"""The ConfirmController class. | ||
""" | ||
|
||
def __init__(self): | ||
"""The ConfirmController Constructor | ||
""" | ||
pass | ||
|
||
def verify_show(self, request: Request, view: View): | ||
"""Show the Verify Email page for unverified users. | ||
Arguments: | ||
Request {masonite.request.request} -- The Masonite request class. | ||
Request {masonite.view.view} -- The Masonite view class. | ||
Returns: | ||
[type] -- [description] | ||
""" | ||
|
||
return view.render('auth/verify', {'app': request.app().make('Application'), 'Auth': Auth(request)}) | ||
|
||
def confirm_email(self, request: Request, view: View): | ||
"""Confirm User email and show the correct response. | ||
Arguments: | ||
Request {masonite.request.request} -- The Masonite request class. | ||
Request {masonite.view.view} -- The Masonite view class. | ||
Returns: | ||
[type] -- [description] | ||
""" | ||
|
||
sign = Sign() | ||
token = sign.unsign(request.param('id')) | ||
|
||
if token is not None: | ||
tokenParts = token.split("::") | ||
if len(tokenParts) > 1: | ||
id = tokenParts[0] | ||
user = self.get_user(id) | ||
|
||
if user.verified_at is None: | ||
timestamp = datetime.datetime.fromtimestamp(float(tokenParts[1])) | ||
now = datetime.datetime.now() | ||
timestamp_plus_10 = timestamp + datetime.timedelta(minutes=10) | ||
|
||
if now < timestamp_plus_10: | ||
user.verified_at = datetime.datetime.now() | ||
user.save() | ||
|
||
return view.render('auth/confirm', {'app': request.app().make('Application'), 'Auth': Auth(request)}) | ||
|
||
return view.render('auth/error', {'app': request.app().make('Application'), 'Auth': Auth(request)}) | ||
|
||
def get_user(self, id): | ||
"""Get the user from the database | ||
Arguments: | ||
id {str} -- The user id | ||
Returns: | ||
[User] -- [User model] | ||
""" | ||
|
||
return User.find(id) | ||
|
||
def send_verify_email(self, request: Request): | ||
user = request.user() | ||
|
||
if isinstance(user, MustVerifyEmail): | ||
request.app().resolve(user.verify_email) | ||
|
||
return request.redirect('/home') |
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,12 @@ | ||
{% extends 'auth/base.html' %} | ||
|
||
{% block content %} | ||
<div class="col-xs-4 col-xs-offset-4"> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading">Verified</div> | ||
<div class="panel-body"> | ||
Thank you for confirming your email. Click here to go to the <a href="/home">home</a> page. | ||
</div> | ||
</div> | ||
</div> | ||
{% endblock %} |
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 @@ | ||
{% extends 'auth/base.html' %} | ||
|
||
{% block content %} | ||
<div class="col-xs-4 col-xs-offset-4"> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading">Verifying Error</div> | ||
<div class="panel-body"> | ||
Confirming email failed. Click here to go <a href="/home">home</a> | ||
</div> | ||
</div> | ||
</div> | ||
{% endblock %} |
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,13 @@ | ||
{% extends 'auth/base.html' %} | ||
|
||
{% block content %} | ||
<div class="col-xs-4 col-xs-offset-4"> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading">Verify</div> | ||
<div class="panel-body"> | ||
Please check your email and follow the link to verify your email. If | ||
you need us to resend the email. Click <a href="/email/verify/send">here</a> | ||
</div> | ||
</div> | ||
</div> | ||
{% endblock %} |
Oops, something went wrong.