Skip to content

Structured #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions client/config/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import dotenv from "dotenv";

dotenv.config();

const variables = {
clientPort
}
export default variables
103 changes: 58 additions & 45 deletions client/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import dotenv from "dotenv";
dotenv.config();
import express, { response } from "express";
import express from "express";
import bodyParser from "body-parser";
import axios from 'axios';
import md5 from "md5";
import multer from "multer";
import imgbbUploader from 'imgbb-uploader';
import fs from 'fs';
import bcrypt from "bcrypt";

const app = express();
const port = process.env.PORT || 3000;
const serverURL = `http://localhost:${process.env.SERVER_PORT}`;
const serverURL = `https://t-chat-server.onrender.com`;

let users = []
let messages = []
Expand Down Expand Up @@ -79,43 +79,53 @@ app.post("/changepp",upload.single('picture'),async(req,res)=>{
})
})

app.post("/login",async(req,res)=>{
const name = req.body.username;
const password = req.body.password;
//check if the user is in the database and render the index
let found = false;
let activeUser;
await getAllUsers();
users.forEach(user => {
if(user.name.toLowerCase() == name.toLowerCase()){
found = true
activeUser = user
}
});
if(found){
await axios.post(`${serverURL}/logIn?user=${activeUser.name}`)
.then(()=>console.log("loged In"))
.catch(err=>console.log(err))
await getUsers(name)
if(activeUser.password == md5(password)){
res.render('index',{
users:users,
user:activeUser,
messages:messages
})
}else{
res.render('start',{
login:1,
error:"incorrect password. Try again"
})
app.post("/login", async (req, res) => {
try {
const name = req.body.username;
const password = req.body.password;
//check if the user is in the database and render the index
let found = false;
let activeUser;
await getAllUsers();
users.forEach((user) => {
if (user.name.toLowerCase() == name.toLowerCase()) {
found = true;
activeUser = user;
}
});
if (found) {
await axios
.post(`${serverURL}/logIn?user=${activeUser.name}`)
.then(() => console.log("loged In"))
.catch((err) => console.log(err));
await getUsers(name);
bcrypt.compare(password, activeUser.password, (err, result) => {
if (result) {
res.render("index", {
users: users,
user: activeUser,
messages: messages,
});
} else {
res.render("start", {
login: 1,
error: "incorrect password. Try again",
});
}
});
} else {
res.render("start", {
login: 1,
error: "incorrect username. Try again",
});
}
}else{
res.render('start',{
login:1,
error:"incorrect username. Try again"
})
} catch {
res.render("start", {
login: 1,
error: "Error signing you in. Try again later",
});
}
})
});

app.post("/signup",async(req,res)=>{
const username = req.body.username;
Expand Down Expand Up @@ -148,7 +158,7 @@ app.post("/signup",async(req,res)=>{
}catch{
res.render("start",{
signup:1,
error:"Error signing you up. Try again"
error:"Error signing you up. Try again later"
})
}
}else{
Expand Down Expand Up @@ -189,19 +199,22 @@ app.post("/updateuser",async(req,res)=>{
app.post("/deleteUser",async(req,res)=>{
try{
await axios.post(`${serverURL}/deleteUser?user=${req.body.user}`)
.then(()=>console.log("deleted"))
.catch(err=>console.log(err))
res.redirect("/")
res.json({
redirected: true,
url: "/"
});
}catch(err){
res.json({message:err.message})
}
})
app.post("/logOut",async(req,res)=>{
try{
await axios.post(`${serverURL}/logOut?user=${req.body.user}`)
.then(()=>console.log("loged out"))
.catch(err=>console.log(err))
res.redirect("/")

res.json({
redirected: true,
url: "/"
});
}catch(err){
res.json({message:err.message})
}
Expand Down
Loading