Skip to content

Commit

Permalink
continous refactory
Browse files Browse the repository at this point in the history
  • Loading branch information
* committed Jan 9, 2024
1 parent 74bd75e commit 6d79a5f
Show file tree
Hide file tree
Showing 45,219 changed files with 3,837,667 additions and 12,729 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
api/node_modules
client/node_modules
/.pnp
.pnp.js

Expand Down
1 change: 1 addition & 0 deletions api/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DB_KEY= ""
62 changes: 62 additions & 0 deletions api/controllers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { db } from "../db.js";
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";

export const register = (req, res) => {
// CHECK EXISTING USER
const q = "SELECT * FROM users WHERE email = ? OR username = ?";

db.query(q, [req.body.email, req.body.username], (err, data) => {
if (err) return res.status(500).json(err);
if (data.length) return res.status(409).json("User already exists!");

// Hash the password and create a user
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync(req.body.password, salt);

const q = "INSERT INTO users(`username`,`email`,`password`) VALUES (?)";
const values = [req.body.username, req.body.email, hash];

db.query(q, [values], (err, data) => {
if (err) return res.status(500).json(err);
return res.status(200).json("User has been created.");
});
});
};

export const login = (req, res) => {
// CHECK USER

const q = "SELECT * FROM users WHERE username = ?";

db.query(q, [req.body.username], (err, data) => {
if (err) return res.status(500).json(err);
if (data.length === 0) return res.status(404).json("User not found!");

// Check password
const isPasswordCorrect = bcrypt.compareSync(
req.body.password,
data[0].password
);

if (!isPasswordCorrect)
return res.status(400).json("Wrong username or password!");

const token = jwt.sign({ id: data[0].id }, "jwtkey");
const { password, ...other } = data[0];

res
.cookie("access_token", token, {
httpOnly: true,
})
.status(200)
.json(other);
});
};

export const logout = (req, res) => {
res.clearCookie("access_token",{
sameSite:"none",
secure:true
}).status(200).json("User has been logged out.")
};
89 changes: 89 additions & 0 deletions api/controllers/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { db } from "../db.js";
import jwt from "jsonwebtoken";

export const getPosts = (req, res) => {
const q = req.query.cat
? "SELECT * FROM posts WHERE cat=?"
: "SELECT * FROM posts";

db.query(q, [req.query.cat], (err, data) => {
if (err) return res.status(500).send(err);

return res.status(200).json(data);
});
};

export const getPost = (req, res) => {
const q =
"SELECT * FROM `posts` WHERE `id`=?";

db.query(q, [req.params.id], (err, data) => {
if (err) return res.status(500).json(err);

return res.status(200).json(data[0]);
});
};

export const addPost = (req, res) => {
const token = req.cookies.access_token;
if (!token) return res.status(401).json("Not authenticated!");

jwt.verify(token, "jwtkey", (err, userInfo) => {
if (err) return res.status(403).json("Token is not valid!");

const q =
"INSERT INTO posts(`title`, `desc`, `img`, `cat`, `date`,`uid`) VALUES (?)";

const values = [
req.body.title,
req.body.desc,
req.body.img,
req.body.cat,
req.body.date,
userInfo.id,
];

db.query(q, [values], (err, data) => {
if (err) return res.status(500).json(err);
return res.json("Post has been created.");
});
});
};

export const deletePost = (req, res) => {
const token = req.cookies.access_token;
if (!token) return res.status(401).json("Not authenticated!");

jwt.verify(token, "jwtkey", (err, userInfo) => {
if (err) return res.status(403).json("Token is not valid!");

const postId = req.params.id;
const q = "DELETE FROM posts WHERE `id` = ? AND `uid` = ?";

db.query(q, [postId, userInfo.id], (err, data) => {
if (err) return res.status(403).json("You can delete only your post!");

return res.json("Post has been deleted!");
});
});
};

export const updatePost = (req, res) => {
const token = req.cookies.access_token;
if (!token) return res.status(401).json("Not authenticated!");

jwt.verify(token, "jwtkey", (err, userInfo) => {
if (err) return res.status(403).json("Token is not valid!");

const postId = req.params.id;
const q =
"UPDATE posts SET `title`=?,`desc`=?,`img`=?,`cat`=? WHERE `id` = ? AND `uid` = ?";

const values = [req.body.title, req.body.desc, req.body.img, req.body.cat];

db.query(q, [...values, postId, userInfo.id], (err, data) => {
if (err) return res.status(500).json(err);
return res.json("Post has been updated.");
});
});
};
101 changes: 101 additions & 0 deletions api/controllers/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { db } from '../db.js';
import jwt from 'jsonwebtoken';

export const getUsers = (req,res) => {
db.query("SELECT `id`,`name`,`username`,`picture`,`email`,`telp`,`bio` FROM `users`;", (error, result) => {

if(error) return res.status(500).send(error);
return res.status(200).json(result);
});
}

