如何在Django REST Social Auth中使用Angular 5?

时间:2018-03-31 22:20:38

标签: django angular oauth-2.0

我真的不了解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();
            });
        };
    });

1 个答案:

答案 0 :(得分:0)

多年后,我学习了如何使用它,这是一个实现了这种授权的应用程序示例 https://github.com/Alenorze/lnrz