Sto sviluppando un’applicazione di 1 pagina in AngularJS usando e Django Rest Framework + Django CORS Headers.
Il mio problema è che il cookie “csrftoken” non compare mai nel mio browser quando ho contattato il back-end.
Per esempio: sto facendo un login usando un post. Ottengo il cookie “sessionid” correttamente ma il “csrftoken” non compare mai e quindi non posso fare post appropriati dal mio client dato che mi verrà negato a causa della mancanza del token csrf.
Alcuni frammenti di codice dal fronte / backend. Questi sono frammenti non finiti, quindi non rimanere appeso su un codice scritto male.
class LoginView(APIView): renderer_classs = (JSONPRenderer, JSONRenderer) def post(self, request, format=None): serializer = LoginSerializer(data=request.DATA) if serializer.is_valid(): userAuth = authenticate(username=serializer.data['username'], password=serializer.data['password']) if userAuth: if userAuth.is_active: login(request, userAuth) loggedInUser = AuthUserProfile.objects.get(pk=1) serializer = UserProfileSerializer(loggedInUser) user = [serializer.data, {'isLogged': True}] else: user = {'isLogged': False} return Response(user, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
.controller('LoginCtrl', ['$scope', '$http', 'uService', '$rootScope', function(scope, $http, User, rootScope) { scope.login = function() { var config = { method: 'POST', withCredentials: true, url: rootScope.apiURL+'/user/login/', data : scope.loginForm }; $http(config) .success(function(data, status, headers, config) { if (status == 200) { console.log(data[0]); //Test code // succefull login User.isLogged = true; User.username = data.username; } else { console.log(data); //Test code User.isLogged = false; User.username = ''; } }) .error(function(data, status, headers, config) { console.log('Testing console error'); User.isLogged = false; User.username = ''; }); };
}]);
Qualcuno con buoni consigli / idee / esempi?