本文整理匯總了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);
});