Skip to content

Commit

Permalink
fix: Fixes issue where attempting to signin with google OAuth throws …
Browse files Browse the repository at this point in the history
…a "user exists" error instead of logging in (formbricks#3355)

Co-authored-by: Matthias Nannt <[email protected]>
  • Loading branch information
Pushan2005 and mattinannt authored Oct 11, 2024
1 parent 0224564 commit fca2989
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/lib/authOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ export const authOptions: NextAuthOptions = {
}

if (!user || !credentials) {
throw new Error("No user matches the provided credentials");
throw new Error("Invalid credentials");
}
if (!user.password) {
throw new Error("No user matches the provided credentials");
throw new Error("Invalid credentials");
}

const isValid = await verifyPassword(credentials.password, user.password);

if (!isValid) {
throw new Error("No user matches the provided credentials");
throw new Error("Invalid credentials");
}

return {
Expand Down Expand Up @@ -190,6 +190,7 @@ export const authOptions: NextAuthOptions = {
},
async signIn({ user, account }: any) {
if (account.provider === "credentials" || account.provider === "token") {
// check if user's email is verified or not
if (!user.emailVerified && !EMAIL_VERIFICATION_DISABLED) {
throw new Error("Email Verification is Pending");
}
Expand Down Expand Up @@ -245,7 +246,8 @@ export const authOptions: NextAuthOptions = {
const existingUserWithEmail = await getUserByEmail(user.email);

if (existingUserWithEmail) {
throw new Error("A user with this email exists already.");
// Sign in the user with the existing account
return true;
}

const userProfile = await createUser({
Expand Down

0 comments on commit fca2989

Please sign in to comment.