Python Forum
Trying to use flask_login from front end with rauth
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to use flask_login from front end with rauth
#1
I have posted my question here: https://stackoverflow.com/questions/4727...6_47279862

I am using React as frontend and Python & Flask as my backend with Rauth as OAuth2 for Facebook login and storing details of every user in db and using flask_login as protection. Those information are Sports, Education, Phone number and other things.

What I am doing is after user gets logged in I am trying to fetch his details from react app. But I am getting error user not logged in while on browser if I open that API I can see the json.

@login_manager.user_loader
def load_user(id):
    return User.select().where(User.id == id).first()


@app.route('/')
def index():
    return redirect(WEB_URL)


@app.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(WEB_URL)


@app.route('/me')
@login_required  # After removing decorator still I am not getting
def me():
    return jsonify({'email': current_user.email}), 200



@app.route('/authorize/<provider>')
def oauth_authorize(provider):
    if not current_user.is_anonymous:
        return redirect(WEB_URL)
    oauth = OAuthSignIn.get_provider(provider)
    return oauth.authorize()


@app.route('/callback/<provider>')
def oauth_callback(provider):
    if not current_user.is_anonymous:
        return redirect(WEB_URL)
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()
    if social_id is None:
        return redirect(WEB_URL)
    user = User.select().where(User.social_id == social_id).first()
    if not user:
        user = User(social_id=social_id, nickname=username, email=email)
        user.save()
    login_user(user, True)
    return redirect(WEB_URL)
So from fronend I am calling /authorize/<provider> which then redirects to /callback/<provider> and logs user in.

Now When I am opening /me on browser it works fine returns required json or redirects me to homepage.

But when I am trying to fetch the api from frontend or Postman either I am getting user not found (if I remove the decorator login_required) or returning the whole homepage.

I am using Miguel's auth so you can check that, it's working fine.
Reply
#2
I am getting error from the code which is like this.
Quote:Traceback (most recent call last):
File "python", line 1, in <module>
NameError: name 'login_manager' is not defined
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  can webflow be used as a front end? LandonJPGinn 3 1,322 Jul-26-2023, 12:18 PM
Last Post: Gaurav_Kumar
  flask, I want to send a json from the front end and update it to the postgresql DB wi passion044 1 4,015 Apr-12-2018, 08:40 AM
Last Post: thomasp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020