Home

Awesome

grant-vercel

Vercel Serverless Function handler for Grant

var grant = require('grant').vercel({
  config: {/*configuration - see below*/}, session: {secret: 'grant'}
})

module.exports = async (req, res) => {
  var {response} = await grant(req, res)
  if (response) {
    res.statusCode = 200
    res.setHeader('content-type', 'application/json')
    res.end(JSON.stringify(response))
  }
}

Also available for AWS, Azure, Google Cloud

ES Modules and TypeScript


Configuration

The config key expects your Grant configuration.

vercel.json

The following optional routing is used in all examples:

{
  "rewrites": [
    {"source": "/(.*)", "destination": "/api/grant"}
  ]
}

.vercel

You need a .vercel folder containing a project.json file in every example:

{"orgId":"...","projectId":"..."}

Routing

You login by navigating to:

https://[PROJECT].vercel.app/connect/google

The redirect URL of your OAuth app have to be set to:

https://[PROJECT].vercel.app/connect/google/callback

And locally:

http://localhost:3000/connect/google
http://localhost:3000/connect/google/callback

Session

The session key expects your session configuration:

OptionDescription
nameCookie name, defaults to grant
secretCookie secret, required
cookiecookie options, defaults to {path: '/', httpOnly: true, secure: false, maxAge: null}
storeExternal session store implementation

NOTE:

Example session store implementation using Firebase:

var request = require('request-compose').client

var path = process.env.FIREBASE_PATH
var auth = process.env.FIREBASE_AUTH

module.exports = {
  get: async (sid) => {
    var {body} = await request({
      method: 'GET', url: `${path}/${sid}.json`, qs: {auth},
    })
    return body
  },
  set: async (sid, json) => {
    await request({
      method: 'PATCH', url: `${path}/${sid}.json`, qs: {auth}, json,
    })
  },
  remove: async (sid) => {
    await request({
      method: 'DELETE', url: `${path}/${sid}.json`, qs: {auth},
    })
  },
}

Handler

The AWS Lambda handler for Grant accepts:

ArgumentTypeDescription
reqrequiredThe request object
resrequiredThe response object
stateoptionalDynamic State object {dynamic: {..Grant configuration..}}

The AWS Lambda handler for Grant returns:

ParameterAvailabilityDescription
sessionAlwaysThe session store instance, get, set and remove methods can be used to manage the Grant session
redirectOn redirect onlyHTTP redirect controlled by Grant, it is set to true when Grant is going to handle the redirect internally
responseBased on transportThe response data, available for transport-state and transport-session only

Examples

ExampleSessionCallback λ
transport-stateCookie Store
transport-querystringCookie Store
transport-sessionFirebase Session Store
dynamic-stateFirebase Session Store

Different session store types were used for example purposes only.

Configuration

All variables at the top of the Makefile with value set to ... have to be configured:

The transport-session and the dynamic-state examples requires your Firebase configuration to be set in vercel.json:

{
  "env": {
    "FIREBASE_PATH": "...",
    "FIREBASE_AUTH": "..."
  }
}
https://[project].firebaseio.com/[prefix]
{
  "rules": {
    ".read": "auth == '[key]'",
    ".write": "auth == '[key]'"
  }
}

All variables can be passed as arguments to make as well:

make plan example=transport-querystring ...

Develop

# build example locally
make build-dev
# run example locally
make run-dev

Deploy

# deploy Grant
make deploy