区块链节点发现 节点认证_节点和护照js facebook认证

区块链节点发现 节点认证

As a user, It’s very often we would have used Facebook accounts to login to a web application. The process would have been so simple. This blog will explain to you how to implement Facebook authentication with Node and Express JS. To implement this, we will be using a third-party library called Passport JS. Passport JS is authentication middleware for Node and Express JS. Passport JS can be used with any Express JS applications. Passport JS provides 500 + strategies.

作为用户,通常我们会使用Facebook帐户登录Web应用程序。 这个过程是如此简单。 该博客将向您介绍如何使用Node和Express JS实现Facebook身份验证。 为了实现这一点,我们将使用一个名为Passport JS的第三方库。 Passport JS是Node和Express JS的身份验证中间件。 Passport JS可以与任何Express JS应用程序一起使用。 Passport JS提供500多种策略。

  1. Initializing a Node JS Project

    初始化Node JS项目

Initially let’s create a new Node js Project. The below commands create a new folder and then initializes the node to our project.

首先,让我们创建一个新的Node js项目。 以下命令创建一个新文件夹,然后将节点初始化为我们的项目。

mkdir facebook_passport
cd facebook_passport/
npm init -y
touch index.js

Now install the required packages.

现在安装所需的软件包。

npm install express cookie-session passport 

After installing, copy the below code to your index.js file.

安装后,将以下代码复制到index.js文件中。

index.js

index.js

const express = require('express')
const app = express()app.get('/',(req,res)=>{
res.send('Hello world')
})app.listen(8000,()=>{
console.log('Serve is up and running at the port 8000')
})

Now start the server using node index.js. Then navigate to http://localhost:8000/. You should see Hello world getting displayed in the browser.

现在,使用node index.js启动服务器。 然后导航到http:// localhost:8000 / 。 您应该看到Hello world正在浏览器中显示。

2. Creating OAuth client ID

2.创建OAuth客户端ID

Before using the passport’s Facebook Authentication strategy, you should have registered your app or web application with Facebook. To do that, follow the steps below.

在使用护照的Facebook身份验证策略之前,您应该已经在Facebook中注册了您的应用程序或Web应用程序。 为此,请按照以下步骤操作。

a. Login to Facebook developers dashboard.

一种。 登录到Facebook开发人员仪表板。

b. Navigate to MyApps page.

b。 导航到MyApps页面。

https://developers.facebook.com/apps

https://developers.facebook.com/apps

Image for post
Creating a new App
创建一个新的应用程序

c. Hit the Create App button.

C。 点击创建应用程序按钮。

d. Now a Model dialog box will open. Choose the type of app you want to create. After choosing, give a name for your app and hit the Create App ID button.

d。 现在将打开“模型”对话框。 选择您要创建的应用程序类型。 选择后,为您的应用命名,然后点击“创建应用ID”按钮。

Image for post
Image for post

e. Now, a new app would have been created. Navigate to Settings -> Basics.

e。 现在,将创建一个新的应用程序。 导航至Settings -> Basics

Image for post
Facebook OAuth Client ID
Facebook OAuth客户端ID

f. At the top, you will see the App ID and App secret displayed. Make a note of it.

F。 在顶部,您将看到显示的应用程序ID和应用程序密钥。 记下它。

3. Configuring Facebook OAuth

3.配置Facebook OAuth

Now let’s start integrating Facebook Authentication with our project. To do that I am creating a new file named passport.js which holds the credentials that we created from the Facebook developer’s Dashboard.

现在,让我们开始将Facebook身份验证与我们的项目集成在一起。 为此,我要创建一个名为passport.js的新文件,其中包含我们从Facebook开发人员的仪表板创建的凭据。

passport.js

passport.js

const passport = require('passport');
const FacebookStrategy = require('passport-facebook').Strategy;passport.serializeUser(function(user, done) {
done(null, user);
});passport.deserializeUser(function(user, done) {
done(null, user);
});passport.use(new FacebookStrategy({
clientID: "611015742909469",
clientSecret: "757**********************bed",
callbackURL: "http://localhost:8000/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
return done(null, profile);
}

));

Copy and paste the below code to your index.js file.

将以下代码复制并粘贴到index.js文件中。

