How can I login again when I logout using Google Plus Auth - django

To login with Google Plus Auth, I use the example of Python Social Auth at https://github.com/omab/python-social-auth/tree/master/examples/django_example
And I have the scenario as follows:
Firstly, I login by google plus sign in button
<div id="signinButton">
<span class="g-signin" data-scope="{{ plus_scope }}"
data-clientid="{{ plus_id }}"
data-redirecturi="postmessage"
data-accesstype="offline"
data-cookiepolicy="single_host_origin"
data-callback="signInCallback">
</span>
</div>
Secondly, I click on the logout button to sign out
<a class="btn btn-primary" href="/logout/">
<i class="fa fa-sign-out"></i>
Logout
</a>
Finally, when I login again by Google Plus sign button, the Google popup can not display to enter the username and password. However, I can login again by the button of Django example
<a class="col-md-2 btn btn-default" name="{{ backend|backend_class }}" href="{% url "social:begin" backend=name %}">
<i class="fa fa-{{ name|icon_name }}"></i>
{{ backend|backend_name }}
</a>
But I don't like this method because this is not recommended from Google https://developers.google.com/+/web/signin/redirect-uri-flow
As I understand, Google popup cannot display because I logged in by Google. I just logout from my django example app. For that reason, the popup cannot open.
However, I think this is a common action of users. They can login/logout many times from our Django apps and they don't need to know about their Google status.
So, is there any approach to login again for my scenario?
Thank you so much
Notes: This is my screen shot

Related

Django URL Page error when link is clicked twice

I have a url defined as:
path("home/projects/",project_view,name = "viewProject")
This render correctly when i click on the 'Details' link on project_view.html
<li>
<a href="projects" data-toggle="tooltip" class="tip-bottom" title="All Projects">
<i class="fa fa-list" aria-hidden="true">Details</i></a>
</li>
But while i am on the same page,if i click the same link again i get an error:
home/projects/ [name='viewProject']
"Page not found...The current path, home/projects/projects, didn't match any of these"
I understand what the error means,but how can i redirect the page to "home/projects/" if the link is clicked twice?
Instead of writing the url patterens you should use the django url template tag like this:
<a href="{% url 'viewProject' %}" data-toggle="tooltip" class="tip-bottom" title="All Projects">
<i class="fa fa-list" aria-hidden="true">Details</i></a>

How to implement a google-login for any google APIs in a Django wep-app?

I'm currently developing a Django web app that will allow users to sign-in with their google account and get access to their Google Calendar and Google Drive data.
I already succeeded in doing so on another project in a front-end context using an Angular library called ng-gapi (GoogleAuthService) but I can't seem to find an equivalent for Django! All I was able to do for the moment is to implement a simple google login, but can't communicate with the different google APIs.
I have tried to use a module called django-allauth but it only logs-in the user via google but without providing any useful information (like authorization tokens etc...) needed to grant the web app to communicate with the user's google data.
{% load socialaccount %}
{% providers_media_js %}
{% load static %}
<html>
<body>
{% if user.is_authenticated %}
<p>Welcome {{ user.username }}</p>
<a class="btn btn-warning" href="http://localhost:8000/accounts/logout/">Log out</a>
<a class="btn btn-secondary" href="http://localhost:8000/show/">My Gigs</a>
<p>You're logged in with {{ user.get_provider }} as {{ user }}.</p>
<img style="max-width: 80px; padding:10px; margin-bottom:10px" src="{{ user.socialaccount_set.all.0.get_avatar_url }}" />
<p>UID: {{ user.socialaccount_set.all.0.uid }}</p>
<p>Date Joined: {{ user.socialaccount_set.all.0.date_joined}}</p>
<p>Last Login: {{ user.socialaccount_set.all.0.last_login}}</p>
<p>{{ user.socialaccount_set.all.0.extra_data.name }}</p>
{% else %}
<a class="btn btn-primary" href="{% provider_login_url 'google' %}">Log in</a>
{% endif %}
</body>
</html>
When I try to log-in this way, I get the google sign-in page which works well, but I don't have the second window which would have to be the one concerning the user granting my web-app to have access to his Google Calendar and Google Drive data (even though I have already configured it in my google developer console).
I would be immensely grateful if you could help me, I know that it looks as if this question has been answered multiple times but I have read hundreds of posts on the subject and couldn't find a solution.
You can use Django social auth library
An example of the implementation is here
You should enable access to google calendar and disk API from GOOGLE API CONSOLE

In Django, the button url is called unexpectedly, The url request is excuted differently

