From c75ee379eb78e44d3c7a04d0ab4f10ed93625c1d Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Mon, 12 Feb 2018 13:14:32 -0500 Subject: [PATCH 01/11] Separate user definition to allow extending types --- types/passport/index.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/types/passport/index.d.ts b/types/passport/index.d.ts index 0e612810d4..2b520df48f 100644 --- a/types/passport/index.d.ts +++ b/types/passport/index.d.ts @@ -11,7 +11,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 +25,8 @@ declare global { isAuthenticated(): boolean; isUnauthenticated(): boolean; } + interface User { + } } } From 9d3100b8ea35790c81bc19d1ad544631feab6f56 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Mon, 12 Feb 2018 13:38:23 -0500 Subject: [PATCH 02/11] Update index.d.ts --- types/passport/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/passport/index.d.ts b/types/passport/index.d.ts index 2b520df48f..8adc4584d4 100644 --- a/types/passport/index.d.ts +++ b/types/passport/index.d.ts @@ -25,6 +25,7 @@ declare global { isAuthenticated(): boolean; isUnauthenticated(): boolean; } + // tslint:disable-next-line:no-empty-interface interface User { } } From 259c6434a43493e6ca28ae7ebba5529f191151b2 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Thu, 15 Feb 2018 09:55:22 -0500 Subject: [PATCH 03/11] Update passport-tests.ts --- types/passport/passport-tests.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/types/passport/passport-tests.ts b/types/passport/passport-tests.ts index 58c306964c..3e7f4e675b 100644 --- a/types/passport/passport-tests.ts +++ b/types/passport/passport-tests.ts @@ -137,3 +137,14 @@ passportInstance.use(new TestStrategy()); const authenticator = new passport.Authenticator(); authenticator.use(new TestStrategy()); + +declare namespace Express { + interface User { + username: string; + } +} + +app.use((req, res, next) => { + console.log(req.user.username); + next(); +}); From 3ef2b4b3b4735ad80add4ff1bf5c25bfeb55bfe6 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Thu, 15 Feb 2018 10:12:29 -0500 Subject: [PATCH 04/11] Update passport-tests.ts --- types/passport/passport-tests.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/types/passport/passport-tests.ts b/types/passport/passport-tests.ts index 3e7f4e675b..cfee5c914f 100644 --- a/types/passport/passport-tests.ts +++ b/types/passport/passport-tests.ts @@ -138,13 +138,15 @@ passportInstance.use(new TestStrategy()); const authenticator = new passport.Authenticator(); authenticator.use(new TestStrategy()); -declare namespace Express { - interface User { - username: string; - } +declare global { + namespace Express { + interface User { + username: string; + } + } } app.use((req, res, next) => { - console.log(req.user.username); + req.user.username = "hello user"; next(); }); From ba7deb74ed0dd4d047c87b11c1207f9550f0381c Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Thu, 15 Feb 2018 10:15:30 -0500 Subject: [PATCH 05/11] Update index.d.ts --- types/passport/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/passport/index.d.ts b/types/passport/index.d.ts index 8adc4584d4..10f5d8ae8f 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 From fb7985211e0e08efbc0f78f7708f58594a7bb211 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Thu, 15 Feb 2018 10:31:14 -0500 Subject: [PATCH 06/11] Update passport-tests.ts --- types/passport/passport-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/passport/passport-tests.ts b/types/passport/passport-tests.ts index cfee5c914f..6bc7bbe64e 100644 --- a/types/passport/passport-tests.ts +++ b/types/passport/passport-tests.ts @@ -146,7 +146,7 @@ declare global { } } -app.use((req, res, next) => { +app.use((req: express.Request, res: express.Response, next: (err?: any) => void) => { req.user.username = "hello user"; next(); }); From 3b91816142ed28e283218537e5a6ffd8828773cf Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Thu, 15 Feb 2018 13:29:47 -0500 Subject: [PATCH 07/11] Update passport-tests.ts --- types/passport/passport-tests.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/types/passport/passport-tests.ts b/types/passport/passport-tests.ts index 6bc7bbe64e..49b2436333 100644 --- a/types/passport/passport-tests.ts +++ b/types/passport/passport-tests.ts @@ -147,6 +147,8 @@ declare global { } app.use((req: express.Request, res: express.Response, next: (err?: any) => void) => { - req.user.username = "hello user"; + if (req.user) { + req.user.username = "hello user"; + } next(); }); From d0e71f615e10c5d21086e851340f2b2e43912cb1 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Fri, 16 Feb 2018 11:07:54 -0500 Subject: [PATCH 08/11] Update passport-tests.ts https://stackoverflow.com/questions/40349987/how-to-suppress-typescript-error-ts2533-object-is-possibly-null-or-undefine --- types/passport/passport-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/passport/passport-tests.ts b/types/passport/passport-tests.ts index 49b2436333..9cd246e7f5 100644 --- a/types/passport/passport-tests.ts +++ b/types/passport/passport-tests.ts @@ -148,7 +148,7 @@ declare global { app.use((req: express.Request, res: express.Response, next: (err?: any) => void) => { if (req.user) { - req.user.username = "hello user"; + req.user!.username = "hello user"; } next(); }); From 1d61b414bbafcddec38b568ab8f8033bf6a408fa Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Fri, 16 Feb 2018 11:14:17 -0500 Subject: [PATCH 09/11] Update passport-tests.ts --- types/passport/passport-tests.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/types/passport/passport-tests.ts b/types/passport/passport-tests.ts index 9cd246e7f5..23058684a7 100644 --- a/types/passport/passport-tests.ts +++ b/types/passport/passport-tests.ts @@ -148,7 +148,9 @@ declare global { app.use((req: express.Request, res: express.Response, next: (err?: any) => void) => { if (req.user) { - req.user!.username = "hello user"; + if (req.user.username) { + req.user.username = "hello user"; + } } next(); }); From 18c0620984fa14fca80bc986618f24ecead8bb83 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Sat, 17 Feb 2018 21:40:04 -0500 Subject: [PATCH 10/11] Update index.d.ts --- types/passport/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/passport/index.d.ts b/types/passport/index.d.ts index 10f5d8ae8f..06c7f0b9db 100644 --- a/types/passport/index.d.ts +++ b/types/passport/index.d.ts @@ -26,8 +26,8 @@ declare global { isAuthenticated(): boolean; isUnauthenticated(): boolean; } - // tslint:disable-next-line:no-empty-interface interface User { + [_: string]: any; } } } From 32ce5226688be7ee04df405f431d410cb7a114cd Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Sat, 17 Feb 2018 21:46:13 -0500 Subject: [PATCH 11/11] Update passport-tests.ts --- types/passport/passport-tests.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/types/passport/passport-tests.ts b/types/passport/passport-tests.ts index 23058684a7..630091afc7 100644 --- a/types/passport/passport-tests.ts +++ b/types/passport/passport-tests.ts @@ -151,6 +151,9 @@ app.use((req: express.Request, res: express.Response, next: (err?: any) => void) if (req.user.username) { req.user.username = "hello user"; } + if (req.user.id) { + req.user.id = "123"; + } } next(); });