firebase auth/admin"/>
firebase auth/admin
我正在尝试对节点使用 firebase 身份验证。
这是我的 index.js
const express = require('express');
const { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut } = require('firebase/auth');
const authRouter = express.Router();
const auth = getAuth();
authRouter.get('/', (req, res) => {
// open auth/views/main.html
res.render('main.ejs', { root: './views' });
});
authRouter.post('/signup', async (req, res) => {
const email = req.body.email;
const password = req.body.password;
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
const user = userCredential.user;
res.status(200).json({ message: '회원가입 성공', user });
} catch (error) {
const errorCode = error.code;
const errorMessage = error.message;
res.status(400).json({ message: '회원가입 실패', errorCode, errorMessage });
}
});
authRouter.post('/login', async (req, res) => {
const email = req.body.email;
const password = req.body.password;
try {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
const user = userCredential.user;
res.status(200).json({ message: '로그인 성공', user });
} catch (error) {
const errorCode = error.code;
const errorMessage = error.message;
res.status(400).json({ message: '로그인 실패', errorCode, errorMessage });
}
});
authRouter.post('/logout', async (req, res) => {
try {
await signOut(auth);
res.status(200).json({ message: '로그아웃 성공' });
} catch (error) {
const errorCode = error.code;
const errorMessage = error.message;
res.status(400).json({ message: '로그아웃 실패', errorCode, errorMessage });
}
});
module.exports = authRouter;
server.js
const express = require('express');
const bodyParser = require('body-parser');
const { getAuth } = require('firebase/auth');
const { initializeApp } = require('firebase/app');
require('dotenv').config({ path: '.env' });
const firebaseConfig = {
apiKey: process.env.API_KEY,
authDomain: process.env.AUTH_DOMAIN,
projectId: process.env.PROJECT_ID,
storageBucket: process.env.STORAGE_BUCKET,
messagingSenderId: process.env.MESSAGING_SENDER_ID,
appId: process.env.APP_ID,
measurementId: process.env.MEASUREMENT_ID,
};
const app = express(); // create an instance of express
const firebaseApp = initializeApp(firebaseConfig);
const auth = getAuth(firebaseApp);
app.use(bodyParser.json()); // use the body-parser middleware
const authRoutes = require('./auth/index');
app.use('/auth', authRoutes);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
main.ejs
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="/auth/signup" method="POST">
<h1>회원가입</h1>
<div> email : <input type="email" name="email" id="signUpEmail" /> </div>
<div> password : <input type="password" name="password" id="signUpPassword" /> </div>
<button type="submit" id="signUpButton" >회원가입 하기</button>
</form>
</body>
</html>
我尝试注册,但它显示了管理员限制操作错误。
当我使用邮递员时效果很好。但如果我在网络上尝试,它会给我错误。
请帮助我,,
我尝试注册,但它显示了管理员限制操作错误。
当我使用邮递员时效果很好。但如果我在网络上尝试,它会给我错误。
回答如下:更多推荐
firebase auth/admin
发布评论