'Unknown authentication strategy' 在服务器中使用 TS 时出错

'Unknown authentication strategy' error using TS in server

config/passport.ts

/// <reference path='../typings/index.d.ts' />

import * as passport from 'passport';
import { User } from '../models/user';
import * as config from './main';
import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt';
import { Strategy as LocalStrategy } from 'passport-local';

const localOptions = { usernameField: 'email' };

// Setting up local login strategy
const localLogin = new LocalStrategy(localOptions, function(email, password, done) {

  User.findOne({ email: email }, function(err, user) {
    if(err) { return done(err); }
    if(!user) { return done(null, false, { message: 'Your login details could not be verified. Please try again.' }); }

    //test instead of user.comparePassword
    user.schema.methods.comparePassword(password, function(err, isMatch) {
      if (err) { return done(err); }
      if (!isMatch) { return done(null, false, { message: "Your login details could not be verified. Please try again." }); }

      return done(null, user);
    });
  });
});

const jwtOptions = {
    // Telling Passport to check authorization headers for JWT
    jwtFromRequest: ExtractJwt.fromAuthHeader(),
    // Telling Passport where to find the secret
    secretOrKey: config.secret
};

// Setting up JWT login strategy
const jwtLogin = new JwtStrategy(jwtOptions, function(payload, done) {
  User.findById(payload._id, function(err, user) {
    if (err) { return done(err, false); }

    if (user) {
      done(null, user);
    } else {
      done(null, false);
    }
  });
});

passport.use(jwtLogin);
passport.use(localLogin);

router.ts

import * as AuthenticationController from './controllers/authentication';
import * as express from 'express';
import * as passport from 'passport';
import * as passportService from './config/passport';


// Middleware to require login/auth
const requireAuth = passport.authenticate('jwt', { session: false });
const requireLogin = passport.authenticate('local', { session: false });

// Constants for role types
const REQUIRE_ADMIN = "Admin";
const REQUIRE_OWNER = "Owner";
const REQUIRE_CLIENT = "Client";
const REQUIRE_MEMBER = "Member";

export function router(app) {
  // Initializing route groups
  const apiRoutes = express.Router();
  const authRoutes = express.Router();

        // Registration route
          authRoutes.post('/register', AuthenticationController.register);

        // Login route
          authRoutes.post('/login', requireLogin, AuthenticationController.login);

    // Set auth routes as subgroup/middleware to apiRoutes
      apiRoutes.use('/auth', authRoutes);

// Set url for API group routes
  app.use('/api', apiRoutes);
};

index.ts

/// <reference path="./typings/index.d.ts" />

// Importing Node modules and initializing Express
import * as express from 'express';
import * as logger from 'morgan';
import * as config from './config/main';
import * as bodyParser from 'body-parser';
import * as mongoose from 'mongoose';
import { router } from './router';

export const app = express();

const server = app.listen(config.port);
console.log(`Your server is running on port ${config.port}.`);

app.use(logger('dev'));

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Credentials");
    res.header("Access-Control-Allow-Credentials", "true");
    next();
});

//DB connection
mongoose.connect(config.database);

//Body parsing
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

router(app);

错误:

POST /api/auth/login 500 4.796 ms - 1947
Error: Unknown authentication strategy "local"
    at attempt (/Users/pumz/closeloop/app/server/node_modules/passport/lib/middleware/authenticate.js:173:37)
    at authenticate (/Users/pumz/closeloop/app/server/node_modules/passport/lib/middleware/authenticate.js:349:7)
    at Layer.handle [as handle_request] (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/layer.js:95:5)
    at /Users/pumz/closeloop/app/server/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/index.js:271:10)
    at Function.handle (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/index.js:176:3)
    at router (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/index.js:46:12)
    at Layer.handle [as handle_request] (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/index.js:312:13)
    at /Users/pumz/closeloop/app/server/node_modules/express/lib/router/index.js:280:7
    at Function.process_params (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/index.js:330:12)
    at next (/Users/pumz/closeloop/app/server/node_modules/express/lib/router/index.js:271:10)

我相信一切都已配置好,我没有遇到任何编译错误,并且寄存器按预期工作。

我看到一些 require('passport-local')(passport) 解决了它,但它不起作用。也不将身份验证方法更改为 local-login

import * as passportService from './config/passport';

TypeScript 将省略该导入,因为您没有使用任何 config/passport 正在导出(实际上您根本没有从该模块导出任何内容)的值位置。参见 https://github.com/Microsoft/TypeScript/issues/2812 和类似内容。

如果您查看 router.ts 的转译输出,您会发现它确实缺少 require('./config/passport')。解决方法是从该模块导出一些东西然后引用它——也许在导出的 init() 函数中初始化护照身份验证策略。

我知道我迟到了,但无论如何我都会提供我的建议,以防其他需要它的人找到他们的路。

根据www.typescriptlang.org

Though not recommended practice, some modules set up some global state that can be used by other modules. These modules may not have any exports, or the consumer is not interested in any of their exports. To import these modules, use: import "./my-module.js";

在你的情况下应该这样做:

    import './config/passport';

将执行passport.ts的所有内容