facebook login: How to redirect to a specific URL after login in - facebook-login

I have the following code in my html:
<div class="fb-login-button" scope="email,user_checkins,user_events,friends_checkins">Login with Facebook</div>
How do I specify a callback URL? I want to redirect to a URL that then stores the returned auth token and other details.
Thanks!

If you add onlogin="..." to your DIV you can specify a function to execute after login, for example:
<div class="fb-login-button" scope="email,user_checkins,user_events,friends_checkins" onlogin="alert( 'Logged In' );">Login with Facebook</div>

Related

Rendering Grafana inside django website using API Key

I’m attempting to pass an API token for a user to grafana from our own internal website so that the main gui renders in an iframe. We don’t want the users to have to log into grafana after they have logged into our site, so we are creating their users in grafana, building an API token and attaching that to the user for our website. When the user goes to the page that has the grafana iframe, we are sending an ajax get request with the token so grafana renders the main dashboards with the users information.
If we do just a standard iframe everything works fine and we render inside the frame. We can get to the login page and do everything we need to. When I add the token so we don’t need to authenticate nothing renders and I see no errors/logs on either grafana or the website. If I send an invalid token I see the expected “401 invalid Api key” error on both the website and the grafana logs. This is what I’m sending from the website…
<div class="content">
<div class="container-fluid" id="container">
</div>
</div>
<script>
$.ajax({
url: "{{url}}",
type: "GET",
beforeSend: function(xhr){
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Authorization', 'Bearer {{token}}');
},
success: function(r) {
$('#container').html(r);
}
});
</script>
With the above nothing happens, I get no errors or logs. If I keep everything else the same and just adjust the token to make it invalid, grafana says it is invalid, so I know it is making it to the server. Why is nothing coming back to be rendered?
Thanks!
That' s an Error in Grafana-API, the generated API-Key. Use a Key that is generated inside Grafana

How do I use url_for calling #app.route('/')?

I've a route like this:
#app.route('/')
#app.route('/upload')
def upload():
if someCondition():
return redirect(url_for(''))
return render_template('upload.html')
and a section of upload.html template:
<a class="link toolbar-nav-links" href="{{ url_for('') }}">Home</a>
When I attempt to launch the web page, I get BuildError: Could not build url for endpoint ''. Did you mean 'upload' instead?
When I change to url_for('upload'), I can successfully load the page and the links work, but when I click on Home, I get 192.168.1.1:5000/upload in the browser URL bar.
How do I implement url_for() so that when I click on the Home link, I would load 192.168.1.1:5000/ instead of 192.168.1.1:5000/upload?

python-social-auth custom redirect url in Django

I am using python-social-auth package along with Django to manage oauth and openID logins.
Now the issue is that, I can specify one redirect url in django settings.py, but in some case I want the user to be redirected to the same page from which he initiated the authentication process.
E.g: if my redirect url is : /home/ and I am currently in a page /products/product1 from which I authenticate the user, then I want the user to be redirected to /products/product1/ rather than /home/
Please help me solve this problem or at least some materials that covers this in detail.
Thanks in advance
I'm pulling your answer out of the question and placing it here for future reference.
Adding a parameter ?next={{request.get_full_path}} to the auth url.
A bit better is to conditianally add next when it exsists
<a href="{% url 'social:begin' "google-oauth2" %}
{% if request.GET.next %}?next={{request.GET.next}}{% endif %}">
<img src="{{ STATIC_URL }}images/login-google.png" alt="Google">
</a>

Showing 'cancel' on login page to return user to where they were (using django.contrib.auth)

We are using the #login_required decorator so that users see a login page if they try to access a url for which they need to be authenticated.
We want to show a 'cancel' button on the login page, which should return the user to whichever page they were on when they tried to access the url (by clicking a link etc - we don't need to deal with them manually entering the url).
At the moment our login.html looks for a request parameter 'login_cancel_url' and if present uses that (otherwise the home page).
However, this means we have to manually pass this parameter (set to the url of the current page) whenever we show a link or button that leads to an 'authentication required' url.
Is there a more elegant way to do this?
Thanks, Martin
Well you can try get the referrer header from the request but as far as I am aware, it's browser dependent and is not very reliable so the way you are doing it is probably best. You could try make life easier by creating template tags to avoid having to rewrite the return URL manually.
You are easily able to get the current URL from django's request object on any page, so instead of setting it manually on the link, you could write a snippet of html:
link_to_login.html
<!-- You should probably get /login/ using the {% url ... %} template tag -->
<a href="/login/?login_cancel_url={{ request.path|urlencode }}">
Login Page</a>
and use the {% include "link_to_login.html"%} template tag.
Alternatively, If the text needs to be different depending on the link you can instead create an inclusion template tag:
templatetags/extra_auth_tags.py
#register.inclusion_tag('templates/extra_auth_tags/login_link.html')
def login_link(context, text=None):
return {
'text':text
}
templates/extra_auth_tags/login_link.html
<!-- You should probably get /login/ using the {% url ... %} template tag -->
<a href="/login/?login_cancel_url={{ request.path|urlencode }}">
{% if text %}
{{ text }}
{% else %}
Some Default Text
{% endif %}
</a>
and then call it in your templates as {% login_link text="Check you messages" %}. Be aware that keyword arguments for inclusion tags are only supported in the django dev version so you might need to write the template tag by hand.

How to get the previous URL in Django

I'll give the step by step info.
Let's say we're in the about page, the URL is example.com/about.
There's an email a friend button, when clicked, the URL is example.com/emailafriend.
Then when I clicked the submit button, the referal URL will be submitted is example.com/emailafriend. Question, how to get the about page URL? BTW, I'm using request.META['HTTP_REFERER'].
You could work with the request path attribute and add that to your form's action attribute, like so:
<form method="post" action="/tell-a-friend?return_url={{ request.path }}">
...
</form>
Then, in your tell-a-friend view, HttpResponseRedirect to the return_url query parameter.
It looks like maybe you are using APPEND_SLASH which will do a redirect if you don't have a slash. Try changing the button link to sample.com/emailafriend/, note the ending slash.
Maybe simply pass sample.com/about as GET or POST param? Or even through session.