當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript koa-passport.initialize函數代碼示例

本文整理匯總了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;未經允許,請勿轉載。