storing the facebook login session
<p>I am successfully authenticating the user via facebook and getting their name in def fbconnect()</p>

<pre><code>print "username" ,login_session['username']
session.name=login_session['username']
print "session.name",session.name
</code></pre>

<p>But in my html, when I try check to see whether the user is logged in or not </p>

<pre><code>{% if session.name != '' %}
&lt;a href="{{url_for('showLogin')}}"&gt;Click Here to Login&lt;/a&gt;
{% else %}
&lt;a href="{{url_for('fbdisconnect')}}"&gt;&gt;Welcome {{session.name}}, Logout&lt;/a&gt;
{% endif %}
</code></pre>

<p>It doesn't go to the Welcome name section, because session.name is '' What am I missing here? Thanks.</p>

<p>This is my python stuff</p>

<pre><code>  @app.route('/fbconnect', methods=['POST'])
def fbconnect():
    if request.args.get('state') != login_session['state']:
        print "state not equal to login_session"
        response = make_response(json.dumps('Invalid state parameter.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response
    access_token = request.data
    print "access token received %s " % access_token

    app_id = json.loads(open('fb_client_secrets.json', 'r').read())[
        'web']['app_id']
    app_secret = json.loads(
        open('fb_client_secrets.json', 'r').read())['web']['app_secret']
    url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&amp;client_id=%s&amp;client_secret=%s&amp;fb_exchange_token=%s' % (
        app_id, app_secret, access_token)
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]

    # Use token to get user info from API
    userinfo_url = "https://graph.facebook.com/v2.4/me"
    print "userinfo_url", userinfo_url
    # strip expire tag from access token
    token = result.split("&amp;")[0]
    print "token", token


    url = 'https://graph.facebook.com/v2.4/me?%s&amp;fields=name,id,email' % token
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]
    # print "url sent for API access:%s"% url
    # print "API JSON result: %s" % result
    data = json.loads(result)
    login_session['provider'] = 'facebook'
    login_session['username'] = data["name"]
    login_session['email'] = data["email"]
    login_session['facebook_id'] = data["id"]

    # The token must be stored in the login_session in order to properly logout, let's strip out the information before the equals sign in our token
    stored_token = token.split("=")[1]
    login_session['access_token'] = stored_token

    # Get user picture
    url = 'https://graph.facebook.com/v2.4/me/picture?%s&amp;redirect=0&amp;height=200&amp;width=200' % token
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]
    data = json.loads(result)

    login_session['picture'] = data["data"]["url"]

    # see if user exists
    user_id = getUserID(login_session['email'])
    if not user_id:
        user_id = createUser(login_session)
    login_session['user_id'] = user_id

    output = ''
    output += '&lt;h1&gt;Welcome, '
    output += login_session['username']

    output += '!&lt;/h1&gt;'
    output += '&lt;img src="'
    output += login_session['picture']
    output += ' " style = "width: 300px; height: 300px;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;"&gt; '

    flash("Now logged in as %s" % login_session['username'])

    print "username" ,login_session['username']
    session['name']=login_session['username']
    print "session['name']",session['name']

    return output
</code></pre>

<p>If I change the html to </p>

<pre><code>{% if login_session['username'] != '' %}
</code></pre>

<p>Then it says UndefinedError: 'login_session' is undefined</p>

<p>It is working now using <code>session['username']</code> </p>

<p>I had to move some code around from one page to another to get it to work though. </p>

<p>Thanks for your help. </p>

python flask facebook-login