本文整理汇总了TypeScript中koa-passport.initialize函数的典型用法代码示例。如果您正苦于以下问题:TypeScript initialize函数的具体用法?TypeScript initialize怎么用?TypeScript initialize使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了initialize函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: setup
async setup () {
this.app.koa.use(passport.initialize())
this.insert(passport)
for (const strategy of this.config.strategies) {
if (strategy && strategy.key) {
this.app.passport.use(strategy.key, strategy.strategy)
} else {
this.app.passport.use(strategy)
}
}
}
示例2: configPassport
function configPassport(app): void {
app.use(passport.initialize());
passport.serializeUser((user, done) => {
done(null, user.id);
});
const strategy = new LocalStrategy({usernameField: 'accountName'}, async function(accountName, password, done){
const accountCriteria = {
activationToken: null
};
try {
let username = null, email = null;
if (isEmail(accountName)) {
email = accountName;
Object.assign(accountCriteria, {
email
});
} else {
username = accountName
const profile = await Profile.findOne({
username
}).exec();
// log('profile ', profile);
if (!profile) {
return done(null, false, {
message: 'User not found with user name ' + accountName
});
} else {
Object.assign(accountCriteria, {
_id: profile.accountId
});
}
}
const account = await Account.findOne(accountCriteria).exec();
// log('account ', account);
if (!account) {
return done(null, false, {
message: 'User not found with email ' + accountName
});
} else {
account.comparePassword(password, async function(err, isMatch) {
if (err) {
return done(err);
}
if (!isMatch) {
return done(null, false, {
message: 'Username password misMatch'
});
}
if (!username) {
const profile = await Profile.findOne({
accountId: account.id
}).exec();
if (!profile) {
return done(null, false, {
message: 'Profile not found'
});
}
username = profile.username;
}
return done(null, Object.assign({}, account.toJson(), {username}));
});
}
} catch(error) {
log('passport error ', error)
return done(null, false, {
message: error.message
});
}
});
passport.use(strategy);
console.log('passport configured.');
}
示例3: Koa
import * as Koa from 'koa';
import * as passport from 'koa-passport';
const app = new Koa();
let ctx: Koa.Context;
app.use(passport.initialize());
app.use(passport.session());
app.use(async (ctx: Koa.Context): Promise<void> => {
ctx.isAuthenticated();
ctx.isUnauthenticated();
ctx.login({});
ctx.logout();
ctx.state.user;
});
app.use(async (ctx: Koa.Context, next: () => Promise<any>) => {
return passport.authenticate('local', (user: any, info: any, status: any) => {
if (user === false) {
ctx.status = 401;
ctx.body = { success: false };
} else {
ctx.body = { success: true };
return ctx.login(user);
}
})(ctx, next);
});