首頁 > web前端 > js教程 > Node.js應用程序的護照身份驗證

Node.js應用程序的護照身份驗證

William Shakespeare
發布: 2025-02-15 09:39:12
原創
408 人瀏覽過

Passport Authentication for Node.js Applications

本教程使用Passport.js(一種流行的身份驗證中間件)在Node.js Web應用程序中演示了Facebook和GitHub身份驗證。 護照簡化了OAuth和OpenID Connect Integration。

密鑰概念:

  • Passport.js:>
  • express.js:
  • > node.js的Web應用程序框架用於構建應用程序的結構和處理路由。 > oauth 2.0:
  • > Facebook和github使用的授權框架。
  • > >>策略:
  • Passport使用每個提供商的策略(例如,
  • >,)。 passport-facebook passport-github
  • >應用程序設置:

>

    項目初始化:
  1. 創建一個項目目錄並初始化node.js project:>

    mkdir AuthApp
    cd AuthApp
    npm init -y
    登入後複製
    html設置:
  2. 在項目root中創建
  3. auth.html

    >
    <!DOCTYPE html>
    <html>
    <head>
      <title>Node.js OAuth</title>
    </head>
    <body>
      <a href="https://www.php.cn/link/cc1d70ad9d0ce820738dc9ffc4053a76">Sign in with Facebook</a><br><br>
      <a href="https://www.php.cn/link/899a13400527fd3b12105ec17a67dbac">Sign in with GitHub</a>
    </body>
    </html>
    登入後複製
    安裝依賴項:
  4. 安裝express和Passport:
  5. express App:
    npm install express passport --save
    登入後複製
    創建
  6. >

    運行應用程序()並驗證index.js顯示HTML。停止應用程序(

    )。
    const express = require('express');
    const app = express();
    const port = process.env.PORT || 3000;
    
    app.get('/', (req, res) => res.sendFile('auth.html', { root: __dirname }));
    app.listen(port, () => console.log(`App listening on port ${port}`));
    登入後複製

    node index.js http://localhost:3000 Ctrl C護照配置:

>安裝提供商策略:安裝Facebook和github策略:

  1. >

    中的護照設置
    npm install passport-facebook passport-github --save
    登入後複製
  2. > Facebook身份驗證:index.js

    const passport = require('passport');
    app.use(passport.initialize());
    app.use(passport.session());
    
    app.get('/success', (req, res) => res.send("Successfully logged in!"));
    app.get('/error', (req, res) => res.send("Error logging in."));
    
    passport.serializeUser((user, done) => done(null, user));
    passport.deserializeUser((user, done) => done(null, user));
    登入後複製
>創建一個Facebook應用程序:

遵循Facebook的說明,以創建一個新應用並獲得您的>和

  1. >在>中配置Facebook策略:> App ID App Secret記住要將Facebook應用程序的有效OAuth重定向URIS配置為

    >。
  2. github身份驗證:index.js

    const FacebookStrategy = require('passport-facebook').Strategy;
    const FACEBOOK_APP_ID = 'YOUR_FACEBOOK_APP_ID'; // Replace with your App ID
    const FACEBOOK_APP_SECRET = 'YOUR_FACEBOOK_APP_SECRET'; // Replace with your App Secret
    
    passport.use(new FacebookStrategy({
        clientID: FACEBOOK_APP_ID,
        clientSecret: FACEBOOK_APP_SECRET,
        callbackURL: "https://www.php.cn/link/cc1d70ad9d0ce820738dc9ffc4053a76/callback",
        profileFields: ['id', 'displayName', 'photos', 'email'] //Optional: Specify fields to retrieve
    }, (accessToken, refreshToken, profile, done) => {
        done(null, profile);
    }));
    
    app.get('https://www.php.cn/link/cc1d70ad9d0ce820738dc9ffc4053a76', passport.authenticate('facebook'));
    app.get('https://www.php.cn/link/cc1d70ad9d0ce820738dc9ffc4053a76/callback',
        passport.authenticate('facebook', { failureRedirect: '/error' }),
        (req, res) => res.redirect('/success')
    );
    登入後複製

    http://localhost:3000https://www.php.cn/link/cc1d70ad9d0ce820738dc9ffc4053a76/callback>創建一個github應用程序:

    創建一個新的github應用,並獲得您的

>中的github策略:
  1. > 將您的github應用程序的授權回調URL配置為Client ID>Client Secret>

  2. 運行應用程序:

    啟動服務器(node index.js)並測試Facebook和GitHub登錄鏈接。 該路線將表示成功的身份驗證。 請記住,用您的實際值替換佔位符和秘密。 這提供了一個基本框架; 對於生產就緒的應用程序,需要在數據庫中進行錯誤處理和用戶持久性。 >

以上是Node.js應用程序的護照身份驗證的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板