write custom decorators in django to authenticate the user

51 views
Skip to first unread message

rohit.a...@gmail.com

unread,
Nov 2, 2017, 12:23:33 AM11/2/17
to Django users
def authenticated(f):
def wrap(request, *args, **kwargs):
token = request.META.get('HTTP_AUTHORIZATION')
entry = Person.objects.get(pk=kwargs['entry_id'])
payload = jwt.decode(token, SECRET)
user = Person.objects.get(
username=payload.get('username'),
pk=payload.get('id'))

wrap.__doc__=f.__doc__
wrap.__name__=f.__name__
return wrap

But the request object that I get is class object of the view and not the regular request object

e.g if I do print (request) output is

<login.views.Test object at 0x7ff106d82a50>

where am I going wrong.

rohit.a...@gmail.com

unread,
Nov 2, 2017, 1:32:32 AM11/2/17
to Django users
I am using jwt tokens 

yves.m...@liqd.de

unread,
Nov 2, 2017, 7:18:18 AM11/2/17
to Django users
Can you post an example of how you are using the decorator? If you are using it on a dispatch/get/post ... method of a class based views, it will still get the instance (self) as a first parameter. So you should take also self as an argument to your inner wrap method.

Unrelated: Your wrap method never calls f, that is probably also something you would like to change.
Reply all
Reply to author
Forward
0 new messages