diff --git a/types/passport/index.d.ts b/types/passport/index.d.ts index 0e612810d4..06c7f0b9db 100644 --- a/types/passport/index.d.ts +++ b/types/passport/index.d.ts @@ -4,6 +4,7 @@ // Eric Naeseth // Igor Belagorudsky // Tomek Łaziuk +// Daniel Perez Alvarez // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -11,7 +12,7 @@ declare global { namespace Express { interface Request { authInfo?: any; - user?: any; + user?: User; // These declarations are merged into express's Request type login(user: any, done: (err: any) => void): void; @@ -25,6 +26,9 @@ declare global { isAuthenticated(): boolean; isUnauthenticated(): boolean; } + interface User { + [_: string]: any; + } } } diff --git a/types/passport/passport-tests.ts b/types/passport/passport-tests.ts index 58c306964c..630091afc7 100644 --- a/types/passport/passport-tests.ts +++ b/types/passport/passport-tests.ts @@ -137,3 +137,23 @@ passportInstance.use(new TestStrategy()); const authenticator = new passport.Authenticator(); authenticator.use(new TestStrategy()); + +declare global { + namespace Express { + interface User { + username: string; + } + } +} + +app.use((req: express.Request, res: express.Response, next: (err?: any) => void) => { + if (req.user) { + if (req.user.username) { + req.user.username = "hello user"; + } + if (req.user.id) { + req.user.id = "123"; + } + } + next(); +});