멀티테넌시로 인증
이 문서에서는 멀티 테넌트 Identity Platform 환경에서 사용자를 인증하는 방법을 설명합니다.
시작하기 전에
프로젝트에 멀티테넌시를 사용 설정하고 테넌트를 구성해야 합니다. 자세한 방법은 멀티테넌시 시작하기를 참조하세요.
또한 앱에 클라이언트 SDK를 추가해야 합니다.
콘솔에서 Identity Platform 페이지로 이동합니다. Google Cloud
Identity Platform 사용자 페이지로 이동오른쪽 상단에서 애플리케이션 설정 세부정보를 클릭합니다.
코드를 웹 앱에 복사합니다. 예를 들면 다음과 같습니다.
웹 버전 9
import { initializeApp } from "firebase/app"; const firebaseConfig = { apiKey: "...", // By default, authDomain is '[YOUR_APP].firebaseapp.com'. // You may replace it with a custom domain. authDomain: '[YOUR_CUSTOM_DOMAIN]' }; const firebaseApp = initializeApp(firebaseConfig);
웹 버전 8
firebase.initializeApp({ apiKey: '...', // By default, authDomain is '[YOUR_APP].firebaseapp.com'. // You may replace it with a custom domain. authDomain: '[YOUR_CUSTOM_DOMAIN]' });
테넌트로 로그인
테넌트에 로그인하려면 테넌트 ID를 auth 객체에 전달해야 합니다.
tenantId는 페이지를 새로고침하면 사라집니다.
웹 버전 9
import { getAuth } from "firebase/auth"; const auth = getAuth(); const tenantId = "TENANT_ID1"; auth.tenantId = tenantId;
웹 버전 8
const tenantId = "TENANT_ID1"; firebase.auth().tenantId = tenantId;
이 auth 인스턴스의 향후 로그인 요청에는 테넌트 ID를 변경하거나 재설정할 때까지 테넌트 ID(앞의 예시에서 TENANT_ID1)가 포함됩니다.
하나 또는 여러 개의 auth 인스턴스를 사용하여 여러 테넌트로 작업할 수 있습니다.
단일 auth 인스턴스를 사용하려면 테넌트 간에 전환할 때마다 tenantId 속성을 수정합니다. 프로젝트 수준의 IdP로 되돌리려면 tenantId를 null로 설정합니다.
웹 버전 9
// One Auth instance // Switch to tenant1 auth.tenantId = "TENANT_ID1"; // Switch to tenant2 auth.tenantId = "TENANT_ID2"; // Switch back to project level IdPs auth.tenantId = null;
웹 버전 8
// One Auth instance // Switch to tenant1 firebase.auth().tenantId = "TENANT_ID1"; // Switch to tenant2 firebase.auth().tenantId = "TENANT_ID2"; // Switch back to project level IdPs firebase.auth().tenantId = null;
여러 인스턴스를 사용하려면 각 테넌트의 새 auth 인스턴스를 만들고 다른 ID를 할당합니다.
웹 버전 9
// Multiple Auth instances import { initializeApp } from "firebase/app"; import { getAuth } from "firebase/auth"; const firebaseApp1 = initializeApp(firebaseConfig1, 'app1_for_tenantId1'); const firebaseApp2 = initializeApp(firebaseConfig2, 'app2_for_tenantId2'); const auth1 = getAuth(firebaseApp1); const auth2 = getAuth(firebaseApp2); auth1.tenantId = "TENANT_ID1"; auth2.tenantId = "TENANT_ID2";
웹 버전 8
// Multiple Auth instances firebase.initializeApp(config, 'app1_for_tenantId1'); firebase.initializeApp(config, 'app2_for_tenantId2'); const auth1 = firebase.app('app1').auth(); const auth2 = firebase.app('app2').auth(); auth1.tenantId = "TENANT_ID1"; auth2.tenantId = "TENANT_ID2";
테넌트로 로그인하면 user.tenantId가 해당 테넌트로 설정된 테넌트 사용자가 반환됩니다. 나중에 auth 인스턴스에서 tenantId를 전환해도 currentUser 속성은 변경되지 않으며 여전히 이전 테넌트와 동일한 사용자를 가리킵니다.
웹 버전 9
import { signInWithEmailAndPassword, onAuthStateChanged } from "firebase/auth"; // Switch to TENANT_ID1 auth.tenantId = 'TENANT_ID1'; // Sign in with tenant signInWithEmailAndPassword(auth, email, password) .then((userCredential) => { // User is signed in. const user = userCredential.user; // user.tenantId is set to 'TENANT_ID1'. // Switch to 'TENANT_ID2'. auth.tenantId = 'TENANT_ID2'; // auth.currentUser still points to the user. // auth.currentUser.tenantId is 'TENANT_ID1'. }); // You could also get the current user from Auth state observer. onAuthStateChanged(auth, (user) => { if (user) { // User is signed in. // user.tenantId is set to 'TENANT_ID1'. } else { // No user is signed in. } });
웹 버전 8
// Switch to TENANT_ID1 firebase.auth().tenantId = 'TENANT_ID1'; // Sign in with tenant firebase.auth().signInWithEmailAndPassword(email, password) .then((result) => { const user = result.user; // user.tenantId is set to 'TENANT_ID1'. // Switch to 'TENANT_ID2'. firebase.auth().tenantId = 'TENANT_ID2'; // firebase.auth().currentUser still point to the user. // firebase.auth().currentUser.tenantId is 'TENANT_ID1'. }); // You could also get the current user from Auth state observer. firebase.auth().onAuthStateChanged((user) => { if (user) { // User is signed in. // user.tenantId is set to 'TENANT_ID1'. } else { // No user is signed in. } });
이메일/비밀번호 계정
다음 예시에서는 새 사용자를 등록하는 방법을 보여줍니다.
웹 버전 9
import { createUserWithEmailAndPassword } from "firebase/auth"; auth.tenantId = 'TENANT_ID'; createUserWithEmailAndPassword(auth, email, password) .then((userCredential) => { // User is signed in. // userCredential.user.tenantId is 'TENANT_ID'. }).catch((error) => { // Handle / display error. // ... });
웹 버전 8
firebase.auth().tenantId