The route /auth/facebook redirects the client to Facebook Login Page.

路由/auth/facebook将客户端重定向到Facebook登录页面。

The route /auth/facebook/callback will act as a callback URL which will be called if Facebook Authentication is Successful.

/auth/facebook/callback路由将充当回调URL,如果Facebook身份验证成功,则将调用该URL。

The route /ath/error will be called if any error has occurred during Facebook Authentication.

如果在Facebook身份验证期间发生任何错误,则将调用/ath/error路由。

index.js

index.js

const express = require('express')
const app = express()
const cookieSession = require('cookie-session')
const passport = require('passport');
require('./passport')app.use(cookieSession({
name: 'facebook-auth-session',
keys: ['key1', 'key2']
}))
app.use(passport.initialize());
app.use(passport.session());
app.get('/',(req,res)=>{
res.send(`Hello world ${req.user.displayName}`)
})
app.get('/auth/error', (req, res) => res.send('Unknown Error'))app.get('/auth/facebook',passport.authenticate('facebook'));app.get('/auth/facebook/callback',passport.authenticate('facebook', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
app.listen(8000,()=>{
console.log('Serve is up and running at the port 8000')
})

Now navigate to http://localhost:8000/auth/facebook. You will be redirected to Facebook’s Login page. On logging in to your Facebook account, you will be redirected back to our webpage and you will see your Facebook User name getting displayed on our webpage.

现在导航到http:// localhost:8000 / auth / facebook 。 您将被重定向到Facebook的登录页面。 登录到您的Facebook帐户后,您将被重定向回我们的网页,并且您会看到您的Facebook用户名显示在我们的网页上。

4. Protecting Route and adding a logout view

4.保护路由并添加注销视图

Now let’s add middleware to find if the user has logged in or not. To do that, I am creating a file named auth.js in the Middleware folder.

现在,让我们添加中间件来查找用户是否已登录。 为此,我在Middleware文件夹中创建了一个名为auth.js的文件。

Middleware/auth.js

Middleware/auth.js

const isLoggedIn = (req, res, next) => {if (req.user) {
next();
} else {
res.status(401).send('Not Logged In');
}
}module.exports = isLoggedIn

Once done, pass the middleware to the route /. Now navigate to the URL http://localhost:8000/. Automatically you will be redirected to the /auth/facebook route.

完成后,将中间件传递给路由/ 。 现在导航到URL http://localhost:8000/ . 自动,您将被重定向到/auth/facebook路由。

Now let’s create a function for logout. Just by calling the function req.logout() you can logout from the Facebook Account.

现在让我们创建一个注销功能。 只需调用函数req.logout()即可从Facebook帐户注销。

index.js

index.js

const isLoggedIn = require('./Middleware/auth')app.get('/',isLoggedIn,(req,res)=>{
res.send(`Hello world ${req.user.displayName}`)
})app.get('/logout', (req, res) => {
req.session = null;
req.logout();
res.redirect('/');
})

Just navigate to http://localhost:8000/logout. You will be logged out.

只需浏览至http:// localhost:8000 / logout即可。 您将被注销。

Do visit my previous blog where I have implemented Google OAuth Authentication with Passport JS.

请访问我以前的博客,其中我已使用Passport JS实现了Google OAuth身份验证。

Feel free to contact me for any queries.

如有任何疑问,请随时与我联系。

Email: sjlouji10@gmail.com

电子邮件:sjlouji10@gmail.com

Linkedin: https://www.linkedin.com/in/sjlouji/

Linkedin: https : //www.linkedin.com/in/sjlouji/

Complete code on my GitHub:

我的GitHub上的完整代码:

Happy coding…

编码愉快!

翻译自: https://medium.com/swlh/node-and-passport-js-facebook-authentication-76cbfa903ff3

区块链节点发现 节点认证

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
国密即国家密码局认定的国产密码算法,即商用密码。国密算法是国家密码局制定标准的一系列算法。其中包括了对称加密算法,椭圆曲线非对称加密算法,杂凑算法。具体包括SM1,SM2,SM3等,其中:SM2为国家密码管理局公布的公钥算法,其加密强度为256位。其它几个重要的商用密码算法包括:SM1,对称加密算法,加密强度为128位,采用硬件实现;SM3,密码杂凑算法,杂凑值长度为32字节,和SM2算法同期公布,参见《国家密码管理局公告(第 22 号)》;SMS4,对称加密算法,随WAPI标准一起公布,可使用软件实现,加密强度为128位。商用密码,是指能够实现商用密码算法的加密、解密和认证等功能的技术。(包括密码算法编程技术和密码算法芯片、加密卡等的实现技术)。商用密码技术是商用密码的核心,国家将商用密码技术列入国家秘密,任何单位和个人都有责任和义务保护商用密码技术的秘密。商用密码的应用领域十分广泛,主要用于对不涉及国家秘密内容但又具有敏感性的内部信息、行政事务信息、经济信息等进行加密保护。比如:商用密码可用于企业门禁管理、企业内部的各类敏感信息的传输加密、存储加密,防止非法第三方获取信息内容;也可用于各种安全认证、网上银行、数字签名等。例如:在门禁应用中,采用SM1算法进行身份鉴别和数据加密通讯,实现卡片合法性的验证,保证身份识别的真实性。 安全是关系国家、城市信息、行业用户、百姓利益的关键问题。国家密码管理局针对现有重要门禁系统建设和升级改造应用也提出指导意见,加强芯片、卡片、系统的标准化建设。截止目前,国密门禁系统的升级的案例也逐渐增多,基于自主国产知识产权的CPU卡、CPU卡读写设备及密钥管理系统广泛受到关注。一些厂商如同方锐安在2009年推出CPU卡安全门禁系列产品,在2010年北京安博会上,该公司再次向业界展示出“御”系列CPU卡门禁系统、TF-DF6000系列安全门禁读卡器以及基于CPU卡技术的一卡通系统等主流产品和系统。这些厂商是全国推广的国密门禁产品的先驱者,使“御”系列CPU卡门禁系统广泛应用于政府、监狱、司法、军工企业和大型公共智能建筑等高安全领域。以太坊是互联网新时代的基础:内建货币与支付。用户拥有个人数据主权,且不会被各类应用监听或窃取数据。人人都有权使用开放金融系统。基于中立且开源的基础架构,不受任何组织或个人控制。以太坊主网于 2015 年上线,是世界头部的可编程区块链。和其它区块链一样,以太坊也拥有原生加密货币,叫作 ether (ETH)。 ETH 是一种数字货币, 和比特币有许多相同的功能。 它是一种纯数字货币,可以即时发送给世界上任何地方的任何人。 ETH 的供应不受任何政府或组织控制,它是去中心化且具稀缺性的。 全世界的人们都在使用 ETH 进行支付,或将其作为价值存储和抵押品。但与其它区块链不同的是,以太坊可以做更多的工作。 以太坊是可编程的,开发者可以用它来构建不同于以往的应用程序。这些去中心化的应用程序(或称“dapps”)基于加密货币与区块链技术, 因而值得信任,也就是说 dapps 一旦被“上传”到以太坊,它们将始终按照编好的程序运行。 这些应用程序可以控制数字资产,以便创造新的金融应用; 同时还是去中心化的,这意味着没有任何单一实体或个人可以控制它们。目前,全世界有成千上万名开发者正在以太坊上构建应用程序、发明新的应用程序,其中有许多现在已经可以使用:加密货币钱包:让你可以使用 ETH 或其他数字资产进行低成本的即时支付金融应用程序:让你可以借贷、投资数字资产去中心化市场:让你可以交易数字资产,甚至就现实世界事件的“预测”进行交易游戏:你可以拥有游戏内的资产,甚至可以由此获得现实收益以及更多,更多。以太坊社区是世界上最大最活跃的区块链社区。它包括核心协议开发者、加密经济研究员、密码朋克、挖矿组织、ETH 持有者、应用开发者、普通用户、无政府主义者、财富 500 强公司,以及现在的你。没有公司或中心化的组织能够控制以太坊。 一直以来,以太坊由多元化的全球性社区贡献者来协同进行维护和改善,社区成员耕耘于以太坊的方方面面,从核心协议到应用程序。 这个网站,就像以太坊的其他部分一样,是由一群人共同构建的,并将持续构建下去。本课程定制符合国家标准的以太坊。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值