DEV Community

Discussion on: JWT Authorization for serverless APIs on AWS Lambda

Collapse
 
rahulahire profile image
Rahul Ahire • Edited

@tmaximini your article was great and to the point but I want to know how can I send and receive jwt token via cookies especially I'm interested in httpOnly.

I tried in following way but it didn't worked well
This is my code

Lambda Handler

const cookie = require('cookie');

module.exports.hello = async (event) => {
  return {
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Credentials': true,
      'Set-Cookie': cookie.serialize('name', 'lambda-cookie'),
    },
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'Go Serverless v1.0! Your function executed successfully!',
        input: event,
      },
      null,
      2
    ),
  };

};
Enter fullscreen mode Exit fullscreen mode

serverless.yml

service: cookie

frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221
functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: /getcookie
          method: get
          cors: true 
Enter fullscreen mode Exit fullscreen mode

Results

cookie header

but no cookie present