Authentification

Authentification

Starter-SaaS uses next-auth for authentication.

It is possible to have several authentifincations:

  • GoogleProvider
  • GithubProvider
  • Email password (CredentialsProvider)

You can view the configuration in src/lib/auth.ts

  providers: [
    GithubProvider({
      clientId: process.env.GITHUB_ID ?? "",
      clientSecret: process.env.GITHUB_PASS ?? "",
      profile(profile) {
        return {
          id: profile.id.toString(),
          name: profile.name || profile.login,
          gh_username: profile.login,
          email: profile.email,
          image: profile.avatar_url,
        };
      },
    }),
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID ?? '',
      clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? '',
    }),
    CredentialsProvider({
      name: 'Credentials',
      credentials: {
        email: { label: 'Email', type: 'email', placeholder: 'jsmith' },
        password: { label: 'Password', type: 'password' },
      },
      async authorize(credentials) {
        const creds = await loginSchema.parseAsync(credentials);
 
        const user = await prisma.user.findFirst({
          where: { email: creds.email },
        });
 
        if (!user) {
          return null;
        }
 
        const passwordMatch = await bcrypt.compare(
          creds.password,
          user.password as any
        );
 
        if (!passwordMatch) {
          throw new Error(JSON.stringify([{ message: 'Password not good!' }]));
        }
 
        return {
          id: user.id,
          name: user.name,
          email: user.email,
        };
      },
    }),
  ],