An warware: req.user

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.

Shafi posts:

Leave a Comment