From 39c44eb49cbc43f1e6dde2fbca8ed3cfe74f55ce Mon Sep 17 00:00:00 2001 From: Horiuchi_H Date: Tue, 17 Jun 2014 15:23:55 +0900 Subject: [PATCH 1/2] add Profile common interface --- passport-facebook/passport-facebook.d.ts | 31 +++++++++++++++--------- passport/passport.d.ts | 19 +++++++++++++++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/passport-facebook/passport-facebook.d.ts b/passport-facebook/passport-facebook.d.ts index 47c9b68bb..0cf0cfae0 100644 --- a/passport-facebook/passport-facebook.d.ts +++ b/passport-facebook/passport-facebook.d.ts @@ -10,18 +10,25 @@ declare module 'passport-facebook' { import passport = require('passport'); import express = require('express'); - interface Profile { - id:string; - provider:string; - displayName:string; - name:{familyName:string; givenName:string; middleName:string}; - profileUrl:string; + interface Profile extends passport.Profile { + gender: string; + profileUrl: string; } - class Strategy implements passport.Strategy{ - constructor(options:{clientID:string; clientSecret:string; callbackURL:string}, - verify:(accessToken:string, refreshToken:string, profile:Profile, done:(error:any, user?:any) => void) => void); - name: string; - authenticate:(req: express.Request, options?: Object) => void; + interface IStrategyOption { + clientID: string; + clientSecret: string; + callbackURL: string; + + scopeSeparator?: string; + enableProof?: boolean; + profileFields?: string[]; } -} \ No newline at end of file + + class Strategy implements passport.Strategy { + constructor(options: IStrategyOption, + verify: (accessToken: string, refreshToken: string, profile: Profile, done: (error: any, user?: any) => void) => void); + name: string; + authenticate: (req: express.Request, options?: Object) => void; + } +} diff --git a/passport/passport.d.ts b/passport/passport.d.ts index cd86228f6..3b46c7d8f 100644 --- a/passport/passport.d.ts +++ b/passport/passport.d.ts @@ -42,6 +42,24 @@ declare module 'passport' { name?: string; authenticate(req: express.Request, options?: Object): void; } + + interface Profile { + provider: string; + id: string; + displayName: string; + name? : { + familyName: string; + givenName: string; + middleName?: string; + }; + emails?: { + value: string; + type?: string; + }[]; + photos?: { + value: string; + }[]; + } } declare module Express { @@ -60,3 +78,4 @@ declare module Express { isUnauthenticated(): boolean; } } + From ae9f9a270720100f2b5ddfe7319a855450e22f0d Mon Sep 17 00:00:00 2001 From: Horiuchi_H Date: Wed, 18 Jun 2014 19:09:57 +0900 Subject: [PATCH 2/2] bugfix & rename test file --- .../{passport-test.ts => passport-tests.ts} | 29 ++++++++++++++++++- passport/passport.d.ts | 4 +-- 2 files changed, 30 insertions(+), 3 deletions(-) rename passport/{passport-test.ts => passport-tests.ts} (61%) diff --git a/passport/passport-test.ts b/passport/passport-tests.ts similarity index 61% rename from passport/passport-test.ts rename to passport/passport-tests.ts index e3ee9d4f2..7542e2e0e 100644 --- a/passport/passport-test.ts +++ b/passport/passport-tests.ts @@ -34,7 +34,7 @@ app.post('/login', }); app.post('/login', function(req, res, next) { - passport.authenticate('local', function(err, user, info) { + passport.authenticate('local', function(err: any, user: { username: string; }, info: { message: string; }) { if (err) { return next(err) } if (!user) { req.session.error = info.message; @@ -52,6 +52,33 @@ app.get('/logout', function(req, res) { res.redirect('/'); }); +function authSetting(): void { + var authOption = { + successRedirect: '/', + failureRedirect: '/login', + }; + var successCallback = (req: express.Request, res: express.Response) => { + res.redirect('/'); + }; + + app.get('/auth/facebook', + passport.authenticate('facebook')); + app.get('/auth/facebook/callback', + passport.authenticate('facebook', authOption), successCallback); + + app.get('/auth/twitter', + passport.authenticate('twitter')); + app.get('/auth/twitter/callback', + passport.authenticate('twitter', authOption)); + + app.get('/auth/google', + passport.authenticate('google', { scope: + [ 'https://www.googleapis.com/auth/userinfo.profile' ] })); + app.get('/auth/google/callback', + passport.authenticate('google', authOption), successCallback); + +} + function ensureAuthenticated(req: express.Request, res: express.Response, next: (err?: any) => void) { if (req.isAuthenticated()) { return next(); } if (req.isUnauthenticated()) { diff --git a/passport/passport.d.ts b/passport/passport.d.ts index 3b46c7d8f..fbb5f1164 100644 --- a/passport/passport.d.ts +++ b/passport/passport.d.ts @@ -15,8 +15,8 @@ declare module 'passport' { function initialize(options?: { userProperty: string; }): express.Handler; function session(options?: { pauseStream: boolean; }): express.Handler; - function authenticate(strategy: string, options: Object, callback?: express.Handler): express.Handler; - function authenticate(strategy: string, callback2: (err: any, user: any, info: any) => void): express.Handler; + function authenticate(strategy: string, callback?: Function): express.Handler; + function authenticate(strategy: string, options: Object, callback?: Function): express.Handler; function authorize(strategy: string, options: Object, callback?: express.Handler): express.Handler; function serializeUser(fn: (user: any, done: (err: any, id: any) => void) => void): void; function deserializeUser(fn: (id: any, done: (err: any, user: any) => void) => void): void;