V
V
Vlad_beg2020-04-28 11:22:07
Flask
Vlad_beg, 2020-04-28 11:22:07

How to get the ID of the currently logged in user?

For authentication, I use flask_loginsuch a problem that you need to get the user id in the post method, for this, as I understand it, the method needs to be decorated with @login_requiredto get access to the current_user, but the decorator apparently does not work for post methods and gives an error.
In the get methods, I can access the data of the current user. Also in the post method, if I check if the user is authorized, then False.
What are the solutions?

class Authenticate(MethodView):

  def post(self):
    ...some code
    login_user(user, remember=True)
    g.user = current_user.id


class User(MethodView):

  def post(view):
    # not working
    print(current_user.id)
    print(g.user)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question