added changePassword, checkUserExist, insertUser & message functions
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 168 KiB |
@@ -1,3 +1,4 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
|
||||
@@ -13,9 +13,10 @@ export default function Login() {
|
||||
const navigate = useNavigate();
|
||||
const onSubmit: SubmitHandler<Inputs> = (data) => {
|
||||
axios
|
||||
.post("http://localhost:3000/api/login", { data })
|
||||
.post("http://localhost:5173/api/auth/login", { data })
|
||||
.then(() => {
|
||||
navigate("/lost");
|
||||
console.log("logged in");
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
@@ -32,11 +33,11 @@ export default function Login() {
|
||||
</div>
|
||||
|
||||
<div className="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-6">
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-5">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="username"
|
||||
className="text-white block text-sm font-medium leading-6"
|
||||
className="text-white block text-sm leading-6"
|
||||
>
|
||||
Username
|
||||
</label>
|
||||
@@ -61,7 +62,7 @@ export default function Login() {
|
||||
<div className="flex items-center justify-between">
|
||||
<label
|
||||
htmlFor="password"
|
||||
className="text-white block text-sm font-medium leading-6 "
|
||||
className="text-white block text-sm leading-6 "
|
||||
>
|
||||
Password
|
||||
</label>
|
||||
|
||||
@@ -15,14 +15,13 @@ export default function Signup() {
|
||||
handleSubmit,
|
||||
formState: { errors },
|
||||
} = useForm<Inputs>();
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
const onSubmit: SubmitHandler<Inputs> = (data) => {
|
||||
axios
|
||||
.post("http://localhost:3000/api/signup", { data })
|
||||
.post("http://localhost:5173/api/auth/signup", data)
|
||||
.then(() => {
|
||||
navigate("/chat");
|
||||
console.log("Signed up");
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
@@ -40,11 +39,11 @@ export default function Signup() {
|
||||
</div>
|
||||
|
||||
<div className="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-6">
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-5">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="username"
|
||||
className="text-white text-sm font-medium leading-6"
|
||||
className="text-white text-sm leading-6"
|
||||
>
|
||||
Username
|
||||
</label>
|
||||
@@ -62,17 +61,23 @@ export default function Signup() {
|
||||
autoComplete="username"
|
||||
className="pl-2 block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
|
||||
/>
|
||||
{errors?.username?.type === "maxLength" && (
|
||||
<p>First name cannot exceed 20 characters</p>
|
||||
)}
|
||||
{errors?.username?.type === "minLength" && (
|
||||
<p>Username must be between 4 and 20 characters</p>
|
||||
)}
|
||||
{errors?.username?.type === "pattern" && (
|
||||
<p>
|
||||
Username can only contain letters, numbers and underscores
|
||||
</p>
|
||||
)}
|
||||
<div>
|
||||
{errors?.username?.type === "maxLength" && (
|
||||
<p className="text-red-400 text-sm">
|
||||
First name cannot exceed 20 characters
|
||||
</p>
|
||||
)}
|
||||
{errors?.username?.type === "minLength" && (
|
||||
<p className="text-red-400 text-sm">
|
||||
Username must be between 4 and 20 characters
|
||||
</p>
|
||||
)}
|
||||
{errors?.username?.type === "pattern" && (
|
||||
<p className="text-red-400 text-sm">
|
||||
Username can only contain letters, numbers and underscores
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -80,7 +85,7 @@ export default function Signup() {
|
||||
<div className="flex items-center justify-between">
|
||||
<label
|
||||
htmlFor="password"
|
||||
className="text-white text-sm font-medium leading-6"
|
||||
className="text-white text-sm leading-6"
|
||||
>
|
||||
Password
|
||||
</label>
|
||||
@@ -98,13 +103,17 @@ export default function Signup() {
|
||||
className="pl-2 block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
|
||||
/>
|
||||
{errors?.password?.type === "maxLength" && (
|
||||
<p>First name cannot exceed 20 characters</p>
|
||||
<p className="text-red-400 text-sm">
|
||||
First name cannot exceed 20 characters
|
||||
</p>
|
||||
)}
|
||||
{errors?.password?.type === "minLength" && (
|
||||
<p>Username must be between 4 and 20 characters</p>
|
||||
<p className="text-red-400 text-sm">
|
||||
Username must be between 4 and 20 characters
|
||||
</p>
|
||||
)}
|
||||
{errors?.password?.type === "pattern" && (
|
||||
<p>
|
||||
<p className="text-red-400 text-sm">
|
||||
Username can only contain letters, numbers and underscores
|
||||
</p>
|
||||
)}
|
||||
@@ -114,7 +123,7 @@ export default function Signup() {
|
||||
<div className="flex items-center justify-between">
|
||||
<label
|
||||
htmlFor="password"
|
||||
className="text-white text-sm font-medium leading-6"
|
||||
className="text-white text-sm leading-6"
|
||||
>
|
||||
Repeat password
|
||||
</label>
|
||||
@@ -146,7 +155,7 @@ export default function Signup() {
|
||||
type="submit"
|
||||
className="text-black w-full justify-center rounded-md bg-green-600 px-3 py-1.5 text-sm font-semibold leading-6 shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
|
||||
>
|
||||
Sign in
|
||||
Sign up
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
SERVER_PORT=3000
|
||||
JWT_SECRET=jklsdfHJKDFJKLDF@
|
||||
|
||||
PG_USER=postgres
|
||||
PG_PASSWORD=haslo
|
||||
|
||||
10
server/auth/jwt.js
Normal file
10
server/auth/jwt.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const jwt = require("jsonwebtoken");
|
||||
const jwtSecret = process.env.JWT_SECRET;
|
||||
|
||||
function generateJwtToken(username) {
|
||||
jwt.sign(username, jwtSecret, { expiresIn: "30d" });
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
generateJwtToken,
|
||||
};
|
||||
@@ -25,12 +25,13 @@ client
|
||||
),
|
||||
);
|
||||
|
||||
// Creating database tables
|
||||
async function createTables() {
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS accounts (
|
||||
username VARCHAR(20),
|
||||
password VARCHAR(128),
|
||||
id VARCHAR(128),
|
||||
user_id VARCHAR(128),
|
||||
created_at VARCHAR(100)
|
||||
);
|
||||
`);
|
||||
@@ -39,11 +40,51 @@ async function createTables() {
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
sender_id VARCHAR(128),
|
||||
receiver_id VARCHAR(128),
|
||||
messages VARCHAR(500),
|
||||
created_at VARCHAR(100)
|
||||
message VARCHAR(500),
|
||||
created_at VARCHAR(100),
|
||||
message_id SERIAL PRIMARY KEY
|
||||
);
|
||||
`);
|
||||
}
|
||||
|
||||
async function insertUser(username, password, user_id, created_at) {
|
||||
const query = `
|
||||
INSERT INTO accounts (username, password, user_id, created_at)
|
||||
VALUES ($1, $2, $3, $4);
|
||||
`;
|
||||
client.query(query, [username, password, user_id, created_at]);
|
||||
}
|
||||
|
||||
async function insertMessage(sender_id, receiver_id, message) {
|
||||
const query = `
|
||||
INSERT INTO messages (sender_id, receiver_id, message)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id;
|
||||
`;
|
||||
client.query(query, [sender_id, receiver_id, message]);
|
||||
}
|
||||
|
||||
async function checkUserExist(username) {
|
||||
const query = `
|
||||
SELECT COUNT(*) FROM accounts
|
||||
WHERE username = $1;
|
||||
`;
|
||||
const result = await client.query(query, [username]);
|
||||
return result.rows[0].count > 0;
|
||||
}
|
||||
|
||||
async function changePassword(username, newPassword) {
|
||||
const query = `
|
||||
UPDATE accounts
|
||||
SET password = $1
|
||||
WHERE username = $2;
|
||||
`;
|
||||
client.query(query, [newPassword, username]);
|
||||
}
|
||||
module.exports = {
|
||||
client,
|
||||
insertUser,
|
||||
insertMessage,
|
||||
checkUserExist,
|
||||
changePassword,
|
||||
};
|
||||
|
||||
@@ -2,27 +2,40 @@ const express = require("express");
|
||||
const { createServer } = require("http");
|
||||
const { Server } = require("socket.io");
|
||||
const app = express();
|
||||
const cors = require("cors");
|
||||
const server = createServer(app);
|
||||
const io = new Server(server);
|
||||
require("dotenv").config();
|
||||
const PORT = process.env.SERVER_PORT;
|
||||
const {
|
||||
initializeDatabaseConnection,
|
||||
client,
|
||||
insertUser,
|
||||
insertMessage,
|
||||
checkUserExist,
|
||||
changePassword,
|
||||
} = require("./db/db.js");
|
||||
|
||||
const { initializeDatabaseConnection, client } = require("./db/db.js");
|
||||
|
||||
app.use("/socket.io", express.static("./node_modules/socket.io/client-dist/"));
|
||||
const corsOptions = {
|
||||
origin: "http://localhost:5173",
|
||||
optionsSuccessStatus: 200,
|
||||
};
|
||||
|
||||
app.get("/api/signup", (req, res) => {
|
||||
res.send("niger");
|
||||
// Serve socket.io js
|
||||
app.use("/socket.io", express.static("./node_modules/socket.io/client-dist/"));
|
||||
app.use(cors(corsOptions));
|
||||
|
||||
app.post("/api/auth/signup", (req, res) => {
|
||||
res.status(200).send("Successfully signed up");
|
||||
});
|
||||
|
||||
app.post("/api/auth/login", (req, res) => {
|
||||
res.status(200).send("Successfully logged In");
|
||||
});
|
||||
io.on("connection", (socket) => {
|
||||
console.log(`User: ${socket.id} just connected`);
|
||||
});
|
||||
|
||||
//initializeDatabaseConnection();
|
||||
app.listen(PORT, corsOptions, () => {
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Server is running on port: ${PORT}`);
|
||||
});
|
||||
|
||||
0
server/socket/socket.js
Normal file
0
server/socket/socket.js
Normal file
Reference in New Issue
Block a user