-
Notifications
You must be signed in to change notification settings - Fork 123
/
home_jwt.html
124 lines (114 loc) · 5 KB
/
home_jwt.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
{% extends "base.html" %}
{% load staticfiles %}
{% block content %}
<h3>Note! <a href="https://github.com/davesque/django-rest-framework-simplejwt">django-rest-framework-simplejwt</a> must be installed for this method</h3>
<div ng-app="JWTApp">
<div ng-controller="LoginJWTCtrl as ctrl">
<div>
<h4>rest_framework_simplejwt.authentication.JWTAuthentication</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>
(**) In this example <a href="https://github.com/davesque/django-rest-framework-simplejwt#token-types">pair token</a> is used. With simplejwt you can use <a href="https://github.com/davesque/django-rest-framework-simplejwt#sliding-tokens">sliding token</a> as well.
</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_pair_user' provider='facebook' %}",
clientId: '{{ facebook_key }}'
});
$authProvider.google({
url: "{% url 'login_social_jwt_pair_user' provider='google-oauth2' %}",
clientId: '{{ googleoauth2_key }}',
redirectUri: window.location.origin + '/'
});
$authProvider.twitter({
url: "{% url 'login_social_jwt_pair_user' provider='twitter' %}",
});
$authProvider.authToken = 'JWT';
$authProvider.tokenPrefix = 'satellizer_simple_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" %}').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 'django-rest-framework-simplejwt'?";
console.log(data)
console.log(err_msg);
alert(err_msg);
});
};
$scope.logout = function(){
$auth.removeToken();
set_user();
self.jwtPayload = $auth.getPayload();
};
});
</script>
{% endblock scripts %}