Reputation: 143
In my app, I have a route called /main. I also have a route called /index which checks to see if a session token has expired. If it has, it calls /login. If not, it goes back to /main.
This is so that if someone refreshes /main then it will check their session status first.
What I'd like to do at the start of /main is call /index. Then, at the end of /index go back to /main and continue from where it left off rather than starting the route again.
So, something like:
@app.route('/main')
return redirect(url_for('index'))
#Do the /main stuff
@app.route('index')
#Do some stuff
return redirect(url_for('main')) #But don't go back to the start, go back to doing the /main stuff
Obviously, this will just get me into an endless loop. Could someone suggest the proper method to achieve this.
Upvotes: 2
Views: 3213
Reputation: 41
Not quite what you're requesting, but have you considered having your check done outside of a route and calling it when needed in your routes? Something like this is what I have set up --
class User(object):
def __init__(self, current_user=None, option=None):
self.current_user = current_user
self.option = option
def do_checks(self):
self.current_user = request.remote_user
option_val = request.cookies.get('option')
if option_val is None:
self.option = None
else:
self.option = option_val
@app.route('/route/', methods=['GET'])
def myroute():
user = User()
user.do_checks()
if user.option is None:
return redirect(url_for('login'))
## code..
Or maybe look into setting up decorators for your routes, similar to Flask-Login, to lock certain routes only to those who are already authorized:
@app.route('/post')
@login_required
def post():
pass
if not current_user.is_authenticated:
return current_app.login_manager.unauthorized()
Flask-Login — Flask-Login 0.4.1 documentation
Upvotes: 1