<head>태그의 초기화 스니펫 밑에 다음 스크립트와 CSS 파일을 포함한다.<script src="https://www.gstatic.com/firebasejs/ui/6.0.1/firebase-ui-auth.js"></script>
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/6.0.1/firebase-ui-auth.css" />
b. npm 모듈
다음 명령어를 사용하여 npm/yarn을 통해 FirebaseUI와 종속 항목을 설치한다.
$npm install firebaseui --save
소스 파일 내에서 다음 모듈을 require 처리 한다.
let firebase = require('firebase')
let firebase = require('firebaseui')
c. bower 구성요소
다음 명령어를 사용하여 Bower를 통해 FirebaseUI와 종속 항목을 설치합니다.
$bower install firebaseui --save
HTTP 서버가 bower_compenents/ 안의 파일을 제공하는 경우 필요한 파일을 HTML에 포함한다.
<script src="bower_components/firebaseui/dist/firebaseui.js"></script>
<link type="text/css" rel="stylesheet" href="bower_components/firebaseui/dist/firebaseui.css" />
<script>
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Firebase Authentication and get a reference to the service
const auth = getAuth(app);
</script>
신규 사용자가 자신의 이메일 주소와 비밀번호를 사용해 앱에 사입할 수 있는 양식을 만듦.
사용자가 양식을 작성하면, 사용자가 입려한 이메일 주소와 비밀번호의 유효성을 검사한 후, createUserWithEmailAndPassword 메서드에 전달.
<script>
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ..
});
</script>
기존 사용자가 자신의 이메일 주소와 비밀번호를 사용해 로그인할 수 있는 양식을 만듦.
사용자가 양식을 작성하면, signInWithEmailAndPassword 메서드를 호출.
<script>
import { getAuth, signInWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
});
</script>
로그인한 사용자에 대한 정보가 필요한 앱 페이지마다 전역 인증 객체에 관찰자를 연결.
사용자의 로그인 상태가 변경될 때마다 이 관찰자가 호출됨.
onAuthStateChanged 메서드를 사용해 관찰자를 연결.
사용자가 로그인 되면 관찰자에서 사용자에 대한 정보를 가져올 수 있음.
<script>
import { getAuth, onAuthStateChanged } from "firebase/auth";
const auth = getAuth();
onAuthStateChanged(auth, (user) => {
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/auth.user
const uid = user.uid;
// ...
} else {
// User is signed out
// ...
}
});
</script>