我正在尝试在我的应用中实现一个登录功能,我正在使用护照进行身份验证。但我在打字稿编译上遇到错误:
TSError: ⨯ Unable to compile TypeScript
server/server.ts (132,26): Argument of type 'PassportStatic' is not
assignable to parameter of type 'Passport'.
Types of property 'use' are incompatible.
Type '{ (strategy: Strategy): PassportStatic; (name: string, strategy: Strategy): PassportStatic; }' is not assignable to type '{ (strategy: Strategy): this; (name: string, strategy: Strategy): this; }'.
Type 'PassportStatic' is not assignable to type 'this'. (2345)
at getOutput (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:307:15)
at /media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:336:16
at Object.compile (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:496:11)
at Module.m._compile (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:392:43)
at Module._extensions..js (module.js:580:10)
at Object.require.extensions.(anonymous function) [as .ts] (/media/rohan/1084CB1284CAF96C/code/hall3/hall3-server/node_modules/ts-node/src/index.ts:395:12)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
这是我的server.ts代码:
import * as bodyParser from 'body-parser';
import * as connectMongo from 'connect-mongo';
import * as express from 'express';
import * as session from 'express-session';
import * as cookieParser from 'cookie-parser';
import * as httpLogger from 'morgan';
import * as mongoose from 'mongoose';
import * as passport from 'passport';
import * as path from 'path';
// Config
import { Config } from './config/local';
import { PassportConfig } from './config/passport';
import { logger } from './config/logger';
/**
* The server.
*
* @class Server
*/
export class Server {
// The express app instance
public app: express.Application;
/**
* Bootstrap the application
*
* @class Server
* @method bootstrap
* @static
* @return {ng.auto.IInjectorService} Returns the newly created injector for this app
*/
public static bootstrap = (): Server => {
return new Server();
};
/**
* Constructor.
*
* @class Server
* @constructor
*/
constructor() {
// create expressjs application
this.app = express();
// configure application
this.config();
this.routes();
}
/**
* Configure application
*
* @class Server
* @method config
*/
public config = (): void => {
this.app.use(httpLogger('dev'));
// use json bodyparser
this.app.use(bodyParser.json());
// use query string parser
this.app.use(bodyParser.urlencoded({
extended: true
}));
// use CookieParser for setting and reading cookies
this.app.use(cookieParser(Config.session_secret));
/* some code skipped */
// Set up passport
**PassportConfig.setup(passport, this.userModel);**
this.app.use(passport.initialize());
this.app.use(passport.session()); // persistent login sessions
}
/**
* Create router
*
* @class Server
* @method routes
*/
public routes = () => {
// Backend Admin API Routes
let prefix = '';
//this.app.use(`/accounts`, LoginRoute.create(this.model.localUser, passport));
this.app.use('/data', DataRoute.create());
this.app.use('/files', express.static(path.join(__dirname, 'files')));
};
/**
* Shutdown
*
* @class Server
* @method shutdown
*/
public shutdown = () => {
logger.info('Shutting Down');
this.connection.close();
}
}
这是我的PassportConfig代码:
import { Model } from 'mongoose';
import { Passport } from 'passport';
import { Strategy, IStrategyOptions, VerifyFunction } from 'passport-local';
import { IUserModel } from '../models/user';
export class PassportConfig {
static setup = (passport: Passport, model: Model<IUserModel>) => {
// serialize by username as it is unique <Type of user, type of id>
passport.serializeUser<IUserModel, string>((user: IUserModel, done) => {
// Return unique identification of the user
done(null, user._id);
});
// deserialize by username <Type of user, typeof of id>
passport.deserializeUser<IUserModel, string>((id: string, done) => {
// findbyid and return user
model.findById(id, (err, user: IUserModel) => {
done(err, user);
});
});
// Specify strategy options
let options: IStrategyOptions = {
usernameField: 'username',
passwordField: 'password',
passReqToCallback: false
};
// verify function for signup
let verifySignUp: VerifyFunction = (username: string, password: string, done) => {
process.nextTick(() => {
model.findOne({
'username': username
}, (err, user: IUserModel) => {
if (err) {
return done(err);
}
if (user) {
return done(err, null);
} else {
let newUser = new model();
newUser.username = username;
newUser.password = newUser.generateHash(password);
// save the user
newUser.save((error) => {
return done(error, newUser);
});
}
});
});
};
let verifySignIn: VerifyFunction = (username: string, password: string, done) => {
process.nextTick(() => {
model.findOne({
'username': username
}, (err, user: IUserModel) => {
if (err)
return done(err);
if (!user) {
return done(null, null);
} else if (!user.validPassword(password)) {
return done(null, null);
}
return done(null, user);
});
});
};
passport.use('local-signup', new Strategy(options, verifySignUp));
passport.use('local-signin', new Strategy(options, verifySignIn));
}
}
我的Webstorm IDE在我的server.ts文件中passport
行的PassportConfig.setup(passport, this.userModel);
下面给出了一个红色下划线。
请帮忙。
答案 0 :(得分:0)
仅根据导入进行猜测,但看起来您要导入passport
中server.ts
库中的所有内容:
import * as passport from 'passport';
因此变量passport
的类型为PassportStatic
。
在PassportConfig
中,您要从库Passport
导入具体的导出类passport
:
import { Passport } from 'passport';
变量Passport
的类型为Passport
。
要解决此问题,您可以执行以下操作:
Passport
server.ts
的方式与PassportConfig
passport.Passport
中使用server.ts
(而不只是Passport
)