Skip to content

Commit 558747d

Browse files
authored
fix: Logging in redirects to the right page. (#201)
* fix: Logging in redirects to the right page.
1 parent dbcc486 commit 558747d

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lms/lmsweb/views.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,28 @@ def is_safe_url(target):
9696
)
9797

9898

99+
def get_next_url(url_next_param: Optional[str]):
100+
next_url = url_next_param if url_next_param else None
101+
if not is_safe_url(next_url):
102+
return fail(400, "The URL isn't safe.")
103+
return redirect(next_url or url_for('main'))
104+
105+
99106
@webapp.route('/login', methods=['GET', 'POST'])
100107
def login():
101108
if current_user.is_authenticated:
102-
return redirect(url_for('main'))
109+
return get_next_url(request.args.get('next'))
103110

104111
username = request.form.get('username')
105112
password = request.form.get('password')
113+
next_page = request.form.get('next')
106114
user = User.get_or_none(username=username)
107115

108116
if user is not None and user.is_password_valid(password):
109117
login_user(user)
110-
next_url = request.args.get('next_url')
111-
if not is_safe_url(next_url):
112-
return fail(400, "The URL isn't safe.")
113-
return redirect(next_url or url_for('main'))
118+
return get_next_url(next_page)
119+
elif user is not None:
120+
return redirect(url_for('login', **{'next': next_page}))
114121

115122
return render_template('login.html')
116123

lms/templates/login.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ <h1 id="main-title" class="h3 font-weight-normal">{{ _('התחברות') }}</h1>
2020
<div class="form-group">
2121
<input class="form-control form-control-lg" type="hidden" name="csrf_token" id="csrf_token" value="{{ csrf_token() }}" required>
2222
</div>
23+
<div class="form-group">
24+
<input class="form-control form-control-lg" type="hidden" name="next" id="next" value="{{ request.args.get('next', '') }}">
25+
</div>
2326
<button class="btn btn-primary btn-lg btn-block">{{ _('התחבר') }}</button>
2427
</form>
2528
</div>

0 commit comments

Comments
 (0)