Skip to main content

Setting the User type

In order to use your own type for req.session.user (default is unknown) you can make use of declaration merging.

1. Create the typings folder

Create a folder named typings inside your src folder (or an equivalent folder).

2. Extend Gatekeeper's User Interface

Inside the typings folder create another folder called gatekeeper-authentication. Inside it create a file named index.d.ts. Inside this file you will extend Gatekeeper's User type, like this:

interface CustomUser {
id: number;
username: string;
// ...
}

declare module 'gatekeeper-authentication' {
export interface User extends CustomUser {}
}

3. Include the typings folder inside your tsconfig.json

Finally, inside your tsconfig.json file, include the route to your typings folder inside the array of the property typeRoots, like this:

{
// ...
"typeRoots": ["./src/typings"]
// ...
}

Now, if you check the type of req.session.user, you'll see it is now the type you defined!