passport-facebook
passport-github
プロジェクトの初期化:プロジェクトディレクトリを作成し、node.jsプロジェクトを初期化します:
htmlセットアップ:
プロジェクトルート:mkdir AuthApp cd AuthApp npm init -y
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>
Express App:
Createnpm install express passport --save
アプリ()を実行し、HTMLを表示しますを確認します。アプリを停止します(index.js
)。
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の戦略をインストール:
パスポートのセットアップ
:npm install passport-facebook passport-github --save
index.js
facebook認証:
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の指示に従って、新しいアプリを作成し、
と
Facebook戦略の構成App ID
:App Secret
。
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アプリを作成して、と。
を取得します:Client ID
Client Secret
でgithub戦略を構成します
に設定します
index.js
const GitHubStrategy = require('passport-github').Strategy; const GITHUB_CLIENT_ID = 'YOUR_GITHUB_CLIENT_ID'; // Replace with your Client ID const GITHUB_CLIENT_SECRET = 'YOUR_GITHUB_CLIENT_SECRET'; // Replace with your Client Secret passport.use(new GitHubStrategy({ clientID: GITHUB_CLIENT_ID, clientSecret: GITHUB_CLIENT_SECRET, callbackURL: "https://www.php.cn/link/899a13400527fd3b12105ec17a67dbac/callback" }, (accessToken, refreshToken, profile, done) => { done(null, profile); })); app.get('https://www.php.cn/link/899a13400527fd3b12105ec17a67dbac', passport.authenticate('github')); app.get('https://www.php.cn/link/899a13400527fd3b12105ec17a67dbac/callback', passport.authenticate('github', { failureRedirect: '/error' }), (req, res) => res.redirect('/success') );
サーバー(node index.js
ルートは、認証の成功を示します。 プレースホルダーIDと秘密を実際の値に置き換えることを忘れないでください。 これは、基本的なフレームワークを提供します。 生産対応のアプリケーションには、データベースでのエラー処理とユーザーの永続性が必要です。
以上がnode.jsアプリケーションのパスポート認証の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。