-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix simplejwt integration, add tests and update example project (#78)
Fix simplejwt integration, add tests and update example project
- Loading branch information
Showing
12 changed files
with
369 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
{% extends "base.html" %} | ||
{% load staticfiles %} | ||
|
||
{% block content %} | ||
<h1>rest_framework_jwt is deprecated and will be dropped in next version! Use <a href="https://github.com/davesque/django-rest-framework-simplejwt">rest_framework_simplejwt</a> instead!</h1> | ||
<h3>Note! <a href="http://getblimp.github.io/django-rest-framework-jwt/">djangorestframework-jwt</a> must be installed for this method</h3> | ||
<div ng-app="JWTApp"> | ||
<div ng-controller="LoginJWTCtrl as ctrl"> | ||
<div> | ||
<h4>rest_framework_jwt.authentication.JSONWebTokenAuthentication</h4> | ||
<div><button ng-click="authenticate('facebook')">Login facebook (OAuth 2.0)</button></div> | ||
<div><button ng-click="authenticate('google')">Login google (OAuth 2.0)</button></div> | ||
<div><button ng-click="authenticate('twitter')">Login twitter (OAuth 1.0)*</button></div> | ||
<div><button ng-click="logout()">Logout jwt</button></div> | ||
</div> | ||
<div> | ||
(*) Currently in case of OAuth 1.0 you still need to have session enabled, as python-social-auth will store some data in session between requests. Maybe in future versions of rest-social-auth this will be solved. | ||
</div> | ||
<div> | ||
<h2>JWT user data</h2> | ||
<div> | ||
{% verbatim %} | ||
<img ng-src='{{ctrl.user.thumb}}' /> | ||
{% endverbatim %} | ||
</div> | ||
<div> | ||
<span>First name:</span> | ||
<span ng-bind="ctrl.user.first_name"></span> | ||
</div> | ||
<div> | ||
<span>Last name:</span> | ||
<span ng-bind="ctrl.user.last_name"></span> | ||
</div> | ||
<div> | ||
<span>Email:</span> | ||
<span ng-bind="ctrl.user.email"></span> | ||
</div> | ||
<h2>Raw JWT payload</h2> | ||
<div> | ||
<pre>{% verbatim %}{{ ctrl.jwtPayload | json }}{% endverbatim %}</pre> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
{% endblock content %} | ||
|
||
{% block scripts %} | ||
{{ block.super }} | ||
<script type="text/javascript"> | ||
|
||
function set_user(response){ | ||
var source; | ||
if (response){ | ||
source = response.data; | ||
} else { | ||
source = { | ||
'username': null, | ||
'first_name': null, | ||
'last_name': null, | ||
'email': null, | ||
'social_thumb': '{% static "anonymous.png" %}' | ||
}; | ||
} | ||
self.user.username = source.username; | ||
self.user.first_name = source.first_name; | ||
self.user.last_name = source.last_name; | ||
self.user.email = source.email; | ||
self.user.thumb = source.social_thumb; | ||
}; | ||
|
||
angular.module('JWTApp', ['satellizer']) | ||
.config(function($authProvider) { | ||
$authProvider.facebook({ | ||
url: "{% url 'login_social_jwt_user' provider='facebook' %}", | ||
clientId: '{{ facebook_key }}' | ||
}); | ||
$authProvider.google({ | ||
url: "{% url 'login_social_jwt_user' provider='google-oauth2' %}", | ||
clientId: '{{ googleoauth2_key }}', | ||
redirectUri: window.location.origin + '/' | ||
}); | ||
$authProvider.twitter({ | ||
url: "{% url 'login_social_jwt_user' provider='twitter' %}", | ||
}); | ||
$authProvider.authToken = 'JWT'; | ||
$authProvider.tokenPrefix = 'satellizer_jwt'; // to not collide with regular token auth | ||
}).controller('LoginJWTCtrl', function($scope, $auth, $http) { | ||
self = this; | ||
|
||
self.user = {}; | ||
set_user(); | ||
if ($auth.getToken()){ | ||
$http.get('{% url "current_user_jwt_old" %}').then(function(response){ | ||
set_user(response); | ||
}); | ||
} | ||
|
||
self.jwtPayload = $auth.getPayload(); | ||
|
||
$scope.authenticate = function(provider) { | ||
$auth.authenticate(provider).then(function(response){ | ||
$auth.setToken(response.data.token); | ||
set_user(response); | ||
self.jwtPayload = $auth.getPayload(); | ||
}).catch(function(data) { | ||
var err_msg = "Something went wrong, maybe you haven't installed 'djangorestframework-jwt'?"; | ||
console.log(data) | ||
console.log(err_msg); | ||
alert(err_msg); | ||
}); | ||
}; | ||
|
||
$scope.logout = function(){ | ||
$auth.removeToken(); | ||
set_user(); | ||
self.jwtPayload = $auth.getPayload(); | ||
}; | ||
}); | ||
|
||
</script> | ||
{% endblock scripts %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
pytest-django==3.4.4 | ||
pytest-django==3.4.8 | ||
httpretty==0.9.6 | ||
unittest2==1.0.1 | ||
mock==1.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.