aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatarLibravatar Biswakalyan Bhuyan <biswa@surgot.in> 2024-07-18 14:56:39 +0530
committerLibravatarLibravatar Biswakalyan Bhuyan <biswa@surgot.in> 2024-07-18 14:56:39 +0530
commit2f613682b733f8f03634df08270469830cad1800 (patch)
treebab1e0c7b19aa9538bbcb099d7e5b36c1f264528
parent05faaf231620ce8d4ee67585bd86f1e97fd32eeb (diff)
downloadadmin-panel-2f613682b733f8f03634df08270469830cad1800.tar.gz
admin-panel-2f613682b733f8f03634df08270469830cad1800.tar.bz2
admin-panel-2f613682b733f8f03634df08270469830cad1800.zip
added the config and setup the basic auth
-rw-r--r--.gitignore1
-rw-r--r--server/.env0
-rw-r--r--server/config/db.js14
-rw-r--r--server/models/User.js7
-rw-r--r--server/routes/auth.js61
-rw-r--r--server/server.js14
-rw-r--r--src/App.js30
-rw-r--r--src/components/Login.js34
-rw-r--r--src/firebase/firebaseConfig.js17
9 files changed, 160 insertions, 18 deletions
diff --git a/.gitignore b/.gitignore
index 4d29575..2b552a6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -14,6 +14,7 @@
# misc
.DS_Store
.env.local
+.env
.env.development.local
.env.test.local
.env.production.local
diff --git a/server/.env b/server/.env
deleted file mode 100644
index e69de29..0000000
--- a/server/.env
+++ /dev/null
diff --git a/server/config/db.js b/server/config/db.js
new file mode 100644
index 0000000..effbe5e
--- /dev/null
+++ b/server/config/db.js
@@ -0,0 +1,14 @@
+const mongoose = require('mongoose')
+const connecttDB = async () => {
+ try {
+ await mongoose.connect(process.env.MONGO_URI, {
+ useNewUrlParser: true,
+ useUnifiedTopology: true,
+ });
+ console.log('MongoDB Connected...');
+ } catch (err) {
+ console.error(err.message);
+ process.exit(1);
+ }
+};
+module.exports = connectDB;
diff --git a/server/models/User.js b/server/models/User.js
new file mode 100644
index 0000000..6773c64
--- /dev/null
+++ b/server/models/User.js
@@ -0,0 +1,7 @@
+const mongoose = require('mongoose');
+const UserSchema = new mongoose.Schema({
+ username: { type: String, reauired: true, unique: true },
+ password: { type: String, required: true },
+ role: { type: String, default: 'admin' },
+});
+module.exports = mongoose.model('User', UserSchema);
diff --git a/server/routes/auth.js b/server/routes/auth.js
new file mode 100644
index 0000000..3fb0986
--- /dev/null
+++ b/server/routes/auth.js
@@ -0,0 +1,61 @@
+const express = require('express');
+const bcrypt = require('bcryptjs');
+const jwt = require('jsonwebtoken');
+const User = require('../models/User');
+const router = express.Router();
+
+// Register
+router.post('/register', async (req, res) => {
+ const { username, password } = req.body;
+ try {
+ let user = await User.findOne({ username });
+ if (user) {
+ return res.status(400).json({ msg: 'User already exists' });
+ }
+ user = new User({
+ username,
+ password,
+ });
+ const salt = await bcrypt.genSalt(10);
+ user.password = await bcrypt.hash(password, salt);
+ await user.save();
+ res.status(200).send('User registered');
+ } catch (err) {
+ console.error(err.message);
+ res.status(500).send('Server error');
+ }
+});
+
+// Login
+router.post('/login', async (req, res) => {
+ const { username, password } = req.body;
+ try {
+ const user = await User.findOne({ username });
+ if (!user) {
+ return res.status(400).json({ msg: 'Invalid credentials' });
+ }
+ const isMatch = await bcrypt.compare(password, user.password);
+ if (!isMatch) {
+ return res.status(400).json({ msg: 'Invalid credentials' });
+ }
+ const payload = {
+ user: {
+ id: user.id,
+ },
+ };
+ jwt.sign(
+ payload,
+ process.env.JWT_SECRET,
+ { expiresIn: '1h' },
+ (err, token) => {
+ if (err) throw err;
+ res.json({ token });
+ }
+ );
+ } catch (err) {
+ console.error(err.message);
+ res.status(500).send('Server error');
+ }
+});
+
+module.exports = router;
diff --git a/server/server.js b/server/server.js
new file mode 100644
index 0000000..8836667
--- /dev/null
+++ b/server/server.js
@@ -0,0 +1,14 @@
+const express = require('express');
+const connectDB = require('./config/db');
+const cors = require('cors');
+const app = express();
+require('dotenv').config();
+
+connectDB();
+app.use(cors());
+app.use(express.json());
+
+app.use('/api/auth', require('./routes/auth'));
+
+const PORT = process.env.PORT || 5000;
+app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
diff --git a/src/App.js b/src/App.js
index 3784575..ffc907d 100644
--- a/src/App.js
+++ b/src/App.js
@@ -1,25 +1,19 @@
-import logo from './logo.svg';
-import './App.css';
+import React, { useState } from 'react';
+import Login from './components/Login';
+
+const App = () => {
+ const [isAuth, setIsAuth] = useState(false);
-function App() {
return (
<div className="App">
- <header className="App-header">
- <img src={logo} className="App-logo" alt="logo" />
- <p>
- Edit <code>src/App.js</code> and save to reload.
- </p>
- <a
- className="App-link"
- href="https://reactjs.org"
- target="_blank"
- rel="noopener noreferrer"
- >
- Learn React
- </a>
- </header>
+ {isAuth ? <AdminPanel /> : <Login setAuth={setIsAuth} />}
</div>
);
-}
+};
+
+const AdminPanel = () => {
+ // Admin panel logic
+ return <div>Admin Panel</div>;
+};
export default App;
diff --git a/src/components/Login.js b/src/components/Login.js
new file mode 100644
index 0000000..9a67051
--- /dev/null
+++ b/src/components/Login.js
@@ -0,0 +1,34 @@
+import React, { useState } from 'react';
+import axios from 'axios';
+
+const Login = ({ setAuth }) => {
+ const [formData, setFormData] = useState({
+ username: '',
+ password: '',
+ });
+
+ const { username, password } = formData;
+
+ const onChange = (e) => setFormData({ ...formData, [e.target.name]: e.target.value });
+
+ const onSubmit = async (e) => {
+ e.preventDefault();
+ try {
+ const res = await axios.post('/api/auth/login', formData);
+ localStorage.setItem('token', res.data.token);
+ setAuth(true);
+ } catch (err) {
+ console.error(err.response.data);
+ }
+ };
+
+ return (
+ <form onSubmit={onSubmit}>
+ <input type="text" name="username" value={username} onChange={onChange} required />
+ <input type="password" name="password" value={password} onChange={onChange} required />
+ <button type="submit">Login</button>
+ </form>
+ );
+};
+
+export default Login;
diff --git a/src/firebase/firebaseConfig.js b/src/firebase/firebaseConfig.js
new file mode 100644
index 0000000..12f2a35
--- /dev/null
+++ b/src/firebase/firebaseConfig.js
@@ -0,0 +1,17 @@
+import firebase from 'firebase/app';
+import 'firebase/storage';
+
+const firebaseConfig = {
+ apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
+ authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
+ projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
+ storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
+ messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
+ appId: process.env.REACT_APP_FIREBASE_APP_ID,
+};
+
+firebase.initializeApp(firebaseConfig);
+
+const storage = firebase.storage();
+
+export { storage, firebase as default };