本文整理汇总了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)
}
}
}
开发者ID:Magnetjs,项目名称:magnet-passport,代码行数:12,代码来源:koa.ts
示例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.');
}
开发者ID:DavidOnGitHub,项目名称:questionApp_backend,代码行数:75,代码来源:passport.ts
示例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);
});
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:30,代码来源:koa-passport-tests.ts
注:本文中的koa-passport.initialize函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论