oauth-everything / passport-discord

A Passport strategy for authenticating with https://discordapp.com/
Mozilla Public License 2.0
20 stars 4 forks source link

Fix object lint errors, output ESM + CJS #12

Open psibean opened 2 years ago

psibean commented 2 years ago

This isn't a necessary or a required change, I did it more out of interest as I had initially implemented my own discord oauth2 passport strategy prior to finding this repo.

cjs output goes to dist/cjs, esm output goes to dist/esm, type output goes into dist and is done independently of the builds via it's own build:types script.

  34:31  error  Don't use `object` as a type. The `object` type is currently hard to use ([see this issue](https://github.com/microsoft/TypeScript/issues/21732)).
Consider using `Record<string, unknown>` instead, as it allows you to more easily inspect and use the keys                                                                                         @typescript-eslint/ban-types       34:31  error  Don't use `object` as a type. The `object` type is currently hard to use ([see this issue](https://github.com/microsoft/TypeScript/issues/21732)).
Consider using `Record<string, unknown>` instead, as it allows you to more easily inspect and use the keys                                                                                         @typescript-eslint/ban-types     
  34:47  error  Don't use `object` as a type. The `object` type is currently hard to use ([see this issue](https://github.com/microsoft/TypeScript/issues/21732)).Consider using `Record<string, unknown>` instead, as it allows you to more easily inspect and use the keys    

These lint errors were present, so I replaced all object type uses as recommended.

I've tested these changes using npm pack and installing the local file on a live application with the following configuration:

The DiscordUserProfile type below is my own which extends the included Profile type (covers the identify scope).

new Strategy({
  clientID: DISCORD_CLIENT_ID,
  clientSecret: DISCORD_CLIENT_SECRET,
  state: true,
  pkce: true,
  scope: ['identify', 'email'],
  passReqToCallback: true,
},
  async (req: Request, accessToken: string, refreshToken: string, profile: DiscordUserProfile, cb: any) => {
    const userSelect = {
      user: {
        select: {
          id: true,
          email: true,
        }
      }
    };
    const existingProvider = await prisma.authProvider.findFirst({
      where: {
        providerUserId: profile.id,
        name: 'discord'
      },
      include: userSelect
    });

    if (existingProvider === null) {
      const existingUser = await prisma.user.findUnique({
        where: { email: profile.email },
        select: userSelect.user.select,
      });
      if (existingUser === null) {
        if (!profile.verified) {
          cb({ error: "Your Discord email must be verified to use it as a sign in option." }, null)
        }

        const newProvider = await prisma.authProvider.create({
          data: {
            name: 'discord',
            providerUserId: profile.id,
            user: {
              create: {
                email: profile.email
              },
            }
          },
          include: userSelect
        })
        cb(null, newProvider.user);
      } else {
        await prisma.authProvider.create({
          data: {
            name: 'discord',
            providerUserId: profile.id,
            user: {
              connect: {
                email: profile.email
              }
            }
          }
        })
        cb(null, existingUser);
      }
    } else  {
      cb(null, existingProvider.user);
    }
  });

Take it or leave it, the changes are here!