כניסה של משתמש באמצעות כתובת אימייל באמצעות Identity Platform
איך משתמשים ב-Identity Platform כדי להכניס משתמש באמצעות כתובת אימייל וסיסמה.
לחצו על תראו לי איך כדי לקרוא הסבר מפורט על המשימה ישירות במסוף Google Cloud :
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloud ליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Make sure that you have the following role or roles on the project: Identity Platform Admin, Service Usage Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Make sure that you have the following role or roles on the project: Identity Platform Admin, Service Usage Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
הפעלת Identity Platform
נכנסים לדף Identity Platform ב-Cloud Marketplace במסוף Google Cloud .
לוחצים על הפעלת Identity Platform.
הגדרת כניסה באמצעות אימייל
עוברים לדף ספקי זהויות.
בדף ספקי זהויות, לוחצים על הוספת ספק.
ברשימה Select a provider, בוחרים באפשרות Email/Password.
מעבירים את המתג מופעל למצב פעיל.
כדי לשמור את הגדרות הספק, לוחצים על שמירה.
יצירת משתמש
נכנסים לדף Users במסוף Google Cloud .
לוחצים על הוספת משתמש.
בשדה אימייל, מזינים כתובת אימייל וסיסמה. חשוב לשים לב לשני הערכים האלה כי תצטרכו אותם בשלב מאוחר יותר.
כדי להוסיף את המשתמש, לוחצים על הוספה. המשתמש החדש מופיע בדף משתמשים.
כניסה לחשבון המשתמש
השלבים לכניסה של המשתמש משתנים בהתאם לגרסת ה-SDK שבה האפליקציה משתמשת.
חשוב לוודא שפעלתם לפי השלבים הנכונים שמתאימים לאפליקציה שלכם.
SDK v9 (מודולרי)
התקנה של SDK והפעלת Firebase
בגרסה 9 של Firebase JS SDK נעשה שימוש בפורמט של מודול JavaScript.
בתהליך העבודה הזה נעשה שימוש ב-npm, ונדרשים כלי חבילה של מודולים או כלי JavaScript framework, כי SDK בגרסה 9 מותאם לעבודה עם כלי חבילה של מודולים כדי להסיר קוד שלא נמצא בשימוש (tree-shaking) ולהקטין את גודל ה-SDK.
כדי להשתמש ב-SDK בגרסה 9:
מתוך ספריית הפרויקט, מתקינים את Firebase באמצעות npm:
npm install firebase
מפעילים את Firebase באפליקציה ויוצרים אובייקט Firebase App:
import { initializeApp } from 'firebase/app'; const firebaseConfig = { apiKey: "API_KEY", authDomain: "AUTH_DOMAIN" }; const app = initializeApp(firebaseConfig);
מחליפים את מה שכתוב בשדות הבאים:
API_KEY: מזהה הפרויקט ב-Firebase.apiKey
AUTH_DOMAIN: מזההauthDomainהפרויקט ב-Firebase.
אפשר למצוא את הערכים האלה בהגדרות של פרויקט Firebase של האפליקציה.
אפליקציית Firebase היא אובייקט דמוי מאגר שבו מאוחסנת תצורה משותפת, ומתבצע אימות משותף בשירותי Firebase. אחרי שמאתחלים אובייקט של Firebase App בקוד, אפשר להוסיף שירותים של Firebase ולהתחיל להשתמש בהם.
גישה ל-Identity Platform ב-JavaScript
אחרי שמפעילים את Firebase SDK, אפשר להשתמש בו בכל מקום באפליקציה. לדוגמה, הנה אפליקציה שמנסה להכניס משתמש שמוגדר בה קשיח ולהציג את התוצאה בדף אינטרנט.
import { initializeApp } from 'firebase/app';
import {
onAuthStateChanged,
signInWithEmailAndPassword,
getAuth
} from 'firebase/auth';
const firebaseConfig = {
apiKey: "API_KEY",
authDomain: "AUTH_DOMAIN"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app, {/* extra options */ });
document.addEventListener("DOMContentLoaded", () => {
onAuthStateChanged(auth, (user) => {
if (user) {
document.getElementById("message").innerHTML = "Welcome, " + user.email;
}
else {
document.getElementById("message").innerHTML = "No user signed in.";
}
});
signIn();
});
function signIn() {
const email = "EMAIL_ID";
const password = "PASSWORD";
signInWithEmailAndPassword(auth, email, password)
.catch((error) => {
document.getElementById("message").innerHTML = error.message;
});
}
מחליפים את מה שכתוב בשדות הבאים:
-
API_KEY: מזהה הפרויקט ב-Firebase.apiKey -
AUTH_DOMAIN: מזהה הפרויקט ב-Firebase.authDomain -
EMAIL_ID: כתובת האימייל של המשתמש שיצרתם קודם במדריך הזה. -
PASSWORD: הסיסמה של המשתמש שיצרתם קודם במדריך הזה.
שימוש בכלי לאיגוד מודולים כדי להקטין את הגודל
Firebase Web SDK מיועד לעבודה עם חבילות מודולים כדי להסיר קוד שלא נמצא בשימוש (tree-shaking). מומלץ מאוד להשתמש בגישה הזו באפליקציות שמוכנות להפצה. כלים כמו Angular CLI, Next.js, Vue CLI או Create React App מטפלים אוטומטית באריזת מודולים של ספריות שמותקנות דרך npm ומיובאות אל מאגר ה-codebase.
לדוגמה, אפשר להשתמש ב-Webpack כדי ליצור תיקייה בשם dist שמכילה את האפליקציה המאוגדת ואת קוד התלות. מידע נוסף זמין במאמר בנושא שימוש בחבילות מודולים עם Firebase.
ייבוא של JavaScript לדף
יוצרים קובץ חדש בשם
index.html.מוסיפים שני קונטיינרים בסיסיים של HTML ומייבאים את ה-JS בחבילה.
<script defer src="js/bundled.js"></script> <div>Identity Platform Quickstart</div> <div id="message">Loading...</div>מפעילים את
index.htmlבדפדפן האינטרנט. תוצג הודעת פתיחה עם כתובת האימייל של המשתמש.
SDK v8 (מהדור הקודם)
יצירת דף אינטרנט
במחשב המקומי, יוצרים קובץ חדש בשם
index.html.בקובץ ה-HTML, מוסיפים שני מאגרי HTML:
<div>Identity Platform Quickstart</div> <div id="message">Loading...</div>
אתחול של Identity Platform client SDK עם מפתח ה-API
נכנסים לדף Identity Providers במסוף Google Cloud .
לוחצים על
פרטי הגדרת האפליקציה .מעתיקים את קוד האתחול אל
index.html.
כניסה לחשבון המשתמש
כדי להוסיף את המשתמש, מעתיקים את הקוד הבא אל
index.html:<script> var email = "EMAIL_ID"; var password = "PASSWORD"; firebase.auth().onAuthStateChanged(function(user) { if (user) { document.getElementById("message").innerHTML = "Welcome, " + user.email; } else { document.getElementById("message").innerHTML = "No user signed in."; } }); firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) { document.getElementById("message").innerHTML = error.message; }); </script>הקוד הזה קורא ל-
signInWithEmailAndPassword()ואז מעבד את התוצאה באמצעות הקריאה החוזרתonAuthStateChanged().מחליפים את מה שכתוב בשדות הבאים:
- EMAIL_ID: כתובת האימייל של המשתמש שיצרתם קודם
- PASSWORD: הסיסמה של המשתמש שיצרתם קודם
פותחים את
index.htmlבדפדפן האינטרנט. תופיע הודעת פתיחה עם כתובת האימייל של המשתמש.
מידע נוסף על מגבלות אחרות ב-Identity Platform זמין במאמר מכסות ומגבלות.
הסרת המשאבים
כדי לא לצבור חיובים לחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה:
מחיקת הספק והמשתמש
אם השתמשתם בפרויקט קיים Google Cloud , מחקו את הספק ואת המשתמש שיצרתם כדי שלא יחויב החשבון שלכם:
נכנסים לדף Identity Providers במסוף Google Cloud .
כדי למחוק את הספק, לוחצים על מחיקה לצד שם הספק. כדי לאשר, לוחצים על מחיקה.
נכנסים לדף Users במסוף Google Cloud .
כדי למחוק את המשתמש שיצרתם, לוחצים על מחיקה לצד שם המשתמש. כדי לאשר, לוחצים על מחיקה.
מחיקת הפרויקט
הדרך הקלה ביותר לבטל את החיוב היא למחוק את הפרויקט שיצרתם בשביל המדריך הזה.
כדי למחוק את הפרויקט:
- במסוף Google Cloud , נכנסים לדף Manage resources.
- ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
- כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.
המאמרים הבאים
באפליקציה אמיתית, המשתמשים נרשמים באמצעות דף הרשמה ייעודי, ואז נכנסים לחשבון על ידי הזנת כתובות האימייל והסיסמאות שלהם. Identity Platform מציע ממשק משתמש מוכן מראש לאימות שאפשר להשתמש בו בדפים האלה, או שאפשר ליצור ממשק משתמש משלכם. אולי תרצו גם לתמוך בשיטות כניסה נוספות, כמו ספקי רשתות חברתיות (כמו פייסבוק או Google), מספרי טלפון, OIDC או SAML.
מידע נוסף על:
כניסה של משתמשים באמצעות JavaScript, Android, iOS, C++ או Unity.
העברת משתמשים מהאפליקציה הקיימת ל-Identity Platform.