Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

[Example] How to use CredentialProvider #20

Open
robodove opened this issue Oct 11, 2022 · 0 comments
Open

[Example] How to use CredentialProvider #20

robodove opened this issue Oct 11, 2022 · 0 comments

Comments

@robodove
Copy link

robodove commented Oct 11, 2022

import AstroAuth from "@astro-auth/core";
import { CredentialProvider } from "@astro-auth/providers";
import db from "../../../db";
import bcrypt from "bcryptjs"

type SignUpOptions = {
  email: string;
  password: string;
  confirmPassword: string;
  signUp: true;
}

type SignInOptions = {
  email: string;
  password: string;
}

type AuthOptions = SignUpOptions

const signUpHandler = async (db: any, { email, password, confirmPassword }: Omit<SignUpOptions, "signUp">) => {
  if (password !== confirmPassword) return false;
  const hashedPassword = await bcrypt.hash(password, 10)
  try {
    await db.user.create({
      data: {
        email,
        hashedPassword,
      }
    })
    return true;
  } catch (e) {
    return false
  }
}

const signInHandler = async (db: any, { email, password }: Pick<SignInOptions, "email"|"password">) => {
    const user = await db.user.findFirst({ where: { email } })
    if (!user) return false;
    const match = await bcrypt.compare(password, user.hashedPassword)
    if (!match) return false;
  
    return true;
}

export const all = AstroAuth({
  authProviders: [
    CredentialProvider({
       async authorize({ email, password, signUp, confirmPassword }: AuthOptions) {
        if (signUp) {
          return signUpHandler(db, { email, password, confirmPassword })
        }
  
        return signInHandler(db, { email, password })
  },
})
  ],
  hooks: {},
});

Assuming passwords are hashed with bcryptjs and there is a db (in this case it's prisma with sqlite)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant