You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromflask_simpleloginimportis_logged_inifis_logged_in():
# do things if anyone is logged inifis_logged_in('admin'):
# do things only if admin is logged in
Protecting your views
fromflask_simpleloginimportlogin_required@app.route('/it_is_protected')@login_required# < --- simple decoratordeffoo():
return'secret'@app.route('/only_mary_can_access')@login_required(username='mary') # < --- accepts a list of namesdefbar():
return"Mary's secret"@app.route('/api', methods=['POST'])@login_required(basic=True) # < --- Basic HTTP Auth for APIdefapi():
# curl -XPOST localhost:5000/api -H "Authorization: Basic Y2h1Y2s6bm9ycmlz" -H "Content-Type: application/json"# Basic-Auth takes base64 encripted username:passwordreturnjsonify(data='You are logged in with basic auth')
classProtectedView(MethodView): # < --- Class Based Viewsdecorators= [login_required]
defget(self):
return"only loged in users can see this"