2018-10-10 18:03:03 -04:00
---
id: 589a69f5f9fc0f352b528e71
title: Implementation of Social Authentication II
challengeType: 2
2020-08-16 04:49:14 +08:00
isHidden: false
forumTopicId: 301557
2018-10-10 18:03:03 -04:00
---
## Description
2020-08-16 04:49:14 +08:00
< section id = 'description' >
注意,本项目在< a href = 'https://glitch.com/#!/import/github/freeCodeCamp/boilerplate-socialauth/' > 这个 Glitch 项目< / a > 的基础上进行开发,你也可以从 < a href = 'https://github.com/freeCodeCamp/boilerplate-socialauth/' > GitHub< / a > 上克隆。
设置 GitHub 验证的最后一步是创建策略本身。为此,你需要在项目中< code > require< / code > 'passport-github',且实例化为 GithubStrategy: < code > const GitHubStrategy = require('passport-github').Strategy;< / code > 。
为了设置 GitHub 策略,我们需要在 < b > passport< / b > 中使用实例化的 < b > GithubStrategy< / b > ,它可以接收两个参数:一个对象(包括 < em > clientID< / em > , < em > clientSecret< / em > 和 < em > callbackURL< / em > ),以及一个回调函数。在这个回调函数中,我们要处理验证成功时,判断用户是否已经在数据库中存在的逻辑,还有如果数据库中不存在,把用户数据添加到数据库的代码。这种处理方式适用于绝大部分第三方验证策略,但有些策略会需要我们提供更多的信息,详情请参考相关策略的 README。例如, Google 的验证策略会要求你提供一个 < em > scope< / em > ,用于标示用户成功登录后,你需要从返回的对象中获取那些信息。以及,这也需要经过用户同意,你才可以获取到。当前我们使用的验证策略,你也可以从它 GitHub 的页面上了解它的用法,不过我们也会在这里进行详细讲解。
你的新策略应该这样去实现:
```js
passport.use(new GitHubStrategy({
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: /*INSERT CALLBACK URL ENTERED INTO GITHUB HERE*/
},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
//Database logic here with callback containing our user object
2018-10-10 18:03:03 -04:00
}
2020-08-16 04:49:14 +08:00
));
```
目前,你的验证部分不会成功。由于没有数据库的逻辑和回调函数,你的代码目前还会报错。但如果你试一试,就可以在右边的控制台里看到输出了你的 GitHub 的个人信息。
完成上述要求后,你就可以在左边提交你的页面链接。
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2020-08-16 04:49:14 +08:00
< section id = 'instructions' >
2018-10-10 18:03:03 -04:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2020-08-16 04:49:14 +08:00
- text: 应添加依赖。
2020-02-18 01:40:55 +09:00
testString: getUserInput => $.get(getUserInput('url')+ '/_api/package.json') .then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, 'passport-github', 'Your project should list "passport-github" as a dependency'); }, xhr => { throw new Error(xhr.statusText); })
2020-08-16 04:49:14 +08:00
- text: 应引入依赖
2020-02-18 01:40:55 +09:00
testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.match(data, /require.*("|')passport-github("|')/gi, 'You should have required passport-github'); }, xhr => { throw new Error(xhr.statusText); })
2020-08-16 04:49:14 +08:00
- text: 到目前为止, Github 策略应正确设置。
2020-02-18 01:40:55 +09:00
testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.match(data, /passport.use.*new GitHubStrategy/gi, 'Passport should use a new GitHubStrategy'); assert.match(data, /callbackURL:( |)("|').*("|')/gi, 'You should have a callbackURL'); assert.match(data, /process.env.GITHUB_CLIENT_SECRET/g, 'You should use process.env.GITHUB_CLIENT_SECRET'); assert.match(data, /process.env.GITHUB_CLIENT_ID/g, 'You should use process.env.GITHUB_CLIENT_ID'); }, xhr => { throw new Error(xhr.statusText); })
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< / section >
## Solution
< section id = 'solution' >
```js
2020-08-16 04:49:14 +08:00
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
2018-10-10 18:03:03 -04:00
```
2020-08-16 04:49:14 +08:00
2018-10-10 18:03:03 -04:00
< / section >