개발자
html에서 아래 같이 data를 전송했을 때 백엔드에서 const {createUserWithEmailAndPassword,signInWithEmailAndPassword,signOut,browserSessionPersistence, setPersistence}= require("firebase/auth"); const db = require("../config/firestore"); const {email,password} = req.body; console.log(email,password) await signInWithEmailAndPassword(auth,email,password) res.redirect("/api/home") 로 받으면 첫 번째 로그인시 Cannot POST /auth/login가 뜨면서 로그인이 안되고 뒤로 갔다가 다시 로그인을 하면 로그인이 완료되는 오류가 발생합니다... firestore은 form 데이터를 받아 저장하는 것에 오류가 발생하지 않는데 왜 authentication에서는 이러 오류가 발생하는지 궁금합니다 ㅠㅠ 해결 방안도 궁금합니다 ! ㅠㅠ
1<form action="/api/auth/login" method="post">
2 <div id="id">
3 <input type="email" name="email" placeholder="이메일">
4 </div>
5 <div id="password">
6 <input type="password" name="password" placeholder="비밀번호">
7 </div>
8 <div id="login-button">
9 <button type="submit">로그인</button >
10 </div>
11</form>
답변 0
첫 답변을 기다리고 있어요.
지금 가입하면 모든 질문의 답변을 볼 수 있어요!
현직자들의 명쾌한 답변을 얻을 수 있어요.
이미 회원이신가요?
커리어리 AI 봇
GPT가 달아주는 답변입니다.
2023년 10월 24일
이 오류는 Firebase 인증 서비스가 비동기 실행되기 때문에 발생하는 것 같습니다. 첫 번째 로그인 요청에서는 Firebase가 아직 준비가 되지 않았을 수 있고, 이 때문에 사용자의 인증 요청은 실패합니다. 하지만 뒤로 가서 다시 로그인을 시도하면 이때는 이미 Firebase 서비스가 준비가 되어 있어 로그인이 성공적으로 이루어집니다. Firebase Authentication이 제대로 작동하게 하려면 Firebase 초기화 코드를 서버와 연결된 스크립트의 상위 레벨에 위치시키십시오. 그리고 `signInWithEmailAndPassword` 함수 앞에 async 키워드를 붙여 비동기 처리를 해주어야 합니다. 아래와 같이 수정해 보세요: ```javascript const { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, } = require("firebase/auth"); const db = require("../config/firestore"); // Define the Firebase function at the top level const auth = getAuth(); const login = async (req, res) => { const { email, password } = req.body; try { // use async-await to ensure the SignIn operation completes before moving further await signInWithEmailAndPassword(auth, email, password); res.redirect("/api/home"); } catch (error) { console.error(error); res.status(400).send("Cannot Post /auth/login"); } }; module.exports = login; ``` 위의 코드에서, 우선 auth 객체를 상위 레벨에서 정의합니다. 그리고 `signInWithEmailAndPassword` 함수 앞에 async-await를 사용하여 이 함수가 완전히 실행될 때까지 기다렸다가 다음 코드로 넘어갑니다. 이렇게 하면 Firebase 인증이 서버 시작과 동시에 초기화되며, 첫 번째 로그인 요청에서도 문제 없이 처리될 수 있을 것입니다.
지금 가입하면 모든 질문의 답변을 볼 수 있어요!