3

All is in my title. I would like to get the current user information because i need it in my view to display action (delete/edit) depending of the user's rights.

I use the basic authentification make by Django (/login /logout), i didn't change anything. Is there a way to listen to this login/logout action and retrieve this information in the Angular context?

Pretty new in angular, i'm searching some informations that would help me to go in one or other direction.

Actually i don't have a REST API for authenticated my users.

I find some interesting article Best place/way to store 'global' data such as 'current logged in user' / How do I store a current user context in Angular? but for now as i said i don't have AuthService just the basic login/logout from Django.

I used Django Rest framework for some action on my project (like post/delete/read/update for some of my models) and it works fine with angularjs. But i'm happy with the basic authentification provided by Django, so i don't want to rewrite it. But i don't know how to listen on it and if it's possible.

I know that is a broad question and for now i dont't have code to show because afters hours of research i don't know where i need to begin. If it's to broad i will remove it.

Thanks,

Community
  • 1
  • 1
Epok
  • 661
  • 1
  • 8
  • 16

1 Answers1

3

OK, you can do something like that

Example (someUrl is url to your function in view.py): In angular controller add $http

$http({method: 'POST', url: '/someUrl'}).
  success(function(data){
      //process aswer
  });

In djnago view.py:

from django.shortcuts import HttpResponse
import json

def check_login(request):
    if request.user.is_authenticated():
        return HttpResponse(json.dumps({'result': {'logged': True}, 'user':      request.user.username}),
                        content_type="application/json")
    else:
        return HttpResponse(json.dumps({'result': {'logged': False}}),
                        content_type="application/json")
Michał
  • 286
  • 1
  • 3
  • 9
  • thxs for the trick, i make my call through my API /api/user/current. For now, i know this is not the better solution because i have to call this endpoint in nearly each controller. But i'll do better next time when i have a deeper understanding of Django and Angularjs – Epok Jan 24 '14 at 00:12