diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index a08a0d47a0..71f35228b2 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -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)) diff --git a/passport-local/passport-local-tests.ts b/passport-local/passport-local-tests.ts new file mode 100644 index 0000000000..de799a4d82 --- /dev/null +++ b/passport-local/passport-local-tests.ts @@ -0,0 +1,53 @@ +/** + * Created by Maxime LUCE . + */ + +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('/'); + }); diff --git a/passport-local/passport-local.d.ts b/passport-local/passport-local.d.ts new file mode 100644 index 0000000000..6b3cbef38e --- /dev/null +++ b/passport-local/passport-local.d.ts @@ -0,0 +1,33 @@ +// Type definitions for passport-local 1.0.0 +// Project: https://github.com/jaredhanson/passport-local +// Definitions by: Maxime LUCE +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +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; + } +}