text: import passport from 'passport';
  import { OAuth2Strategy as Strategy } from 'passport-google-oauth';
  
  import User from './models/User';
  
  export default function auth({ ROOT_URL, server }) {
    const verify = async (accessToken, refreshToken, profile, verified) => {
      let email;
      let avatarUrl;
  
      if (profile.emails) {
        email = profile.emails[0].value;
      }
  
      if (profile.photos && profile.photos.length > 0) {
        avatarUrl = profile.photos[0].value.replace('sz=50', 'sz=128');
      }
  
      try {
        const user = await User.signInOrSignUp({
          googleId: profile.id,
          email,
          googleToken: { accessToken, refreshToken },
          displayName: profile.displayName,
          avatarUrl,
        });
        verified(null, user);
      } catch (err) {
        verified(err);
        console.log(err); // eslint-disable-line
      }
    };
    passport.use(new Strategy(
      {
        clientID: process.env.Google_clientID,
        clientSecret: process.env.Google_clientSecret,
        callbackURL: `${ROOT_URL}/oauth2callback`,
      },
      verify,
    ));
  
    passport.serializeUser((user, done) => {
      done(null, user.id);
    });
  
    passport.deserializeUser((id, done) => {
      User.findById(id, User.publicFields(), (err, user) => {
        done(err, user);
      });
    });
  
    server.use(passport.initialize());
    server.use(passport.session());
  
    server.get('/auth/google', (req, res, redirectUrl) => {
      if (req.query && req.query.redirectUrl && req.query.redirectUrl.startsWith('/')) {
        req.session.finalUrl = req.query.redirectUrl;
      } else {
        req.session.finalUrl = null;
      }
  
      passport.authenticate('google', {
        scope: ['profile', 'email'],
        prompt: 'select_account',
      })(req, res, redirectUrl);
    });
  
    server.get(
      '/oauth2callback',
      passport.authenticate('google', {
        failureRedirect: '/login',
      }),
      (req, res) => {
        if (req.user && req.user.isAdmin) {
          res.redirect('/admin');
        } else if (req.session.finalUrl) {
          res.redirect(req.session.finalUrl);
        } else {
          res.redirect('/my-books');
        }
      },
    );
  
    server.get('/logout', (req, res) => {
      req.logout();
      res.redirect('/login');
    });
  }
  