diff --git a/auth/authenticate.py b/auth/authenticate.py index e4e1048d..ad07a39e 100644 --- a/auth/authenticate.py +++ b/auth/authenticate.py @@ -67,6 +67,9 @@ class JWTAuthenticate(AuthenticationBackend): if payload is None: return AuthCredentials(scopes=[]), AuthUser(user_id=None) + if not payload.device in ("pc", "mobile"): + return AuthCredentials(scopes=[]), AuthUser(user_id=None) + scopes = User.get_permission(user_id=payload.user_id) return AuthCredentials(user_id=payload.user_id, scopes=scopes, logged_in=True), AuthUser(user_id=payload.user_id) @@ -89,8 +92,11 @@ class EmailAuthenticate: raise InvalidToken("invalid token") with local_session() as session: user = session.query(User).filter_by(id=payload.user_id).first() - if not user: - raise Exception("user not exist") + if not user: + raise Exception("user not exist") + if not user.emailConfirmed: + user.emailConfirmed = True + session.commit() auth_token = await Authorize.authorize(user) return (auth_token, user) diff --git a/auth/email.py b/auth/email.py index df8c1a37..8acfd689 100644 --- a/auth/email.py +++ b/auth/email.py @@ -11,11 +11,20 @@ MAILGUN_FROM = "postmaster " % (MAILGUN_DOMAIN) AUTH_URL = "%s/email_authorize" % (BACKEND_URL) +async def send_confirm_email(user): + text = "To confirm registration follow the link" + await send_email(user, text) + async def send_auth_email(user): + text = "To enter the site follow the link" + await send_email(user, text) + +async def send_email(user, text): token = await EmailAuthenticate.get_email_token(user) to = "%s <%s>" % (user.username, user.email) - text = "%s?token=%s" % (AUTH_URL, token) + auth_url_with_token = "%s?token=%s" % (AUTH_URL, token) + text = text % (auth_url_with_token) response = requests.post( MAILGUN_API_URL, auth = ("api", MAILGUN_API_KEY), @@ -23,7 +32,7 @@ async def send_auth_email(user): "from": MAILGUN_FROM, "to": to, "subject": "authorize log in", - "text": text + "html": text } ) response.raise_for_status() diff --git a/resolvers/auth.py b/resolvers/auth.py index 0ed51d80..c9e7be08 100644 --- a/resolvers/auth.py +++ b/resolvers/auth.py @@ -5,7 +5,7 @@ from auth.authorize import Authorize from auth.identity import Identity from auth.password import Password from auth.validations import CreateUser -from auth.email import send_auth_email +from auth.email import send_confirm_email, send_auth_email from orm import User from orm.base import local_session from resolvers.base import mutation, query @@ -31,7 +31,7 @@ async def register(*_, email: str, password: str = ""): create_user.username = email.split('@')[0] if not password: user = User.create(**create_user.dict()) - await send_auth_email(user) + await send_confirm_email(user) return { "user": user } else: create_user.password = Password.encode(create_user.password) @@ -49,7 +49,7 @@ async def login(_, info: GraphQLResolveInfo, email: str, password: str = ""): if not password: await send_auth_email(orm_user) - return {"error" : ""} + return {} try: device = info.context["request"].headers['device']