A
A
Alexey Belov2018-04-01 01:15:11
Django
Alexey Belov, 2018-04-01 01:15:11

How to use Angular 5 with Django REST Social Auth?

I don’t know much about Angular myself, there is a backing on django, it uses https://github.com/st4lk/django-rest-social-auth, there is also an example on AngularJS, but how to recreate it on Angular 5, there is a ng2-ui module -auth, but I wouldn't be able to use it.

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();
        });
    };
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question