blitz-js / blitz

⚡️ The Missing Fullstack Toolkit for Next.js
https://Blitzjs.com
MIT License
13.67k stars 798 forks source link

Blitz passport auth don't call local sqlite db #2562

Closed hariria closed 3 years ago

hariria commented 3 years ago

What do you want and why?

I'm having an issue in my Vercel prod build where when I try to call blitz passport.js auth for google sign in, it seems to call my sqlite database even though there are no references to my local database anywhere in the file. I'd like to sign the user in and create a session for that user without having to access the database. Is this possible?

Here's an example of the error logs on Vercel functions for my build.

Screen Shot 2021-07-08 at 7 30 01 PM

Additional context

My [...auth].ts file

import { passportAuth } from 'blitz';
import client from 'app/components/Apollo/apollo-client';
import { gql } from '@apollo/client';
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import { Strategy as OutlookStrategy } from 'passport-outlook';
import { Strategy as LinkedinStrategy } from 'passport-linkedin-oauth2';
import { AES } from 'crypto-js';

export default passportAuth((ctx) => ({
  successRedirectUrl: '/',
  errorRedirectUrl: '/',
  // may need secure proxy for ssl, see @https://blitzjs.com/docs/passportjs
  strategies: [
    {
      authenticateOptions: {
        scope: ['email', 'profile'],
      },
      strategy: new GoogleStrategy(
        {
          clientID: <CLIENT_ID>,
          clientSecret: <CLIENT_SECRET>,
          callbackURL: '/api/auth/google/callback',
        },
        (async (accessToken, refreshToken, profile, done) => {
          // User.findOrCreate({ googleId: profile.id }, (err, user) => cb(err, user));
          const email: string = profile.emails && profile.emails[0]?.value;
          const firstName: string = profile.name.givenName;
          const lastName: string = profile.name.familyName;
          const profileUrl: string = profile.photos[0].value;
          const idToken: string = profile.id;
          const userInfo = {
            email,
            firstName,
            lastName,
            profileUrl,
            idToken,
          };

            ....

            const publicData = {
              userId: 1,
              ...login,
              email,
              // roles: ['user'],
              // handle: 'google',
            };

            const redirectUrl = '/admin';

            done(null, {
              publicData,

              // redirectUrl
            });
          } catch (err) {
            console.error(err);
          }
        }),
      ), // Provide initialized passport strategy here
    },
  ],
}));
flybayer commented 3 years ago

Blitz built-in auth (which passport adapter uses) requires a database to manage sessions. If you don't want that, then you'll need to use some other auth package like auth0 or vanilla passport. Make sense?