'Grabbing error mesages from Authlib and passing to flash in Flask
How can I pass authlib.integrations.base_client.errors.OAuthError error messages back to the html in flask via the flash() method?
I have these methods in my class
class NPIListApi:
def __init__(self, token=None):
self.token = token
def get_user_token(self, un, pw):
"""generates user token so we can create a client"""
client = OAuth2Session(c.CLIENT_ID, c.CLIENT_SECRET)
return client.fetch_token(
c.AUTH_URL,
authorization_response=c.AUTH_URL,
username=un,
password=pw,
grant_type="password",
)
def return_client(self, token):
"""creates the client required to establish connection"""
client = OAuth2Session(
c.CLIENT_ID,
c.CLIENT_SECRET,
token=token,
)
return client
def establish_connection(self):
"""Method to establish connection to the LIFE API"""
return self.return_client(self.token)
And this is my login route
@app.route("/", methods=["GET", "POST"])
def login():
"""login route where we take the users un & pw to create and store the auth token in our session"""
form = LoginForm()
if form.validate_on_submit():
username = form.username.data
password = form.password.data
n = NPIListApi()
# storing the token in the session not to expose un & pw
session["token"] = n.get_user_token(username, password)
print(session.get("token", None))
if n.get_user_token(username, password):
log.info(f"user: {username} logged in succesfully")
return redirect(url_for("home"))
return render_template("login.html", form=form)
I want to be able to flash() the error message to the user so they know if their creds are wrong etc etc.
I have tried a lot of ways but because I am returning the token in establish_connection(self) I am not sure how to do it as.
Also not how to grab the actual error its not like it's json.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
