Mai amfani Abu ne mai mahimmanci a cikin ci gaban Nau'in Rubutun zamani, musamman idan ana maganar sarrafa amincin mai amfani. Matsayinta shine ainihin ษaukar bayanai game da mai amfani akan buฦatun daban-daban a cikin zama. Kodayake akwai hanyoyi da yawa don aiwatar da req.user, samun bayyananniyar hanya mai inganci na iya zama mai canza wasa.
Fahimtar req.user yana farawa ta hanyar fahimtar matsayinsa a cikin masu shiga tsakani na tsarin HTTP. A cikin Express.js, alal misali, middleware yana sarrafa kayan req.user kuma yana haษa mahimman bayanai. A cikin wannan mahallin, ษaya daga cikin manyan kayan aikin mu shine Passport.js, ฦaฦฦarfan ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun ฦwararrun kayan aikinmu.
Fahimtar Fasfo.js
Passport.js matsakaici ne na Node.js wanda aikinsa shine tabbatar da buฦatun. Yana ba da dabaru daban-daban don tantancewa, kamar OAuth, OpenID, da sauransu. Zaษin dabarun ya dogara da bukatun aikin. Yana da mahimmanci a fahimta, ko da yake, Passport.js baya sarrafa zaman mai amfani kai tsaye. Madadin haka, yana ba da wannan aikin ga aikace-aikacen, yana ba masu haษakawa damar zaษar hanyoyin su don gudanar da zaman da serialization na mai amfani.
Amfani da Passport.js ya haษa da daidaita dabarun farko. Ana yin wannan ta amfani da `passport.use()`inda ka ฦididdige dabarun tare da aikin sake kira da ke tabbatar da masu amfani.
Aiwatar da amincin mai amfani tare da Typescript
import * as passport from 'passport'; import { Strategy as LocalStrategy } from 'passport-local'; passport.use(new LocalStrategy( function (username, password, done) { User.findOne({ username: username }, function (err, user) { if (err) { return done(err); } if (!user) { return done(null, false); } if (!user.verifyPassword(password)) { return done(null, false); } return done(null, user); }); } ));
A cikin wannan misalin, muna amfani da `passport.use()` tare da `LocalStrategy`, ษaya daga cikin ginannen dabarun tantance fasfot.js na cikin gida. Idan an sami mai amfani da ya dace da sunan mai amfani da kalmar sirri, ana mayar da shi; in ba haka ba 'anyi' ana kiran shi da 'ฦarya'.
Haษin req.user
Bayan kafa Passport.js, za mu fara ma'amala da kayan req.user. Ta hanyar tsoho, Passport.js yana haษa abu mai amfani (wanda aka samo daga ingantaccen dabarun) zuwa req.user.
app.get('/example', passport.authenticate('local'), function(req, res) { // If this function gets called, authentication was successful. // `req.user` contains the authenticated user. res.redirect('/users/' + req.user.username); });
Anan, `req.user` zai ฦunshi ingantaccen mai amfani idan an yi nasarar tantancewa. Ta wannan hanyar, zaku iya amfani da `req.user` don samun damar bayanan mai amfani a cikin sauran hanyoyinku da na tsakiya.
A taฦaice, req.user yana wakiltar wani muhimmin sashi na babban tsarin sarrafa bayanan mai amfani da ingantaccen aiki a cikin aikace-aikacen Rubutun ku. Yin amfani da damarsa na iya sa aikace-aikacen ku ya fi aminci, daidaitawa, da abokantaka. Makullin shine fahimtar matsayinsa a cikin mahallin tsarin HTTP ษin da kuka zaษa da sauran ษakunan karatu masu alaฦa kamar Passport.js.