자바스크립트에서 커스텀 인증 시스템을 사용하여 Firebase에 인증
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
사용자가 정상적으로 로그인할 때 커스텀 서명 토큰을 발행하도록 인증 서버를 수정하면 Firebase Authentication에 커스텀 인증 시스템을 통합할 수 있습니다. 그러면 앱이 이 토큰을 받아 Firebase 인증에 사용합니다.
시작하기 전에
- 자바스크립트 프로젝트에 Firebase를 추가합니다.
- 다음과 같이 프로젝트의 서버 키를 가져옵니다.
- 프로젝트 설정의 서비스 계정
페이지로 이동합니다.
- 서비스 계정 페이지의 Firebase Admin SDK 섹션 하단에서
새 비공개 키 생성을 클릭합니다.
- 새 서비스 계정의 공개 키/비공개 키 쌍이 자동으로 컴퓨터에
저장됩니다. 이 파일을 인증 서버에 복사합니다.
Firebase에 인증
- 사용자가 앱에 로그인하면 사용자의 로그인 인증 정보(예: 사용자 이름과 비밀번호)를 인증 서버로 전송하세요. 서버가 사용자 인증 정보를 확인하여 정보가 유효하면 커스텀 토큰을 반환합니다.
- 인증 서버에서 커스텀 토큰을 받은 후 다음과 같이 이 토큰을
signInWithCustomToken
에 전달하여 사용자를 로그인 처리합니다.
Web
import { getAuth, signInWithCustomToken } from "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;
// ...
});
Web
firebase.auth().signInWithCustomToken(token)
.then((userCredential) => {
// Signed in
var user = userCredential.user;
// ...
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
// ...
});
다음 단계
사용자가 처음으로 로그인하면 신규 사용자 계정이 생성되고 사용자가 로그인할 때 사용한 사용자 인증 정보(사용자 이름과 비밀번호, 전화번호 또는 인증 제공업체 정보)에 연결됩니다. 이 신규 계정은 Firebase 프로젝트에 저장되며 사용자의 로그인 방법과 무관하게 프로젝트 내의 모든 앱에서 사용자 본인 확인에 사용할 수 있습니다.
-
앱에서 사용자의 인증 상태를 파악할 때 권장하는 방법은 Auth
객체에 관찰자를 설정하는 것입니다. 그러면 User
객체로부터 사용자의 기본 프로필 정보를 가져올 수 있습니다. 사용자 관리를 참조하세요.
Firebase Realtime Database와 Cloud Storage 보안 규칙의 auth
변수에서 로그인한 사용자의 고유 사용자 ID를 가져온 후 이 ID를 통해 사용자가 액세스할 수 있는 데이터를 관리할 수 있습니다.
인증 제공업체의 사용자 인증 정보를 기존 사용자 계정에 연결하면 사용자가 여러 인증 제공업체를 통해 앱에 로그인할 수 있습니다.
사용자를 로그아웃시키려면 signOut
을 호출합니다.
Web
import { getAuth, signOut } from "firebase/auth";
const auth = getAuth();
signOut(auth).then(() => {
// Sign-out successful.
}).catch((error) => {
// An error happened.
});
Web
firebase.auth().signOut().then(() => {
// Sign-out successful.
}).catch((error) => {
// An error happened.
});
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-08-08(UTC)
[null,null,["최종 업데이트: 2025-08-08(UTC)"],[],[],null,["You can integrate Firebase Authentication with a custom authentication system by\nmodifying your authentication server to produce custom signed tokens when a user\nsuccessfully signs in. Your app receives this token and uses it to authenticate\nwith Firebase.\n\nBefore you begin\n\n1. [Add Firebase to your JavaScript project](/docs/web/setup).\n2. Get your project's server keys:\n 1. Go to the [Service Accounts](https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk) page in your project's settings.\n 2. Click *Generate New Private Key* at the bottom of the *Firebase Admin SDK* section of the *Service Accounts* page.\n 3. The new service account's public/private key pair is automatically saved on your computer. Copy this file to your authentication server.\n\nAuthenticate with Firebase\n\n1. When users sign in to your app, send their sign-in credentials (for example, their username and password) to your authentication server. Your server checks the credentials and returns a [custom\n token](/docs/auth/admin/create-custom-tokens) if they are valid.\n2. After you receive the custom token from your authentication server, pass it to `signInWithCustomToken` to sign in the user: \n\n Web \n\n ```javascript\n import { getAuth, signInWithCustomToken } from \"firebase/auth\";\n\n const auth = getAuth();\n signInWithCustomToken(auth, token)\n .then((userCredential) =\u003e {\n // Signed in\n const user = userCredential.user;\n // ...\n })\n .catch((error) =\u003e {\n const errorCode = error.code;\n const errorMessage = error.message;\n // ...\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/auth-next/custom/auth_sign_in_custom.js#L8-L21\n ```\n\n Web \n\n ```javascript\n firebase.auth().signInWithCustomToken(token)\n .then((userCredential) =\u003e {\n // Signed in\n var user = userCredential.user;\n // ...\n })\n .catch((error) =\u003e {\n var errorCode = error.code;\n var errorMessage = error.message;\n // ...\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/auth/custom.js#L10-L20\n ```\n\nNext steps\n\nAfter a user signs in for the first time, a new user account is created and\nlinked to the credentials---that is, the user name and password, phone\nnumber, or auth provider information---the user signed in with. This new\naccount is stored as part of your Firebase project, and can be used to identify\na user across every app in your project, regardless of how the user signs in.\n\n- In your apps, the recommended way to know the auth status of your user is to\n set an observer on the `Auth` object. You can then get the user's\n basic profile information from the `User` object. See\n [Manage Users](/docs/auth/web/manage-users).\n\n- In your Firebase Realtime Database and Cloud Storage\n [Security Rules](/docs/database/security/user-security), you can\n get the signed-in user's unique user ID from the `auth` variable,\n and use it to control what data a user can access.\n\nYou can allow users to sign in to your app using multiple authentication\nproviders by [linking auth provider credentials to an\nexisting user account.](/docs/auth/web/account-linking)\n\nTo sign out a user, call [`signOut`](/docs/reference/js/auth#signout): \n\nWeb \n\n```javascript\nimport { getAuth, signOut } from \"firebase/auth\";\n\nconst auth = getAuth();\nsignOut(auth).then(() =\u003e {\n // Sign-out successful.\n}).catch((error) =\u003e {\n // An error happened.\n});https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/auth-next/index/auth_sign_out.js#L8-L15\n```\n\nWeb \n\n```javascript\nfirebase.auth().signOut().then(() =\u003e {\n // Sign-out successful.\n}).catch((error) =\u003e {\n // An error happened.\n});https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/auth/index.js#L33-L37\n```"]]