export const getUser = (req,res) => {
const q = "SELECT `id`,`name`,`username`,`picture`,`email`,`telp`,`bio` FROM `users` WHERE `id`=?;";

db.query(q, [req.params.id], (error, result) => {
if(error) return res.status(500).send(error);
return res.status(200).json(result[0]);
});
}

export const addUser = (req,res) => {
const token = req.cookies.access_token;
if (!token) return res.status(401).json("Not authenticated!");

jwt.verify(token, "jwtkey", (err, userInfo) => {
if (err) return res.status(403).json("Token is not valid!");

const q = "INSERT INTO `users` (`name`, `username`, `email`, `email_verified_at`, `password`, `telp`, `bio`, `block`, `picture`, `remember_token`, `created_by`, `updated_by`, `created_at`, `updated_at`) VALUES (?);"

const values = [
req.body.name,
req.body.username,
req.body.email,
req.body.email_verified_at,
req.body.password,
req.body.block,
req.body.picture,
req.body.remember_token,
req.body.created_by,
req.body.updated_by,
req.body.created_at,
req.body.updated_at,
userInfo.id,
];

db.query(q, [values], (error, result) => {
if (error) return res.status(500).json(error);
return res.json("User has been created successfully.");
});
});
}

export const updateUser = (req,res) => {
const token = req.cookies.access_token;
if (!token) return res.status(401).json("Not authenticated!");

jwt.verify(token, "jwtkey", (err, userInfo) => {
if (err) return res.status(403).json("Token is not valid!");

const userId = req.params.id;
const q = "UPDATE `users` SET `name`=?, `usermame`=? `email`=?,`picture`=?,`email_verified_at`=? `password`=? `bio`=? `block`=? `updated_at`=? `remember_token`=? `created_by`=? `updated_by`=? `created_at`=? WHERE `id`=?";

const values = [
req.body.name,
req.body.username,
req.body.email,
req.body.picture,
req.body.email_vedified_at,
req.body.password,
req.body.bio,
req.body.block,
req.body.created_at,
req.body.created_by,
req.body.updated_at,
req.body.updated_by,
req.body.cat
];

db.query(q, [...values, userId, userInfo.id], (error, result) => {
if (error) return res.status(500).json(error);
return res.json("User has been updated successfully.");
});
});
}

export const deleteUser = (req,res) => {
const token = req.cookies.access_token;
if (!token) return res.status(401).json("Not authenticated!");

jwt.verify(token, "jwtkey", (err, userInfo) => {
if (err) return res.status(403).json("Token is not valid!");

const postId = req.params.id;
const q = "DELETE FROM `users` WHERE `id`=?";

db.query(q, [postId, userInfo.id], (error, result) => {
if (err) return res.status(403).json("Forbidden action!");
return res.json("User has been deleted successfully");
});
});
}
8 changes: 8 additions & 0 deletions api/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import mysql from "mysql"

export const db = mysql.createConnection({
host:"localhost",
user:"root",
password: process.env.DB_KEY,
database:"react-serenity"
})
34 changes: 34 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import express from "express";
import authRoutes from "./routes/auth.js";
import userRoutes from "./routes/users.js";
import postRoutes from "./routes/posts.js";
import cookieParser from "cookie-parser";
import multer from "multer";

const app = express();

app.use(express.json());
app.use(cookieParser());
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "../client/public/upload");
},
filename: function (req, file, cb) {
cb(null, Date.now() + file.originalname);
},
});

const upload = multer({ storage });

app.post("/api/upload", upload.single("file"), function (req, res) {
const file = req.file;
res.status(200).json(file.filename);
});

app.use("/api/auth", authRoutes);
app.use("/api/users", userRoutes);
app.use("/api/posts", postRoutes);

app.listen(8800, () => {
console.log("Connected!");
});
48 changes: 48 additions & 0 deletions api/index.mysql2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import express from "express";
import cors from "cors";
import session from "express-session";
import dotenv from "dotenv";

import db from './config/db.js';
import SequelizeStore from "connect-session-sequelize";

import UserRoute from "./routes/UserRoute.js";
import ProductRoute from "./routes/ProductRoute.js";
import AuthRoute from "./routes/AuthRoute.js";

// import cookieParser from "cookie-parser";
dotenv.config();

const app = express();
app.use(express.json());
// app.use(cookieParser());

const sessionStore = SequelizeStore(session.Store);

const store = new sessionStore({
db: db
});

app.use(session({
secret: process.env.SESS_SECRET,
resave: false,
saveUninitialized: true,
store: store,
cookie: {
secure: 'auto'
}
}));

app.use(cors({
credentials: true,
origin: 'http://localhost:3000'
}));

app.use(express.json());
app.use(UserRoute);
app.use(ProductRoute);
app.use(AuthRoute);

app.listen(process.env.PORT || 8800, () => {
console.log("Connected!");
});
1 change: 1 addition & 0 deletions api/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/node_modules/.bin/mkdirp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/node_modules/.bin/nodemon

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/node_modules/.bin/nodetouch

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/node_modules/.bin/nopt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6d79a5f

Please sign in to comment.