Merge pull request #23580 from danielpa9708/master

Separate user definition to allow extending types for passport package
This commit is contained in:
Daniel Rosenwasser
2018-02-18 22:20:39 -08:00
committed by GitHub
2 changed files with 25 additions and 1 deletions

View File

@@ -4,6 +4,7 @@
// Eric Naeseth <https://github.com/enaeseth>
// Igor Belagorudsky <https://github.com/theigor>
// Tomek Łaziuk <https://github.com/tlaziuk>
// Daniel Perez Alvarez <https://github.com/danielpa9708>
// 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;
}
}
}

View File

@@ -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();
});