mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
* Fix issues identified with the Hapi 17 update This commit introduces a variety of changes, primarily fixes, but it also collapses the typings into a single file. Collapsing it into a single file means simpler augmentation of types, which is common in Hapi. Thank you to SimonSchick for contributing a number of changes here. * fix response property
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
// from https://github.com/hapijs/hapi-auth-basic#hapi-auth-basic
|
|
|
|
import Bcrypt = require('bcrypt');
|
|
import Basic = require('hapi-auth-basic');
|
|
import { Server } from 'hapi';
|
|
|
|
const server = new Server();
|
|
|
|
interface User {
|
|
username: string;
|
|
password: string;
|
|
name: string;
|
|
id: string;
|
|
}
|
|
|
|
const users: {[index: string]: User} = {
|
|
john: {
|
|
username: 'john',
|
|
password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret'
|
|
name: 'John Doe',
|
|
id: '2133d32a'
|
|
}
|
|
};
|
|
|
|
const validate: Basic.Validate = async (request, username, password, h) => {
|
|
|
|
const user = users[username];
|
|
if (!user) {
|
|
return { isValid: false, credentials: null };
|
|
}
|
|
|
|
let isValid = await Bcrypt.compare(password, user.password)
|
|
|
|
return { isValid, credentials: { id: user.id, name: user.name } };
|
|
};
|
|
|
|
server.register(Basic).then(() => {
|
|
|
|
server.auth.strategy('simple', 'basic', { validate });
|
|
server.auth.default('simple');
|
|
|
|
server.route({ method: 'GET', path: '/', options: { auth: 'simple' } });
|
|
});
|