Merge pull request #2362 from horiuchi/passport-profile

Add Profile interface in passport
This commit is contained in:
Masahiro Wakame
2014-07-10 12:54:47 +09:00
3 changed files with 66 additions and 13 deletions
+17 -10
View File
@@ -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);
interface IStrategyOption {
clientID: string;
clientSecret: string;
callbackURL: string;
scopeSeparator?: string;
enableProof?: boolean;
profileFields?: string[];
}
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;
authenticate: (req: express.Request, options?: Object) => void;
}
}
@@ -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()) {
+21 -2
View File
@@ -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;
@@ -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;
}
}