Skip to content

Commit d676c33

Browse files
committed
Fix account registration
1 parent 9df1f0b commit d676c33

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

common_config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class Config:
3939
# 'password': 'Sekrit',
4040
}
4141
FROM_EMAIL = 'office@example.com'
42+
SUBJECTS = {
43+
'prefix': '[Freenit] ',
44+
'confirm': 'Account confirmation',
45+
'register': 'Account registration',
46+
}
4247

4348
@staticmethod
4449
def init_app(app):

freenit/api/auth.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,13 @@ def post(self, args):
140140
}
141141
host = request.headers.get('Origin', request.url_root)
142142
requestToken = create_access_token(identity, expires_delta=expires)
143-
url = f'{host}/register/{requestToken}'
143+
url = f'{host}/confirm/{requestToken}'
144144
msg = MIMEText(url, 'plain', 'utf-8')
145+
config = current_app.config
146+
subject = config['SUBJECTS']['prefix'] + config['SUBJECTS']['register']
145147
msg['To'] = user.email
146-
msg['From'] = current_app.config['FROM_EMAIL']
147-
msg['Subject'] = 'Freenit message'
148+
msg['From'] = config['FROM_EMAIL']
149+
msg['Subject'] = subject
148150
current_app.sendmail(msg)
149151
return user
150152

@@ -162,11 +164,18 @@ def get(self, token):
162164
user = User.get(id=identity['id'])
163165
except User.DoesNotExist:
164166
abort(404, message='User does not exist')
167+
if user.active:
168+
abort(409, message='User already activated')
169+
user.active = True
170+
user.save()
165171
text = 'Congratulation, your account is confirmed'
166172
msg = MIMEText(text, 'plain', 'utf-8')
167-
msg['From'] = current_app.config['FROM_EMAIL']
168-
msg['Subject'] = 'Freenit message'
169-
current_app.sendmail(user.email, msg)
173+
config = current_app.config
174+
subject = config['SUBJECTS']['prefix'] + config['SUBJECTS']['confirm']
175+
msg['To'] = user.email
176+
msg['From'] = config['FROM_EMAIL']
177+
msg['Subject'] = subject
178+
current_app.sendmail(msg)
170179
return user
171180

172181

freenit/project/common_config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Config:
2929
OPENAPI_VERSION = '2.0.0'
3030
MEDIA_URL = '/media'
3131
MEDIA_PATH = 'media'
32+
ACCOUNT_REQUEST_EXPIRY = 24 # in hours
3233
PASSWORD_RESET_EXPIRY = 2 # in hours
3334
DATABASE = {
3435
'name': 'database.db',
@@ -41,6 +42,12 @@ class Config:
4142
# 'username': 'someone@example.com',
4243
# 'password': 'Sekrit',
4344
}
45+
FROM_EMAIL = 'office@example.com'
46+
SUBJECTS = {
47+
'prefix': '[Freenit] ',
48+
'confirm': 'Account confirmation',
49+
'register': 'Account registration',
50+
}
4451

4552
@staticmethod
4653
def init_app(app):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name='freenit',
12-
version='0.0.24',
12+
version='0.0.25',
1313
description='REST API framework based on Flask-Smorest',
1414
long_description=README,
1515
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)