function initAuth(app) {
  // Middleware to use passport
  app.use(passport.initialize());
  app.use(passport.session());

  // For saving user creds in cookie
  passport.serializeUser(({ id }, done) => done(null, id));

  // For retrieving user creds object from cookie
  passport.deserializeUser((id, done) => UserData.findById(id, done));

  // Google Auth config via passport
  passport.use(
    new GoogleTokenStrategy(
      {
        clientID: process.env.GOOGLE_CLIENT_ID,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET
      },
      (accessToken, refreshToken, profile, done) => {
        UserCreds.findOrCreate(accessToken, refreshToken, profile, (err, userCred) => {
          if (err) return done(err, null);
          UserData.findById(userCred.userDataId, (err2, user) => {
            return done(err2, user);
          });
        });
      }
    )
  );

  app.post('/auth/google', passport.authenticate('google-token', { session: true }), (req, res) => {
    if (!req.user) {
      return res.send(401, 'User Not Authenticated');
    }
    return res.status(200).send(JSON.stringify(req.user));
  });

  // Logout Route
  app.get('/auth/logout', (req, res, next) => {
    req.logout();
    req.session.destroy(err => {
      if (err) {
        return next(err);
      }
      res.clearCookie('connect.sid', { path: '/' });
      return res.sendStatus(200);
    });
  });
}
/**
 * Initializes passport and authentication-related endpoints for the entire express application.
 */
Initializes passport and authentication-related endpoints for the entire express application.
Initializes passport and authentication - related endpoints for the entire express application .
function initAuth(app) {
  app.use(passport.initialize());
  app.use(passport.session());
  passport.serializeUser(({ id }, done) => done(null, id));
  passport.deserializeUser((id, done) => UserData.findById(id, done));
  passport.use(
    new GoogleTokenStrategy(
      {
        clientID: process.env.GOOGLE_CLIENT_ID,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET
      },
      (accessToken, refreshToken, profile, done) => {
        UserCreds.findOrCreate(accessToken, refreshToken, profile, (err, userCred) => {
          if (err) return done(err, null);
          UserData.findById(userCred.userDataId, (err2, user) => {
            return done(err2, user);
          });
        });
      }
    )
  );
  app.post('/auth/google', passport.authenticate('google-token', { session: true }), (req, res) => {
    if (!req.user) {
      return res.send(401, 'User Not Authenticated');
    }
    return res.status(200).send(JSON.stringify(req.user));
  });
  app.get('/auth/logout', (req, res, next) => {
    req.logout();
    req.session.destroy(err => {
      if (err) {
        return next(err);
      }
      res.clearCookie('connect.sid', { path: '/' });
      return res.sendStatus(200);
    });
  });
}
function initAuth ( app ) { app . use ( passport . initialize ( ) ) ; app . use ( passport . session ( ) ) ; passport . serializeUser ( ( { id } , done ) => done ( null , id ) ) ; passport . deserializeUser ( ( id , done ) => UserData . findById ( id , done ) ) ; passport . use ( new GoogleTokenStrategy ( { clientID : process . env . GOOGLE_CLIENT_ID , clientSecret : process . env . GOOGLE_CLIENT_SECRET } , ( accessToken , refreshToken , profile , done ) => { UserCreds . findOrCreate ( accessToken , refreshToken , profile , ( err , userCred ) => { if ( err ) return done ( err , null ) ; UserData . findById ( userCred . userDataId , ( err2 , user ) => { return done ( err2 , user ) ; } ) ; } ) ; } ) ) ; app . post ( '/auth/google' , passport . authenticate ( 'google-token' , { session : true } ) , ( req , res ) => { if ( ! req . user ) { return res . send ( 401 , 'User Not Authenticated' ) ; } return res . status ( 200 ) . send ( JSON . stringify ( req . user ) ) ; } ) ; app . get ( '/auth/logout' , ( req , res , next ) => { req . logout ( ) ; req . session . destroy ( err => { if ( err ) { return next ( err ) ; } res . clearCookie ( 'connect.sid' , { path : '/' } ) ; return res . sendStatus ( 200 ) ; } ) ; } ) ; }
Initializes passport and authentication-related endpoints for the entire express application.
Initializes passport and authentication - related endpoints for the entire express application .
// Middleware to use passport // For saving user creds in cookie // For retrieving user creds object from cookie // Google Auth config via passport // Logout Route