Open kirill-develops opened 2 years ago
Hey team,
I'm trying to figure out why this code receives the Failed to fetch user profile error
For context, it appears to be happening on first attempts and following attempts go through no problem.
app.use( expressSession({ secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: true, }), ); // =========== Passport Config ============ app.use(passport.initialize()); app.use(passport.session()); passport.use( new GoogleStrategy( { clientID: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, callbackURL: process.env.GOOGLE_CALLBACK_URL, scope: ['profile', 'email', 'https://www.googleapis.com/auth/plus.login'], }, (_accessToken, _refreshToken, profile, done) => { const id = String(profile.id); knex('users') .select('id') .where({ google_id: id }) .then((user) => { if (user.length) { done(null, user[0]); } else { knex('users') .insert({ google_id: id, avatar_url: profile._json.picture, first_name: profile.name.givenName, last_name: profile.name.familyName, email: profile._json.email, }) .then((userId) => { done(null, { id: userId[0] }); }) .catch((err) => { console.log('Error creating a user', err); }); } }) .catch((err) => { console.log('Error fetching a user', err); }); }, ), ); passport.serializeUser((user, done) => { done(null, user.id); }); passport.deserializeUser((userId, done) => { knex('users') .where({ id: userId }) .then((user) => { done(null, user[0]); }) .catch((err) => { console.log('Error finding user', err); }); }); router.get('/google', (req, _res, next) => { req.session.analysis_id = req._parsedUrl.query; next(); }, passport.authenticate('google')); router.get( '/google/callback', passport.authenticate('google', { failureRedirect: `${process.env.CLIENT_URL}/auth-fail`, }), (req, res) => { const param = req.session.analysis_id; // Successful authentication, redirect to client-side application if (!param || param === '/') { res.redirect(`${process.env.CLIENT_URL}/dashboard`); } else { res.redirect(`${process.env.CLIENT_URL}${param}`); } }, );
Hey team,
I'm trying to figure out why this code receives the Failed to fetch user profile error
For context, it appears to be happening on first attempts and following attempts go through no problem.