我真的不了解angulyar,django上有后端,使用https://github.com/st4lk/django-rest-social-auth,AngularJS上也有一个例子,但是如何在Angular 5上重新创建它,有一个模块ng2-ui - 但是我很难用它。
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('SessionApp', ['satellizer'])
.config(function($authProvider) {
$authProvider.linkedin({
url: "{% url 'login_social_session' provider='linkedin-oauth2' %}",
clientId: '86cpqssmi7ej5j',
redirectUri: window.location.origin + '/'
});
}).config(function($httpProvider) {
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
}).controller('LoginCtrl', function($scope, $auth, $http) {
self = this;
self.user = {};
set_user();
var req = {
method: "GET",
url: '{% url "current_user_session" %}',
skipAuthorization: true // in case of session auth don't send token header
}
$http(req).then(function(response){
console.log("Got user from session cookies");
set_user(response);
console.log(response);
});
$scope.authenticate = function(provider) {
$auth.authenticate(provider).then(set_user);
};
$scope.logout = function(){
var req = {
method: "POST",
url: '{% url "logout_session" %}',
skipAuthorization: true // in case of session auth don't send token header
}
$http(req).then(function(response){
set_user();
});
};
});