Skip to content
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

added zod verification in frontend #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions backend/config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ const colors = require("colors");

const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
const conn = mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});

console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline);
console.log(conn);
} catch (error) {
console.error(`Error: ${error.message}`.red.bold);
process.exit(1); // Exit with a non-zero status code to indicate an error
}
};

module.exports = connectDB;

13 changes: 13 additions & 0 deletions backend/controllers/userControllers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const asyncHandler = require("express-async-handler");
const User = require("../models/userModel");
const generateToken = require("../config/generateToken");
const zod = require("zod");

//@description Get or Search all users
//@route GET /api/user?search=
Expand All @@ -22,6 +23,13 @@ const allUsers = asyncHandler(async (req, res) => {
//@description Register new user
//@route POST /api/user/
//@access Public

// const newUserSchema = zod.object({
// name: zod.string(),
// email: zod.string().email(),
// password: zod.string(),
// });

const registerUser = asyncHandler(async (req, res) => {
const { name, email, password, pic } = req.body;

Expand Down Expand Up @@ -62,6 +70,11 @@ const registerUser = asyncHandler(async (req, res) => {
//@description Auth the user
//@route POST /api/users/login
//@access Public

// const authUserSchema = zod.object({
// email: zod.string().email(),
// password: zod.string(),
// });
const authUser = asyncHandler(async (req, res) => {
const { email, password } = req.body;

Expand Down
2 changes: 1 addition & 1 deletion backend/models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const userSchema = mongoose.Schema(
default: false,
},
},
{ timestaps: true }
{ timestamps: true }
);

userSchema.methods.matchPassword = async function (enteredPassword) {
Expand Down
7 changes: 5 additions & 2 deletions frontend/package-lock.json

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

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"react-lottie": "^1.2.3",
"react-notification-badge": "^1.5.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"react-scripts": "^4.0.3",
"react-scrollable-feed": "^1.3.1",
"socket.io-client": "^4.1.2",
"web-vitals": "^1.0.1"
Expand Down
22 changes: 22 additions & 0 deletions frontend/src/components/Authentication/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import axios from "axios";
import { useToast } from "@chakra-ui/react";
import { useHistory } from "react-router-dom";
import { ChatState } from "../../Context/ChatProvider";
import * as zod from "zod";

const Login = () => {
const [show, setShow] = useState(false);
Expand All @@ -21,6 +22,27 @@ const Login = () => {

const submitHandler = async () => {
setLoading(true);
const loginSchema = zod.object({
email: zod.string().email(),
password: zod.string(),
});

const { success } = await loginSchema.safeParseAsync({
email: email,
password: password,
});
if (!success) {
toast({
title: "Check Input for errors",
status: "warning",
duration: 5000,
isClosable: true,
position: "bottom",
});
setLoading(false);
return;
}

if (!email || !password) {
toast({
title: "Please Fill all the Feilds",
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/components/Authentication/Signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useToast } from "@chakra-ui/toast";
import axios from "axios";
import { useState } from "react";
import { useHistory } from "react-router";
import * as zod from "zod";

const Signup = () => {
const [show, setShow] = useState(false);
Expand All @@ -22,6 +23,28 @@ const Signup = () => {

const submitHandler = async () => {
setPicLoading(true);
const newUserSchema = zod.object({
name: zod.string(),
email: zod.string().email(),
password: zod.string(),
});
const { success } = await newUserSchema.safeParseAsync({
name: name,
email: email,
password: password,
});

if (!success) {
toast({
title: "Check Input for errors",
status: "warning",
duration: 5000,
isClosable: true,
position: "bottom",
});
setPicLoading(false);
return;
}
if (!name || !email || !password || !confirmpassword) {
toast({
title: "Please Fill all the Feilds",
Expand Down
2 changes: 1 addition & 1 deletion frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10198,7 +10198,7 @@ [email protected]:
tiny-invariant "^1.0.2"
tiny-warning "^1.0.0"

[email protected]:
react-scripts@^4.0.3:
version "4.0.3"
resolved "https://registry.npmjs.org/react-scripts/-/react-scripts-4.0.3.tgz"
integrity sha512-S5eO4vjUzUisvkIPB7jVsKtuH2HhWcASREYWHAQ1FP5HyCv3xgn+wpILAEWkmy+A+tTNbSZClhxjT3qz6g4L1A==
Expand Down
12 changes: 11 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.12.9",
"nodemon": "^2.0.7",
"socket.io": "^4.1.2"
"socket.io": "^4.1.2",
"zod": "^3.23.8"
}
}
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ [email protected]:
resolved "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz"
integrity sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==

mongoose@^5.12.9:
mongoose@*, mongoose@^5.12.9:
version "5.12.9"
resolved "https://registry.npmjs.org/mongoose/-/mongoose-5.12.9.tgz"
integrity sha512-ZSDjW15DmUbHQcZ2PqoXsJeYnpYipISi6QJH/XHR9dcSg3IRNCa86apcTptBux03/YBPiSZlKNYUNDx7iuMWoA==
Expand Down Expand Up @@ -1531,3 +1531,8 @@ xdg-basedir@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz"
integrity sha512-PSNhEJDejZYV7h50BohL09Er9VaIefr2LMAf3OEmpCkjOi34eYyQYAXUTjEQtZJTKcF0E2UKTh+osDLsgNim9Q==

zod@^3.23.8:
version "3.23.8"
resolved "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz"
integrity sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==