First, the Auth
construct adds an /auth
route to the API.
const auth = new Auth(stack, 'auth', {
authenticator: {
handler: 'auth.handler',
},
});
const api = new Api(stack, 'api', {
routes: {
'GET /': 'index.handler',
}
});
auth.attach(stack, { api });
A session token is generated by visiting /auth/:adapter/authorize
, where :adapter
is one of the keys under
providers
when creating an AuthHandler
.
So given the following code:
export const handler = AuthHandler({
providers: {
foo: FooAdapter(),
bar: BarAdapter(),
}
})
the API will have routes for /auth/foo/authorize
and /auth/bar/authorize
. Note that there will also be /auth/foo|bar/callback
, but for this example, the callback route is not used.
When you hit an authorize
route and it's successful, you can create a token with Session.parameter({ ... })
or Session.cookie({ ... })
. This example uses cookies. By default, auth-token
is used for the cookie name.
It feels a little strange because you don't rely on a lambda authorizer
to protect your API from unauthorized access. Instead, you use useSession()
to
get the current session and determine if the user is authorized (might need to double check this claim... it might happen automagically without calling useSession
).