Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added account linking/email existence check function #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/lib/firebase/authentication/accountLinking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { db, auth } from '../firebaseConfig';
import { getAuth, linkWithPopup, GoogleAuthProvider } from "firebase/auth";

// Example:
// 1. Existing Email/PWD account: ([email protected])
// 2. An user wants to link a new sign-in google account ([email protected])
async function linkingAccount(): Promise<void> {
const auth = getAuth();
const user = auth.currentUser;

if (user) {
// User is signed in
const provider = new GoogleAuthProvider();
try {
await linkWithPopup(user, provider);
console.log("Accounts successfully linked");
} catch (error) {
console.log("Error occured while linking your account", error);
}
} else {
// No user is signed in.
alert("Sign-in needed");
// redirect to the login page
window.location.href = 'route/to/login';
}
}

export { linkingAccount };
7 changes: 2 additions & 5 deletions src/lib/firebase/authentication/googleAuthentication.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { auth } from '../firebaseConfig';
import { FirebaseError } from 'firebase/app';
import { signInWithPopup, signOut, GoogleAuthProvider } from 'firebase/auth';

async function verifyGoogleToken(googleAccessToken: string | undefined): Promise<boolean> {
Expand Down Expand Up @@ -31,10 +30,8 @@ async function signInWithGoogle(): Promise<void> {
} else {
console.log("Please sign in again");
}
} catch (error: unknown) {
if ((error as FirebaseError).code === 'auth/account-exists-with-different-credential') {
console.log("Already existing email address");
}
} catch (error) {
console.log("Unexpected error while signing in");
};
};

Expand Down
17 changes: 17 additions & 0 deletions src/lib/firebase/util/userVerification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { db } from '../firebaseConfig';
import { collection, query, where, getDocs } from "firebase/firestore";

async function checkEmailExists(email: string): Promise<boolean> {
const ref = collection(db, "users");
const q = query(ref, where("email", "==", email));
const querySnapshot = await getDocs(q);
if (!querySnapshot.empty) {
console.log("Email already exists in the database");
return true;
} else {
console.log("Email does not exist. You can register or sign-in with this email");
return false;
}
}

export { checkEmailExists };
Loading