本文整理汇总了TypeScript中passport.deserializeUser函数的典型用法代码示例。如果您正苦于以下问题:TypeScript deserializeUser函数的具体用法?TypeScript deserializeUser怎么用?TypeScript deserializeUser使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了deserializeUser函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: initialize
public initialize(router: express.Router): void {
router.use(passport.initialize());
const useSession = _.get(this.options, 'authOptions.session', true);
if (useSession) {
router.use(passport.session());
if (this.options.serializeUser && this.options.deserializeUser) {
passport.serializeUser((user: any, done: (a: any, b: string) => void) => {
Promise.resolve(this.options.serializeUser(user))
.then((result: string) => {
done(null, result);
}).catch((err: Error) => {
done(err, null);
});
});
passport.deserializeUser((user: string, done: (a: any, b: any) => void) => {
Promise.resolve(this.options.deserializeUser(user))
.then((result: any) => {
done(null, result);
}).catch((err: Error) => {
done(err, null);
});
});
}
}
}
示例2: enableSessionSelialization
function enableSessionSelialization() {
passport.serializeUser( (user: any, callback: any) => {
callback(undefined, user);
});
passport.deserializeUser( (obj: any, callback: any) => {
callback(undefined, obj);
});
}
示例3: LocalStategy
container.resolve((authDirector: IAuthDirector) => {
passport.use(new LocalStategy(authDirector.verifyUser.bind(authDirector)));
passport.serializeUser((user: IUserModel, next) => {
authDirector.serializeUser(user, next);
});
passport.deserializeUser((username: string, next) => {
authDirector.deserializeUser(username, next);
});
});
示例4: load
@InitPhase
@Inject(['logger', 'config', 'express', 'passport-impl'])
@After(['ExpressSession:load', 'Passport:load'])
load(logger, config, app, PassportImpl) {
logger.debug('load Passport-local');
config.defaults({
passportLocal: {
loginPostRoute: '/auth/login',
tokenPostRoute: '/auth/token',
enableTokenLogin: true,
postLoginUrl: '/admin/',
postLogoutUrl: '/',
postErrorUrl: '/login',
// after this many tries, start locks
maxFailTries: 5,
// maximum amount of a time an account may be locked for
maxLockTime: 1 * 3600 * 1000
}
});
const settings = config.get('passportLocal');
passport.serializeUser(function serializeUser(user, done) {
const info = PassportImpl.serializeUserToSession(user);
done(null, info);
});
passport.deserializeUser(function deserializeUser(req, info, done) {
PassportImpl.deserializeUserFromSession(info).then((user) => {
done(null, user);
}).catch(done);
});
configureLocal(app, PassportImpl, settings);
if (settings.enableTokenLogin) {
configureToken(app, PassportImpl, settings);
}
app.get('/logout', function(req, res) {
res.clearCookie('remember_me');
req.logout();
req.session.destroy(function() {
res.okRedirect(settings.postLogoutUrl, {status: true});
});
});
}
示例5: initPassport
export function initPassport(appUrl: string) {
passport.serializeUser(async (user: any, done) => done(null, user._id))
passport.deserializeUser(async (_id, done) => done(null, await User.findOne({_id})))
// Get facebook authentication values from environment variables
const facebookClientId = process.env.MajavashakkiFbClientId
const facebookSecret = process.env.MajavashakkiFbSecret
const isFacebookAuthEnabled = facebookClientId && facebookSecret
if (isFacebookAuthEnabled) {
passport.use(new FbStrategy({
clientID: facebookClientId,
clientSecret: facebookSecret,
callbackURL: appUrl + "/authFacebook",
},
async (accessToken, refreshToken, profile, done) => {
console.log(`User '${profile.displayName}' logged in successfully.`)
try {
const user = await User.findOrCreate(profile.id)
done(null, user)
} catch (err) {
done(err)
}
},
))
} else {
console.warn("[WARNING] Facebook authentication was not enabled. Missing environment variables 'MajavashakkiFbClientId' or 'MajavashakkiFbSecret'")
}
passport.use(new LocalStrategy({ usernameField: "email" }, async (email, password, done) => {
try {
const user = await User.findByLoginEmail(email)
if (!user) {
console.log(`User '${email}' tried to log in with invalid email`)
return done(null, false, { message: "There is no account with this email. :O" });
}
const isValidPassword = await user.isCorrectPassword(password)
if (!isValidPassword) {
console.log(`User '${email}' tried to log in with invalid password`)
return done(null, false, { message: "Invalid password, did you try 'salasana1'?" });
}
console.log(`User '${email}' logged in successfully`)
return done(null, user);
} catch (error) {
return done(error)
}
}));
}
示例6: run
export default function run(app) {
passport.serializeUser(function(user, callback) {
callback(undefined, user);
});
passport.deserializeUser(function(user, callback) {
callback(undefined, user);
});
passport.use(new LocalStrategy(function(username, password, callback) {
userGateway.login(username, password, callback);
}));
}
示例7: function
exports.setup = function(User) {
// serialize sessions
passport.serializeUser((user, cb) => cb(null, user));
passport.deserializeUser((id, cb) => User.load({ criteria: { _id: id } }, cb));
// // used to serialize the user for the session
// passport.serializeUser(function(user, done) {
// done(null, user);
// "user.id"" saved to session req.session.passport.user = {id:'..'}
// });
//
// // used to deserialize the user
// passport.deserializeUser(function(id, done) {
// User.findById(id, function(err, user) {
// done(err, user);
// //"user"" object attaches to the request as req.user.
// });
// });
passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: config.facebook.callbackURL
}, function(accessToken, refreshToken, profile, done) {
User.findOne({ 'facebook.id': profile.id }, function(err, user) {
if (err)
return done(err);
if (!user) {
var data = {
name: profile.displayName,
email: profile.emails[0].value,
username: profile.username,
provider_id: profile.id,
provider: 'facebook',
facebook: profile._json
};
user = new User(data);
user.save(function(err) {
return done(err, user);
});
} else
return done(err, user);
});
}));
};
示例8: PassportInitialize
export function PassportInitialize() {
Passport.use(new Strategy(jwtConfig, (payload: JwtUserPayload, done: (a: null, b: JwtUser) => void) => {
const user: JwtUser = {
username: payload.sub,
roles: payload.auth.split(','),
};
done(null, user);
}));
Passport.serializeUser((user: JwtUser, done: (a: null, b: string) => void) => {
done(null, JSON.stringify(user));
});
Passport.deserializeUser((user: string, done: (a: null, b: JwtUser) => void) => {
done(null, JSON.parse(user));
});
}
示例9: function
export var passportFunction = function() {
var User = mongoose.model('User');
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findOne(
{_id: id},
'-password',
function(err, user) {
done(err, user);
}
);
});
require('./strategies/local.js')();
};
示例10: serializeDeserialize
private serializeDeserialize() {
passport.serializeUser((user, cb) => {
cb(null, user._id);
});
passport.deserializeUser((id, cb) => {
this.userDetailService.loadUserById(id).
then(
(user) => {
cb(null, user.getUserObject());
},
(err) => {
return cb(err);
}
);
});
}