I apologize in advance for asking a rather cryptic question. However, I did not understand it despite going through a lot of material. It would be great if you could shed some light on this.
What is the purpose of a request_loader in flask-login? How does it interact with the user_loader decorator?
If I am using a token based authentication system (I am planning on sending the token to my angularJS front end, storing the token there and sending that token in the authorization-token header), will I need a request_loader or will a user_loader (where I check the auth header and see if the user exists) suffice?
From the Flask-Login documentation:
So, to answer your question, they both serve the same function for Flask-Login. They are both used to load the user.
request_loader
, however, is appropriate for custom logins.Here's a great tutorial I found that utilizes
request_loader
to take advantage of token based authentication (The post is not my own, I'm merely sharing the link): http://gouthamanbalaraman.com/blog/minimal-flask-login-example.htmlTo verify users with Flask-Login's session_id for frontend requests through Angular, you must set the
withCredentials
configuration flag totrue
.That is, if you are using Angular's
$http.post(url,data [,config])
or$http.get(url [,config])
, make sure theconfig
object contains the propertywithCredentials
set to true. This will instruct the browser to use its cookies in the same way it would for a full-on page visit.For example,
will
post
the data{username:'myusername',password:'mypassword'}
to your site/app's/api/login
route and, if you're using Flask-Login and are logged in, Flask will know.You can set this behavior for all
$http
service requests by settingsomewhere in your app. Currently, I have that line of code in my
app.config
block, which seems appropriate to me:(Since this post is about Flask, folks may want to send form data through Angular in such a way that it can be found in
request.form
, which has a similar solution, fyi.)