In Django, the button url is called unexpectedly, so I can not find the page
I tried click below button but it's not work
<button type="button"
class="btn btn-outline-info btn-sm float-right"
name="button"
onclick="location.href='{{fn_id}}/finisher/new'">
post
</button>
result of url request is
It seems that there is a problem because I made the url request when I was on the detail page.
and below buuton is work
<button
type="button"
class="btn btn-outline-info btn-sm float-right"
name="button" onclick="location.href='http://127.0.0.1:8000/bestlec/{{fn_id}}/finisher/new'"
>
post
</button>
but That causes problem Because it requests a local address
do you know how to slove it?? thanks for let me know~!
In your first example, I expect you're on page http://127.0.0.1:8000/bestlec/1. You've then given a relative url of 1/finisher/new which has been added to the current url to give http://127.0.0.1:8000/bestlec/1/1/finisher/new.
If you want to give a relative url use finisher/new. If you need to go to other ids use /bestlec/{{fn_id}}/finisher/new or better still, use {% url 'url-name' fn_id %}

laravel blade check whether authenticated user exists in db

I'm working on user request model. According to the below mentioned code , if the authenticated use id exists in database, he should see thumbs-up(font awesome), suppose if not he should see request button(font awesome). I'm using ajax and JQuery, Everything working fine but I'm not getting how to check it using if condition. someone help me Plz..
<div class="col-sm-1 follows" style="padding:0px">
#if($followCount->user_id != Auth::user()->id)
<h2 id="deligate" value="1">
<i class="fa fa-user-plus" data-toggle="tooltip" title="follow" ></i>
</h2>
#else(!empty($followCount->user_id))
<h2 id="unfollow">
<i class="fa fa-thumbs-up" data-toggle="tooltip" title="unfollow" style="display:none;color: #ff9933" id="test"></i>
</h2>
#endif
</div>
For this you have to use only following code in blade file
#if(Auth::user())
{{ code for authorise user. }}
</h2>
#else
{{ Code for unauthorised user.}}
#endif
This check user authenticated user or not you have not need to add extra code for this.
Only use Auth::user() this check and match with your users table in DB.
Thank You and hope this work for you.

Selenium & django testing

I'm trying to click on a link in navbar to trigger modal for third party authentication, which I need to test. I have some help before about how to fetch and click with selenium, and everything was good until this.
This is the link I'm trying to click
<li class="pull-right hidden-xs hidden-sm" id="showlogin"><button type="button" class="btn btn-sm btn-info round">GET STARTED</button></li>
my test structure is:
def test_live_societe_login_using_third_party_modal(self):
"""TODO: Docstring for test_live_societe_login_third_party_modal.
:returns: return modal for third party authentication
"""
WebDriverWait(self.browser, 10).until(lambda browser:
self.browser.find_element_by_id
('showlogin')).click()
self.assertIn('http://societe.herokuapp.com/contact', self.browser.current_url)
I was able to test all application pages, but hitting this one I got selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with exception. I'm a little confused about it, can someone help me over come this, thanks.
Ok, I was locking at this the wrong way, in my navbar I have constructed GET STARTED button like this:
<li class="pull-right hidden-xs hidden-sm" id="showlogin"><button type="button" class="btn btn-sm btn-info round">GET STARTED</button></li>
<li class="dropdown hidden-md hidden-lg" id="showlogin">
<button type="button" class="btn btn-sm btn-info round">GET STARTED</button>
<ul class="dropdown-menu">
<li class="btn-group-vertical"><a id="facebook_login" href="/accounts/facebook/login" method="js-sdk" class="btn btn-default navbar-btn"><i class="fa fa-facebook"></i> Sign in with Facebook</a>
<a id="linkedin_login" href="/accounts/linkedin/login" class="btn btn-default navbar-btn"><i class="fa fa-linkedin"></i> Sign in with LinkedIn</a>
<a id="twitter_login" href="/accounts/twitter/login" class="btn btn-default navbar-btn"><i class="fa fa-twitter"></i> Sign in with Twitter</a></li>
</ul>
</li>
You can see that I have two <li></li> tags, one is a button for larger viewport and the other tag have dropdown menu for smaller viewport. In the first place I was not selecting the right <li></li> tag. When selenium start testing he opened a tablet view browser (≥768px) and that changed the situation in the sense of I needed to select dropdown and then find a button to click and trigger third party authentication.
I can select dropdown with Css selector, so I just find_element_by_css_selector('div#navbar ul li.dropdown'), after this I selected right social button. All I need to do now is handle social authentication.
This is the code for selecting dropdown and social button for third party authentication:
def test_live_societe_login_using_third_party_modal(self):
"""TODO: Docstring for test_live_societe_login_third_party_modal.
:returns: return modal for third party authentication
"""
dropSelect = WebDriverWait(self.browser, 20).until(
lambda browser: (self.browser.find_element_by_css_selector('div#navbar ul li.dropdown')))
dropSelect.click()
twitter_choice = dropSelect.find_element_by_id('twitter_login')
twitter_choice.click()