{# index.html #}
{% extends "base.html" %}

{% block body %}

    <a href="/">Home</a>
    
    {# Check for errors. #}
    {% if result.error %}
        <h2>Damn that error: {{ result.error.message }}</h2>
    {% endif %}
    
    {# Welcome the user. #}
    {% if result.user %}
        <h1>Hi {{ result.user.name }}</h1>
        <h2>Your id is: {{ result.user.id }}</h2>
        <h2>Your email is: {{ result.user.email }}</h2>
    {% endif %}
    
    {# If the user has credentials, we can access his/her protected resources. #}
    {% if result.user.credentials %}
        
        {# Let's get the user's 5 most recent statuses. #}
        {% if result.provider.name == 'fb' %}
            Your are logged in with Facebook.<br />
            
            {# Insert the user's ID to the URL and access the resource. #}
            {% set url = 'https://graph.facebook.com/{0}?fields=feed.limit(5)'.format(result.user.id) %}
            {% set response = result.provider.access(url) %}
            
            {# Parse the response. #}
            {% if response.status == 200 %}
                
                {% if response.data.error %}
                    Damn that error: {{ response.data.error }}!
                {% endif %}
                
                {% if response.data.feed.data %}
                    Your 5 most recent statuses:<br />
                    
                    {% for status in response.data.feed.data %}
                        <h3>{{ status.message or status.name or status.story }}</h3>
                        Posted on: {{ status.created_time }}
                    {% endfor %}
                {% endif %}
                
            {% endif %}{# response.status == 200 #}
            
        {% endif %}{# result.provider.name == 'fb' #}
        
        {# Do the same for Twitter. #}
        {% if result.provider.name == 'tw' %}
            Your are logged in with Twitter.<br />
            
            {% set url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=5' %}
            {% set response = result.provider.access(url) %}
            
            {% if response.status == 200 %}
                
                {% if response.data.errors %}
                    Damn that error: {{ response.data.errors }}!
                {% endif %}
                
                {% if response.data %}
                    Your 5 most recent tweets:<br />
                    
                    {% for tweet in response.data %}
                        <h3>{{ tweet.text }}</h3>
                        Posted on: {{ tweet.created_at }}
                    {% endfor %}
                {% endif %}
                
            {% endif %}
            
        {% endif %}{# result.provider.name == 'tw' #}
    {% endif %}{# result.user.credentials #}

{% endblock body %}
