Django custom Authentication Backend

24 views
Skip to first unread message

ahmadyosr

unread,
Apr 13, 2017, 4:27:02 PM4/13/17
to Django users
Hello everyone 
I've been trying to create a custom Authentication Backend in Django , It worked .. And it returns an authenticated user and It Logs-in successfuly, 
BUT , whenever I refresh the page or move for another page it just logout ! it doesn't stuck logged in . 
here's the backend snap : 

from django.contrib.auth.models import User
class SBAT(ModelBackend):
def authenticate(self, username=None, id=None):
try :
user = User.objects.get(username= username)
if user.id  == id :
return user
except :
return None 
def get_user(self ,user_id ):
try :
user = User.objects.get(pk=user_id)
except : 
return None 


the login view 
....       
        profile_name = profile_data['name']
profile_id = profile_data['id']

try :   
user = User.objects.get(username=profile_name+profile_id[:3])
except : 
user = User.objects.create_user(username=profile_name+profile_id[:3], id = int(profile_id))
        user = authenticate(username=user.username , id = int(profile_id) )
login(request,user)
        return render(request,'index.html')

It Login successfully but logout whenever I get out of the current page . 
Thanks in advance

Reply all
Reply to author
Forward
0 new messages