Merge pull request #2822 from SomaticIT/add-passport-local

Add definitions for passport-local module
This commit is contained in:
Masahiro Wakame
2014-09-24 22:39:37 +09:00
3 changed files with 87 additions and 0 deletions

View File

@@ -295,6 +295,7 @@ All definitions files include a header with the author and editors, so at some p
* [Optimist](https://github.com/substack/node-optimist) (by [Carlos Ballesteros Velasco](https://github.com/soywiz))
* [Passport](http://passportjs.org/) (by [Hiroki Horiuchi](https://github.com/horiuchi/))
* [passport-facebook](https://github.com/jaredhanson/passport-facebook) (by [James Roland Cabresos](https://github.com/staticfunction/))
* [passport-local](https://github.com/jaredhanson/passport-local) (by [Maxime LUCE](https://github.com/SomaticIT))
* [passport-strategy](https://github.com/jaredhanson/passport-strategy) (by [Lior Mualem](https://github.com/liorm))
* [pathwatcher](http://atom.github.io/node-pathwatcher/) (by [vvakame](https://github.com/vvakame))
* [Parallel.js](https://github.com/adambom/parallel.js) (by [Josh Baldwin](https://github.com/jbaldwin))

View File

@@ -0,0 +1,53 @@
/**
* Created by Maxime LUCE <https://github.com/SomaticIT>.
*/
import express = require("express");
import passport = require('passport');
import local = require('passport-local');
//#region Test Models
interface IUser {
username: string;
}
class User implements IUser {
public username: string;
public password: string;
static findOne(user: IUser, callback: (err: Error, user: User) => void): void {
callback(null, new User());
}
verifyPassword(password: string): boolean {
return true;
}
}
//#endregion
// Sample from https://github.com/jaredhanson/passport-local#configure-strategy
passport.use(new local.Strategy(function (username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false);
}
if (!user.verifyPassword(password)) {
return done(null, false);
}
return done(null, user);
});
}));
// Sample from https://github.com/jaredhanson/passport-local#authenticate-requests
var app = express();
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login' }),
function (req, res) {
res.redirect('/');
});

33
passport-local/passport-local.d.ts vendored Normal file
View File

@@ -0,0 +1,33 @@
// Type definitions for passport-local 1.0.0
// Project: https://github.com/jaredhanson/passport-local
// Definitions by: Maxime LUCE <https://github.com/SomaticIT>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../passport/passport.d.ts"/>
declare module 'passport-local' {
import passport = require('passport');
import express = require('express');
interface IStrategyOptions {
usernameField?: string;
passwordField?: string;
}
interface IVerifyOptions {
message: string;
}
interface VerifyFunction {
(username: string, password: string, done: (error: any, user?: any, options?: IVerifyOptions) => void): void;
}
class Strategy implements passport.Strategy {
constructor(options: IStrategyOptions, verify: VerifyFunction);
constructor(verify: VerifyFunction);
name: string;
authenticate: (req: express.Request, options?: Object) => void;
}
}