Merge remote-tracking branch 'origin/auth' into main

This commit is contained in:
tonyrewin 2022-10-21 08:43:16 +03:00
commit 27cb1c0a69
3 changed files with 19 additions and 23 deletions

View File

@ -4,26 +4,22 @@ from settings import MAILGUN_API_KEY, MAILGUN_DOMAIN
api_url = "https://api.mailgun.net/v3/%s/messages" % MAILGUN_DOMAIN api_url = "https://api.mailgun.net/v3/%s/messages" % MAILGUN_DOMAIN
noreply = "discours.io <noreply@%s>" % MAILGUN_DOMAIN noreply = "discours.io <noreply@%s>" % MAILGUN_DOMAIN
subject = "Confirm email"
tmplt = """<html><body>
Follow the <a href='%s'>link</a> to authorize
</body></html>
"""
async def send_auth_email(user, token): async def send_auth_email(user, token):
try: try:
to = "%s <%s>" % (user.username, user.email) to = "%s <%s>" % (user.name, user.email)
url_with_token = "https://newapi.discours.io/confirm/" + token # TODO: i18n
subject = "Confirm email"
template = "email_confirmation_ru"
response = requests.post( response = requests.post(
api_url, api_url,
auth=("api", MAILGUN_API_KEY), auth=("api", MAILGUN_API_KEY),
data={ data={"from": noreply,
"from": noreply, "to": to,
"to": to, "subject": subject,
"subject": subject, "template": template,
"html": tmplt % url_with_token, "h:X-Mailgun-Variables": "{ \"token\": \"%s\" }" % token}
},
) )
response.raise_for_status() response.raise_for_status()
except Exception as e: except Exception as e:

View File

@ -80,8 +80,8 @@ def create_user(user_dict):
return user return user
def generate_unique_slug(username): def generate_unique_slug(name):
slug = translit(username, "ru", reversed=True).replace(".", "-").lower() slug = translit(name, "ru", reversed=True).replace(".", "-").lower()
with local_session() as session: with local_session() as session:
c = 1 c = 1
user = session.query(User).where(User.slug == slug).first() user = session.query(User).where(User.slug == slug).first()
@ -95,26 +95,26 @@ def generate_unique_slug(username):
@mutation.field("registerUser") @mutation.field("registerUser")
async def register(_, _info, email: str, password: str = "", username: str = ""): async def register(_, _info, email: str, password: str = "", name: str = ""):
"""creates new user account""" """creates new user account"""
with local_session() as session: with local_session() as session:
user = session.query(User).filter(User.email == email).first() user = session.query(User).filter(User.email == email).first()
if user: if user:
raise OperationNotAllowed("User already exist") raise OperationNotAllowed("User already exist")
else: else:
username = username or email.split("@")[0] slug = generate_unique_slug(name)
user_dict = { user_dict = {
"email": email, "email": email,
"username": username, "username": email,
"slug": generate_unique_slug(username) "name": name,
"slug": slug
} }
if password: if password:
user_dict["password"] = Password.encode(password) user_dict["password"] = Password.encode(password)
user = create_user(user_dict) user = create_user(user_dict)
if not password: await auth_send_link(_, _info, email)
user = await auth_send_link(_, _info, email)
return {"user": user} return {"user": user}

View File

@ -154,7 +154,7 @@ type Mutation {
# auth # auth
refreshSession: AuthResult! refreshSession: AuthResult!
registerUser(email: String!, password: String, username: String): AuthResult! registerUser(email: String!, password: String, name: String): AuthResult!
sendLink(email: String!): Result! sendLink(email: String!): Result!
confirmEmail(code: String!): AuthResult! confirmEmail(code: String!): AuthResult!