Anonymous
Not logged in
Talk
Contributions
Create account
Log in
DocsMaulana
Search
Editing
Menambahkan JWT Verifikasi node js
(section)
From DocsMaulana
Namespaces
Page
Discussion
More
More
Page actions
Read
Edit
Edit source
History
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= '''<nowiki>#</nowiki> Menambahkan JWT Verifikasi node js''' = Tutorial ini menjelaskan cara menambahkan middleware jwt verifikasi di node js == '''<nowiki>##</nowiki> Prasyarat''' == # Sebelum memulai, pastikan Anda telah menginstal Node.js dan npm di sistem Anda. # Pastikan server sso laravel telah berjalan # Pastikan telah berhasil mengimplementasikan login sso client == '''<nowiki>##</nowiki> Langkah-langkah''' == === <nowiki>###</nowiki> 1. Instal Dependensi === Pertama, pastikan untuk menginstal semua dependensi yang diperlukan dengan menjalankan perintah berikut di terminal: npm install jsonwebtoken === <nowiki>###</nowiki> 2. Buat Aplikasi Node.js === Buatlah file JavaScript untuk middleware verifikasi, misalnya `verify.js`. === <nowiki>###</nowiki> 3. Konfigurasi Middleware === Tambahkan kode berikut ke dalam file `verify.js` Anda:<blockquote> const jwt = require('jsonwebtoken'); const fs = require('fs'); // Mendapatkan kunci publik dari file (dalam kasus ini, file berisi kunci publik dalam format PEM) const publicKey = fs.readFileSync('oauth-public.key', 'utf8'); // Fungsi middleware untuk verifikasi token JWT function verifyToken(req, res, next) { // Mendapatkan token dari header permintaan const token = req.session.accessToken; // Memeriksa apakah token tersedia if (token == undefined) { return res.status(401).json({ status : false, message: 'silahkan login terlebih dahulu', }); } // Melakukan verifikasi token jwt.verify(token, publicKey, function(err, decoded) { if (err) { return res.status(403).json({ status : false, message: 'Token tidak valid.', error : err }); } req.user = decoded; // Menyimpan informasi pengguna dari token di objek permintaan next(); // Lanjutkan ke middleware atau pengendali berikutnya }); } module.exports = verifyToken;</blockquote><nowiki>###</nowiki> 4. Menambahkan Middleware ke route Tambahkan middleware kedalam route:<blockquote> app.use('/protected',verifyToken) app.get('/protected/getuser',(req,res)=>{ res.send(req.user) })</blockquote><nowiki>###</nowiki> 6. Menjalankan Aplikasi Terakhir, jalankan aplikasi Node.js Anda dengan menjalankan perintah berikut di terminal: node app.js
Summary:
Please note that all contributions to DocsMaulana may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Docs Maulana:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation
Navigation
Main page
Laravel
Recent changes
Wiki tools
Wiki tools
Special pages
Page tools
Page tools
User page tools
More
What links here
Related changes
Page information
Page logs