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

Use async and await #383

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"env": {
"browser": true,
"commonjs": true,
"es2021": true
"es2022": true
},
"parserOptions": {
"ecmaVersion": 12,
Expand Down
19 changes: 9 additions & 10 deletions auth-next/anonymous.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]

function anonSignIn() {
async function anonSignIn() {
// [START auth_anon_sign_in]
const { getAuth, signInAnonymously } = require("firebase/auth");

const auth = getAuth();
signInAnonymously(auth)
.then(() => {
// Signed in..
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ...
});
try {
await signInAnonymously(auth);
// Signed in..
} catch (error) {
const errorCode = error.code;
const errorMessage = error.message;
// ...
}
// [END auth_anon_sign_in]
}
56 changes: 27 additions & 29 deletions auth-next/auth-state-persistence.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]

function setPersistenceSession() {
async function setPersistenceSession() {
const email = "...";
const password = "...";

// [START auth_set_persistence_session]
const { getAuth, setPersistence, signInWithEmailAndPassword, browserSessionPersistence } = require("firebase/auth");

const auth = getAuth();
setPersistence(auth, browserSessionPersistence)
.then(() => {
// Existing and future Auth states are now persisted in the current
// session only. Closing the window would clear any existing state even
// if a user forgets to sign out.
// ...
// New sign-in will be persisted with session persistence.
return signInWithEmailAndPassword(auth, email, password);
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
});
try {
await setPersistence(auth, browserSessionPersistence);
// Existing and future Auth states are now persisted in the current
// session only. Closing the window would clear any existing state even
// if a user forgets to sign out.
// ...
// New sign-in will be persisted with session persistence.
return signInWithEmailAndPassword(auth, email, password);
} catch (error) {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
}
// [END auth_set_persistence_session]
}

function setPersistenceNone() {
async function setPersistenceNone() {
// [START auth_set_persistence_none]
const { getAuth, setPersistence, signInWithRedirect, inMemoryPersistence, GoogleAuthProvider } = require("firebase/auth");

const auth = getAuth();
setPersistence(auth, inMemoryPersistence)
.then(() => {
const provider = new GoogleAuthProvider();
// In memory persistence will be applied to the signed in Google user
// even though the persistence was set to 'none' and a page redirect
// occurred.
return signInWithRedirect(auth, provider);
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
});
try {
await setPersistence(auth, inMemoryPersistence);
const provider = new GoogleAuthProvider();
// In memory persistence will be applied to the signed in Google user
// even though the persistence was set to 'none' and a page redirect
// occurred.
return signInWithRedirect(auth, provider);
} catch (error) {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
}
// [END auth_set_persistence_none]
}
64 changes: 34 additions & 30 deletions auth-next/custom-email-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function handleUserManagementQueryParams() {
// This is the minimum configuration required for the API to be used.
const config = {
'apiKey': "YOUR_API_KEY" // Copy this key from the web initialization
// snippet found in the Firebase console.
// snippet found in the Firebase console.
};
const app = initializeApp(config);
const auth = getAuth(app);
Expand All @@ -49,7 +49,7 @@ function handleUserManagementQueryParams() {
handleVerifyEmail(auth, actionCode, continueUrl, lang);
break;
default:
// Error: invalid mode.
// Error: invalid mode.
}
}, false);
// [END auth_handle_mgmt_query_params]
Expand All @@ -58,20 +58,22 @@ function handleUserManagementQueryParams() {
// [START auth_handle_reset_password]
const { verifyPasswordResetCode, confirmPasswordReset } = require("firebase/auth");

function handleResetPassword(auth, actionCode, continueUrl, lang) {
async function handleResetPassword(auth, actionCode, continueUrl, lang) {
// Localize the UI to the selected language as determined by the lang
// parameter.

// Verify the password reset code is valid.
verifyPasswordResetCode(auth, actionCode).then((email) => {
try {
// Verify the password reset code is valid.
const email = await verifyPasswordResetCode(auth, actionCode);
const accountEmail = email;

// TODO: Show the reset screen with the user's email and ask the user for
// the new password.
const newPassword = "...";

// Save the new password.
confirmPasswordReset(auth, actionCode, newPassword).then((resp) => {
try {
// Save the new password.
const resp = await confirmPasswordReset(auth, actionCode, newPassword);
// Password reset has been confirmed and new password updated.

// TODO: Display a link back to the app, or sign-in the user directly
Expand All @@ -81,55 +83,57 @@ function handleResetPassword(auth, actionCode, continueUrl, lang) {
// TODO: If a continue URL is available, display a button which on
// click redirects the user back to the app via continueUrl with
// additional state determined from that URL's parameters.
}).catch((error) => {
} catch (error) {
// Error occurred during confirmation. The code might have expired or the
// password is too weak.
});
}).catch((error) => {
}
} catch (error) {
// Invalid or expired action code. Ask user to try to reset the password
// again.
});
}
}
// [END auth_handle_reset_password]

// [START auth_handle_recover_email]
const { checkActionCode, applyActionCode, sendPasswordResetEmail } = require("firebase/auth");

function handleRecoverEmail(auth, actionCode, lang) {
async function handleRecoverEmail(auth, actionCode, lang) {
// Localize the UI to the selected language as determined by the lang
// parameter.
let restoredEmail = null;
// Confirm the action code is valid.
checkActionCode(auth, actionCode).then((info) => {
try {
// Confirm the action code is valid.
const info = await checkActionCode(auth, actionCode);
// Get the restored email address.
restoredEmail = info['data']['email'];

// Revert to the old email.
return applyActionCode(auth, actionCode);
}).then(() => {
await applyActionCode(auth, actionCode);
// Account email reverted to restoredEmail

// TODO: Display a confirmation message to the user.

// You might also want to give the user the option to reset their password
// in case the account was compromised:
sendPasswordResetEmail(auth, restoredEmail).then(() => {
try {
// You might also want to give the user the option to reset their password
// in case the account was compromised:
await sendPasswordResetEmail(auth, restoredEmail);
// Password reset confirmation sent. Ask user to check their email.
}).catch((error) => {
} catch (error) {
// Error encountered while sending password reset code.
});
}).catch((error) => {
}
} catch (error) {
// Invalid code.
});
}
}
// [END auth_handle_recover_email]

// [START auth_handle_verify_email]
function handleVerifyEmail(auth, actionCode, continueUrl, lang) {
// Localize the UI to the selected language as determined by the lang
// parameter.
// Try to apply the email verification code.
applyActionCode(auth, actionCode).then((resp) => {
async function handleVerifyEmail(auth, actionCode, continueUrl, lang) {
try {
// Localize the UI to the selected language as determined by the lang
// parameter.
// Try to apply the email verification code.
const resp = await applyActionCode(auth, actionCode);
// Email address has been verified.

// TODO: Display a confirmation message to the user.
Expand All @@ -138,10 +142,10 @@ function handleVerifyEmail(auth, actionCode, continueUrl, lang) {
// TODO: If a continue URL is available, display a button which on
// click redirects the user back to the app via continueUrl with
// additional state determined from that URL's parameters.
}).catch((error) => {
} catch (error) {
// Code is invalid or expired. Ask the user to verify their email address
// again.
});
}
}
// [END auth_handle_verify_email]

23 changes: 11 additions & 12 deletions auth-next/custom.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]

function signInCustom() {
async function signInCustom() {
const token = "token123";

// [START auth_sign_in_custom]
const { getAuth, signInWithCustomToken } = require("firebase/auth");

const auth = getAuth();
signInWithCustomToken(auth, token)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ...
});
try {
const userCredential = await signInWithCustomToken(auth, token);
// Signed in
const user = userCredential.user;
// ...
} catch (error) {
const errorCode = error.code;
const errorMessage = error.message;
// ...
}
// [END auth_sign_in_custom]
}
Loading