Files
freeCodeCamp/curriculum/challenges/chinese/06-quality-assurance/advanced-node-and-express/set-up-passport.chinese.md
Oliver Eyton-Williams 61460c8601 fix: insert blank line after ```
search and replace ```\n< with ```\n\n< to ensure there's an empty line
before closing tags
2020-08-16 04:45:20 +05:30

4.0 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
5895f70cf9fc0f352b528e65 Set up Passport 2 设置护照

Description

提醒一下,这个项目是基于Glitch的以下入门项目构建的,或者是从GitHub克隆的。是时候设置Passport这样我们终于可以开始允许用户注册或登录帐户了除了Passport我们还将使用Express-session来处理会话。使用此中间件将会话ID保存为客户端中的cookie并允许我们使用服务器上的该ID访问会话数据。这样我们将个人帐户信息保留在客户端使用的cookie之外以验证我们的服务器是否经过身份验证并保留密钥以访问存储在服务器上的数据。要设置Passport以便在项目中使用您需要先在package.json中将其作为依赖项添加。 "passport": "^0.3.2"此外现在还要将Express-session添加为依赖项。 Express-session拥有大量可以使用的高级功能但现在我们只是要使用基础知识 "express-session": "^1.15.0"您需要立即设置会话设置并初始化Passport。一定要先创建变量'session'和'passport',分别要求'express-session'和'passport'。要设置您要使用的快速应用程序使用会话我们将仅定义几个基本选项。请务必将“SESSION_SECRET”添加到.env文件中并为其提供随机值。这用于计算用于加密cookie的哈希值
 app.use会话{
  secretprocess.env.SESSION_SECRET
  resave是的
  saveUninitializedtrue
}; 
您也可以继续告诉您的快递应用程序使用 'passport.initialize'和'passport.session'。 (例如, app.use(passport.initialize()); )当您认为自己正确时,请提交您的页面。如果您遇到错误,可以在这里查看到目前为止完成的项目。

Instructions

Tests

tests:
  - text: Passort和Express-session是依赖项
    testString: getUserInput => $.get(getUserInput('url')+ '/_api/package.json') .then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, 'passport', 'Your project should list "passport" as a dependency'); assert.property(packJson.dependencies, 'express-session', 'Your project should list "express-session" as a dependency'); }, xhr => { throw new Error(xhr.statusText); })
  - text: 正确要求依赖性
    testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.match(data, /require.*("|')passport("|')/gi, 'You should have required passport'); assert.match(data, /require.*("|')express-session("|')/gi, 'You should have required express-session'); }, xhr => { throw new Error(xhr.statusText); })
  - text: Express应用程序使用新的依赖项
    testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.match(data, /passport.initialize/gi, 'Your express app should use "passport.initialize()"'); assert.match(data, /passport.session/gi, 'Your express app should use "passport.session()"'); }, xhr => { throw new Error(xhr.statusText); })
  - text: 正确设置会话和会话密钥
    testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.match(data, /secret:( |)process.env.SESSION_SECRET/gi, 'Your express app should have express-session set up with your secret as process.env.SESSION_SECRET'); }, xhr => { throw new Error(xhr.statusText); })

Challenge Seed

Solution

// solution required

/section>