Dieses Tutorial zeigt die Facebook- und Github -Authentifizierung in einer Webanwendung von node.js mit passport.js, einer beliebten Authentifizierung Middleware. Pass vereinfacht die Integration von OAuth und OpenID Connect.
Schlüsselkonzepte:
passport-facebook
, passport-github
). Anwendungsaufbau:
Projektinitialisierung: Erstellen Sie ein Projektverzeichnis und initialisieren Sie ein Node.js -Projekt:
mkdir AuthApp cd AuthApp npm init -y
HTML -Setup: Erstellen auth.html
im Projektroot:
<!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>
Abhängigkeiten installieren: Express und Pass installieren:
npm install express passport --save
Express App: erstellen 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}`));
Führen Sie die App (node index.js
) aus und überprüfen Sie http://localhost:3000
Zeigt die HTML an. Stoppen Sie die App (Ctrl C
).
Passkonfiguration:
Anbieterstrategien installieren: Facebook- und GitHub -Strategien installieren:
npm install passport-facebook passport-github --save
Pass -Setup in 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 -Authentifizierung:
Erstellen Sie eine Facebook -App: Befolgen Sie die Anweisungen von Facebook, um eine neue App zu erstellen und Ihr App ID
und App Secret
zu erhalten.
Konfigurieren Sie die Facebook -Strategie in : 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') );
zu konfigurieren. http://localhost:3000https://www.php.cn/link/cc1d70ad9d0ce820738dc9ffc4053a76/callback
GitHub -Authentifizierung:
Erstellen Sie eine GitHub -App: Erstellen Sie eine neue Github -App und erhalten Sie Ihr und Client ID
. Client Secret
Konfigurieren Sie die GitHub -Strategie in : 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') );
. http://localhost:3000https://www.php.cn/link/899a13400527fd3b12105ec17a67dbac/callback
Führen Sie die Anwendung aus:
Starten Sie den Server (node index.js
) und testen Sie die Facebook- und Github -Links. Die /success
Route zeigt eine erfolgreiche Authentifizierung an. Denken Sie daran, Platzhalter -IDs und Geheimnisse durch Ihre tatsächlichen Werte zu ersetzen. Dies bietet einen grundlegenden Rahmen; Fehlerbehandlung und Benutzerdauer in einer Datenbank wäre für eine produktionsbereite Anwendung erforderlich.
Das obige ist der detaillierte Inhalt vonPassauthentifizierung für Node.js -Anwendungen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!