Skip to content

Commit

Permalink
auth-done
Browse files Browse the repository at this point in the history
  • Loading branch information
Giri-Spheron committed Oct 25, 2023
0 parents commit 4e249bc
Show file tree
Hide file tree
Showing 32 changed files with 22,074 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
2 changes: 2 additions & 0 deletions Backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
3 changes: 3 additions & 0 deletions Backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

node_modules
.env
7 changes: 7 additions & 0 deletions Backend/Routes/userRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const express=require('express');
const Router=express.Router();
const userService=require('../Services/userService');

Router.post('/login',userService.userLogin)
Router.post('/register',userService.userSignup)
module.exports=Router
77 changes: 77 additions & 0 deletions Backend/Services/userService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const db = require("../config/dbConfig");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");

const securityKey = "12345@Ra";
const tableName = "user_information_80001_7788"; // This is your specific table name

const userLogin = async (req, res) => {
const { email, password } = req.body;

const { results } = await db
.prepare(`SELECT * FROM ${tableName} WHERE email = ?;`)
.bind(email)
.all();

if (results.length === 0) {
res.json({
status: 400,
message: "user not matched",
});
} else {
if (await bcrypt.compare(password, results[0].password)) {
const auth = jwt.sign({ data: results[0] }, securityKey);
res.json({
status: 200,
message: "Login success",
token: auth,
});
} else {
res.json({
status: 400,
message: "Please check the password",
});
}
}
};

const userSignup = async (req, res) => {
const { username, email, password } = req.body;
const salt = await bcrypt.genSalt(10);
const hashpwd = await bcrypt.hash(password, salt);

const { results } = await db
.prepare(`SELECT * FROM ${tableName} WHERE email = ?;`)
.bind(email)
.all();

if (results.length > 0) {
res.json({
status: 400,
message: "User already exists",
});
} else {
try {
const { meta: insert } = await db
.prepare(
`INSERT INTO ${tableName} (username, email, password) VALUES (?, ?, ?);`
)
.bind(username, email, hashpwd)
.run();

await insert.txn.wait();

res.json({
status: 200,
message: `Successfully registered ${username}`,
});
} catch (error) {
res.json({
status: 400,
message: error,
});
}
}
};

module.exports = { userSignup, userLogin };
18 changes: 18 additions & 0 deletions Backend/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const express = require("express");
const env = require("dotenv").config();
const bodyparser = require("body-parser");
const cors = require("cors");

const app = express();
const user = require("./Routes/userRouter");

// Middleware configuration
app.use(bodyparser.json());
app.use(cors());
app.use("/user", user);

const PORT = process.env.PORT || 4000; // Corrected the fallback to default value for PORT

app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
21 changes: 21 additions & 0 deletions Backend/config/dbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const env = require("dotenv").config();


const ethers = require("ethers");
const tableland = require("@tableland/sdk");

// Set up the provider and wallet using your private key.
let provider = new ethers.providers.JsonRpcProvider(
"https://capable-winter-paper.matic-testnet.quiknode.pro/152107c19beb0243104885340d25c00808394af7/"
);
let privateKey =
"0x522c6ab985d1f22f3bf1f918ef1e19cd52e986fa91bfe8c18208d20a00ecbbde"; // Your private key
let wallet = new ethers.Wallet(privateKey, provider);

// Connect the wallet to the provider.
let signer = wallet.connect(provider);

// Create the Tableland database instance using the signer.
const db = new tableland.Database({ signer });

module.exports = db;
14 changes: 14 additions & 0 deletions Backend/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:18
# Create app directory
WORKDIR /d/Work/Spheron/loginpage/Backend
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --omit=dev
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "node", "app.js" ]
Loading

0 comments on commit 4e249bc

Please sign in to comment.