I wanted to use Firebase as an authentication provider for my expo app, using their passwordless email magic link. It’s possible to do this without ejecting your Expo app, but it requires that you set up a small “proxy” server that redirects to an Expo generated deep link in the Firebase completion step.
- Create a deep link in Expo.
const expoLink = Linking.makeUrl('your/expo/link');
- Create a link to your redirect server that redirects to your Expo link. I’ve provided a Webtask function that you can test this with.
const FIREBASE_LINK_PROXY = 'https://wt-6e2a5f000b93f69e1b65cf98021e1945-0.sandbox.auth0-extend.com/firebase-authentication-link-redirect';
const proxyUrl = `${FIREBASE_LINK_PROXY}?redirectUrl=${encodeURIComponent(expoLink)}`;
- Send an email using the Firebase JS SDK, passing along your proxy url.
// import firebase from 'firebase/app';
// import 'firebase/auth';
firebase.auth().sendSignInLinkToEmail(email, {
handleCodeInApp: true,
url: proxyUrl
})
.then(/* ... */)
.catch(/* ... */)
// You probably also want to save the email for the completion step
// Here's using Expo's AsyncStorage
AsyncStorage.setItem('@YourApp:unverifiedEmail', email);
- Add the proxy to your authorized domains in Firebase.
If you use the Webtask function from above, add the following domain
wt-6e2a5f000b93f69e1b65cf98021e1945-0.sandbox.auth0-extend.com
to this place (replace <YOUR_FIREBASE_PROJECT>)
https://console.firebase.google.com/project/<YOUR_FIREBASE_PROJECT>/authentication/providers
- Open the link in the email Firebase sends you—you’ll be sent to your Expo app (or asked if on iOS).
- Handle the link in Expo and complete sign-in with the Firebase JS SDK using the link
// The Expo link will be available after successfully
// being redirected into the app.
// Feel free to set this up however you want.
// Here I've put it in componentDidMount()
async componentDidMount() {
const url = await Linking.getInitialURL();
if (url) {
this.handleUrl(url);
}
Linking.addEventListener('url', ({ url }) => {
this.handleUrl(url);
});
};
// Some function that handles the url using the Firebase JS SDK
async function handleUrl(url) {
const isSignInWithEmailLink = firebase.auth().isSignInWithEmailLink(url);
if (isSignInWithEmailLink) {
try {
const email = await AsyncStorage.getItem('@YourApp:unverifiedEmail');
const result = await firebase.auth().signInWithEmailLink(email, url);
this.setState({ user: result.user });
} catch (error) {
console.log(error.message);
}
}
}
- You should now have the
user
object from Firebase. Use it!
Redirect server code
This is just Node.js. You can adapt it easily to any serverless function provider (Zeit Now, Webtask, AWS Lambda, Azure Functions, Netlify Functions, Firebase Functions, etx…) or you own Node.js server.
const url = require('url');
module.exports = (req, res) => {
const { query } = url.parse(req.url, true);
const { redirectUrl, ...rest } = query;
if (redirectUrl) {
const Location = url.format({ pathname: redirectUrl, query: rest });
res.writeHead(302, { Location });
res.end(`Redirecting to ${Location}`);
} else {
res.writeHead(400);
res.end('You must provide a `redirectUrl` in the query');
}
};