Giter Site home page Giter Site logo

expressjs-passportjs-security-googe-authitication's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.