question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

signInWithEmailLink cannot sign in on web layer

See original GitHub issue

I am trying to log the user on the native + Web layer … but the following only seems to log me into the native layer. The following example is from the docs found here Using Javascript SDK

const signInWithEmailLink = async () => {
  // Get the email if available. This should be available if the user completes
  // the flow on the same device where they started it.
  const emailLink = window.location.href;
  // Confirm the link is a sign-in with email link.
  const result = await FirebaseAuthentication.isSignInWithEmailLink({
    emailLink,
  });
  if (
    result.isSignInWithEmailLink
  ) {
    let email = window.localStorage.getItem('emailForSignIn');
    if (!email) {
      // User opened the link on a different device. To prevent session fixation
      // attacks, ask the user to provide the associated email again.
      email = window.prompt(
        'Please provide your email for confirmation.',
      );
    }
    // The client SDK will parse the code from the link for you.
    const result = await FirebaseAuthentication.signInWithEmailLink({
      email,
      emailLink,
    });
    // Clear email from storage.
    window.localStorage.removeItem('emailForSignIn');
    return result.user;
  } else {
    alert('emailLink is invalid.');
  }
};

Additional context:

Been looking through the firebase SDK docs … but haven’t found a solution for this problem. Im assuming we need to create a credential with the email & emailLink and then login … but haven’t found the correct docs for this.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
robingenzcommented, Nov 2, 2022

You are right. Seems like this example is not for the Firebase JS SDK docs. Please try the following:

const signInWithEmailLink = async () => {
  // Get the email if available. This should be available if the user completes
  // the flow on the same device where they started it.
  const emailLink = window.location.href;
  // Confirm the link is a sign-in with email link.
  const result = await FirebaseAuthentication.isSignInWithEmailLink({
    emailLink,
  });
  if (
    result.isSignInWithEmailLink
  ) {
    let email = window.localStorage.getItem('emailForSignIn');
    if (!email) {
      // User opened the link on a different device. To prevent session fixation
      // attacks, ask the user to provide the associated email again.
      email = window.prompt(
        'Please provide your email for confirmation.',
      );
    }
    // The client SDK will parse the code from the link for you.
	const credential = EmailAuthProvider.credentialWithLink(email, emailLink);
	const auth = getAuth();
	const result = await signInWithCredential(auth, credential);
    // Clear email from storage.
    window.localStorage.removeItem('emailForSignIn');
    return result.user;
  } else {
    alert('emailLink is invalid.');
  }
};

(@trancee Did we mix things up here?)

1reaction
robingenzcommented, Nov 2, 2022

@gerardopar That’s great, thank you! I will update the docs. Let me know if you have any more questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authenticate with Firebase Using Email Link in JavaScript
To complete the sign in on landing page, call signInWithEmailLink with the user's email and the actual email link containing the one-time code....
Read more >
Firebase UI does not work with Safari on Mac or iPhone ...
1. On our webpage app the behaviour seen is -. Select Login from our menu and the Firebase UI renders correctly. Select the...
Read more >
Using Firebase Authentication - FlutterFire
Anonymous sign-in provides an extra layer of security if using Firebase Firestore, Realtime Database or even an external API, since you're able to...
Read more >
capacitor-firebase/authentication - NPM Package Overview
If the user should also be logged in on the web layer (for example ... link is a sign-in with email link suitable...
Read more >
firebase signInWithEmailLink method not working properly
import { signInWithEmailLink } from "firebase/auth"; const [email, setEmail] = useState(""); const ... Sign up using Email and Password.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found