Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add remember headers to json response using json renderer

this is the current way I use to add remeber headers to response:

@view_config(route_name='login', renderer='json', request_method='POST')
def post_login(request):
   ...
   ... authentication logic
   ...
   headers = remeber(request, login)
   return HTTPFound(location=came_from, headers=headers)

but my js is waiting for the response {successful: True, message: 'auth OK'}. HTTPFound will redirect to came_from. I want js redirect

so I tried this

@view_config(route_name='login', renderer='json', request_method='POST')
def post_login(request):
   ...
   ... authentication logic
   ...
   return { 'successful': True, 'message': 'auth OK'}

but since the remeber headers are never added to the response it will be never authenticated on the other side of the moooon.

so my question is how to add those remeber headers using json renderer?

like image 363
llazzaro Avatar asked Dec 21 '22 09:12

llazzaro


1 Answers

You can set that information on the response directly, as documented in the Vary Attributes of Rendered Responses section of the Pyramid manual:

@view_config(route_name='login', renderer='json', request_method='POST')
def post_login(request):
   ...
   ... authentication logic
   ...
   headers = remeber(request, login)
   request.response.headerlist.extend(headers)
   return { 'successful': True, 'message': 'auth OK'}
like image 102
Martijn Pieters Avatar answered May 09 '23 12:05

Martijn Pieters