Skip to content

gajjardarshithasmukhbhai/ExpressJs-passportJs-security-googe-authitication

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Passport Js Gmail authtication

mern stack

Documantation of Passport Js

  • step 1:   First you dowload the passport js and passport Js stratergy
  • step 2:    Then After the passport Js code import
  • step 3:    Then After the Authorize Api key for Google + Login is https://www.googleapis.com/auth/plus.login
  • step 4:   and the Google credential and secret Id are set in that passport Js credential
const passport=require('passport');//import the file
const GoogleStrategy=require('passport-google-oauth20');//import the google auth 20
passport.use(new GoogleStrategy({
	callbackURL:'/gajjurock',
	clientID:'security point i not show',
	clientSecret:'security point i not show',
	refreshToken:'security point i not show',
},
()=>{
	//callback function
}))
  • step 5:   then after in google Authroize google redirect set same as you set the callbackURL property in step 4 and then after it's routes is set like
app.get('/gajjurock',(req,res,next)=>{
	res.send('<h1>HEllO</h1>');
});
  • step 7:   you got the email just like this in two steps: app.js
app.get('/passport-google',passportx.authenticate('google',{
	//object which user data you require tell me about the user
	scope:['profile','https://www.googleapis.com/auth/userinfo.email']
}));

passport.js file

passport.use(new GoogleStrategy({
	callbackURL:'/gajjurock',
	clientID:'ercontent.com',
	clientSecret:'vO',
	refreshToken:'tb8mS0NMfiF',
},
(accessToken,refreshToken,profile,done)=>{
	//callback function and Data Of USER
	console.log("->");
	console.log("->",profile.emailId);
	console.log(profile.emails[0].value);//get eamil just like this thorugh callback
}))
  • step 8:    when you want to take profile Data like e-mail,profile photo,username,family Name you take intialize
app.use(passportx.initialize());
app.use(passportx.session());

then after you require set session and cookies store just like this

app.get('/passport-google',passportx.authenticate('google',{
	//object which user data you require tell me about the user
	scope:['profile','https://www.googleapis.com/auth/userinfo.email'],
	// successRedirect: '/accessed',
 //  	failureRedirect: '/access',
 //  	session: false
})
);
  • step 9:   then after session set in so all data in bytestream and then after decode it (-:
passportx.serializeUser(function(user, done) {
  done(null, user);
})
passportx.deserializeUser(function(user, done) {
  done(null, user);
  EMail=user._json.picture;
});


app.get('/gajjurock', 
  passportx.authenticate('google'),
  (req,res)=>{
    // Successful authentication, redirect home.
    // console.log('hello->',EMail);
    res.redirect('/');
  });
  • step 9:
->basically when you use the passport js so that time one thing reminder in passport js
->passport js is middelware and it is based on the clienId and clientSecret and callback url so when you work so that time callback url
is same like the you mention in the developer console

About

in this github repo i introduce lot of Technology like expressJs,passportJs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published