1717 along with this program. If not, see <https://www.gnu.org/licenses/>.
1818"""
1919import datetime
20- import os
2120
2221from flask import Blueprint
2322from flask import current_app
24- from flask import redirect
2523from flask import render_template
2624from flask import request
2725from itsdangerous import BadSignature
3028from PyMatcha .utils .confirm_token import confirm_token
3129from PyMatcha .utils .confirm_token import generate_confirmation_token
3230from PyMatcha .utils .decorators import validate_params
31+ from PyMatcha .utils .errors import BadRequestError
3332from PyMatcha .utils .errors import NotFoundError
3433from PyMatcha .utils .mail import send_mail_html
34+ from PyMatcha .utils .static import FRONTEND_EMAIL_CONFIRMATION_URL
3535from PyMatcha .utils .success import Success
3636
3737
4040auth_email_bp = Blueprint ("auth_email" , __name__ )
4141
4242
43- @auth_email_bp .route ("/auth/confirm/<token>" , methods = ["GET " ])
43+ @auth_email_bp .route ("/auth/confirm/<token>" , methods = ["POST " ])
4444def confirm_email (token ):
4545 current_app .logger .debug ("/auth/confirm/{} -> Call" .format (token ))
4646 try :
4747 email , token_type = confirm_token (token , expiration = 7200 )
4848 except (SignatureExpired , BadSignature ) as e :
4949 if e == SignatureExpired :
5050 current_app .logger .debug ("/auth/confirm -> Signature Expired" )
51- return redirect ( "/?type=confirm&success=false&message= Signature expired " )
51+ raise BadRequestError ( " Signature Expired." , "Request another email confirmation and try again. " )
5252 else :
5353 current_app .logger .debug ("/auth/confirm -> Bad Expired" )
54- return redirect ( "/?type=confirm&success=false&message= Bad Signature" )
54+ raise BadRequestError ( " Bad Signature." , "Request another password reset and try again. " )
5555 else :
5656 if token_type != "confirm" :
5757 current_app .logger .debug ("/auth/confirm -> Wrong token type" )
58- return redirect ( "/?type=confirm&success=false&message= Wrong token type" )
58+ raise BadRequestError ( " Wrong token type. " )
5959 try :
6060 u = get_user (email )
6161 except NotFoundError :
6262 current_app .logger .debug ("/auth/confirm -> User not found" )
63- return redirect ( "/?type=confirm&success=false&message= User not found" )
63+ raise NotFoundError ( " User not found. " )
6464 if u .is_confirmed :
6565 current_app .logger .debug ("/auth/confirm -> User already confirmed" )
66- return redirect ( "/?type=confirm&success=false&message=User already confirmed" )
66+ raise BadRequestError ( "Email already confirmed" , " " )
6767 else :
6868 u .is_confirmed = True
6969 u .confirmed_on = datetime .datetime .utcnow ()
7070 u .save ()
7171 current_app .logger .debug ("/auth/confirm -> User {} confirmed." .format (u .id ))
72- return redirect ( "/?type=confirm&success=true&message=User confirmed " )
72+ return Success ( "Confirmation successfull " )
7373
7474
7575@auth_email_bp .route ("/auth/confirm/new" , methods = ["POST" ])
@@ -90,7 +90,7 @@ def request_new_email_conf():
9090 else :
9191 current_app .logger .debug ("/auth/confirm/new -> User found, sending new confirmation email" )
9292 token = generate_confirmation_token (email = email , token_type = "confirm" )
93- link = os . getenv ( "FRONTEND_BASE_URL" ) + "/auth/confirm/" + token
93+ link = FRONTEND_EMAIL_CONFIRMATION_URL + token
9494 rendered_html = render_template ("confirm_email.html" , link = link )
9595 send_mail_html .delay (dest = data ["email" ], subject = "Confirm your email on PyMatcha" , html = rendered_html )
9696 current_app .logger .debug ("/auth/confirm/new -> New confirmation email sent if user exists in database" )
0 commit comments