Updating type definitions for hapi-auth-basic >= 5.0.0.

This commit is contained in:
Rodrigo Saboya
2017-11-26 23:13:25 -02:00
parent c4df409881
commit e8e3c6a23b
2 changed files with 19 additions and 12 deletions
@@ -22,21 +22,22 @@ const users: {[index: string]: User} = {
}
};
const validate: Basic.ValidateFunc = function (request, username, password, callback) {
const validate: Basic.Validate = async (request, username, password, h) => {
const user = users[username];
if (!user) {
return callback(null, false);
return { isValid: false, credentials: null };
}
Bcrypt.compare(password, user.password, (err, isValid) => {
let isValid = await Bcrypt.compare(password, user.password)
callback(err, isValid, { id: user.id, name: user.name });
});
return { isValid, credentials: { id: user.id, name: user.name } };
};
server.register(Basic, (err) => {
server.register(Basic).then(() => {
server.auth.strategy('simple', 'basic', { validate });
server.auth.default('simple');
server.auth.strategy('simple', 'basic', { validateFunc: validate });
server.route({ method: 'GET', path: '/', config: { auth: 'simple' } });
});
+11 -5
View File
@@ -1,18 +1,24 @@
// Type definitions for hapi 4.2
// Type definitions for hapi-bauth-basic 5.0.0
// Project: https://github.com/hapijs/hapi-auth-basic
// Definitions by: AJP <https://github.com/AJamesPhillips>
// Rodrigo Saboya <https://github.com/saboya>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
import * as Hapi from 'hapi';
declare namespace Basic {
interface ValidateFuncCallback {
(err: Error | null, isValid: boolean, userCredentials?: any): void;
interface ValidateCustomResponse {
response: any,
}
interface ValidateFunc {
(request: Hapi.Request, username: string, password: string, callback: ValidateFuncCallback): void;
interface ValidateResponse {
isValid: boolean,
credentials?: any,
}
interface Validate {
(request: Hapi.Request, username: string, password: string, h: object): Promise<ValidateResponse | ValidateCustomResponse>;
}
}