Make stratgey an exported class

This commit is contained in:
Mohsen Azimi
2018-04-02 17:47:15 -07:00
parent 8a857d6473
commit ce01a4e456
2 changed files with 16 additions and 12 deletions
+8 -8
View File
@@ -5,6 +5,7 @@
// Igor Belagorudsky <https://github.com/theigor>
// Tomek Łaziuk <https://github.com/tlaziuk>
// Daniel Perez Alvarez <https://github.com/danielpa9708>
// Mohsen Azimi <https://github.com/mohsen1>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
@@ -75,12 +76,15 @@ declare namespace passport {
Passport: PassportStatic["Authenticator"];
}
interface Strategy {
/**
* @param T Strategy option config
*/
export abstract class Strategy<T = object> {
name?: string;
authenticate(this: StrategyCreated<this>, req: express.Request, options?: any): any;
}
interface StrategyCreatedStatic {
/** This method must be implemented by the subclass */
abstract authenticate(req: express.Request, options?: T): any;
/**
* Authenticate `user`, with optional `info`.
*
@@ -124,10 +128,6 @@ declare namespace passport {
error(err: any): void;
}
type StrategyCreated<T, O = T & StrategyCreatedStatic> = {
[P in keyof O]: O[P];
};
interface Profile {
provider: string;
id: string;
+8 -4
View File
@@ -2,14 +2,18 @@ import * as passport from 'passport';
import express = require('express');
import 'express-session';
class TestStrategy implements passport.Strategy {
class TestStrategy extends passport.Strategy {
name = 'test';
constructor() { }
authenticate(this: passport.StrategyCreated<this>, req: express.Request) {
authenticate(req: express.Request) {
const user: TestUser = {
id: 0,
};
this.success(user);
if (Math.random() > 0.5) {
this.fail();
} else {
this.success(user);
}
}
}