Skip to content

Commit

Permalink
Added sign up feature
Browse files Browse the repository at this point in the history
  • Loading branch information
harikrishnan-git committed Dec 24, 2024
1 parent edc8088 commit a8df507
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 0 deletions.
70 changes: 70 additions & 0 deletions backend/controllers/user.auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import bcrypt from "bcryptjs";
import { User } from "../models/user.auth.js";

export const Signup = async (req, res) => {
try {
const { fullName, userName, password, confirmPassword, gender } = req.body;

if (password !== confirmPassword) {
console.log("password variation");
res.status(500).send({ error: "Password doesn't match confirmation" });
}

const user = await User.findOne({ userName });
if (user) {
console.log("Duplicate username");
res.status(500).send({ error: "Duplicate user" });
}

const boyProfile = `https://avatar.iran.liara.run/public/boy?username=${userName}`;
const girlProfile = `https://avatar.iran.liara.run/public/girl?username=${userName}`;

const newUser = await new User({
fullName,
userName,
password,
gender,
profilePic: gender === "male" ? boyProfile : girlProfile,
});

newUser
.save()
.then((result) => {
res.status(201).json({
_id: newUser._id,
fullName: newUser.fullName,
userName: newUser.userName,
gender: newUser.gender,
});
})
.catch((err) => {
console.log(err);
});
res.redirect("/auth/login");
} catch (err) {
console.log(err.message);
res.status(500).send({ error: "Internal server error" });
}
};

export const Login = async (req, res) => {
try {
const { userName, password } = req.body;
const user = User.findOne({ userName });
const isPassword = await bcrypt.compare(password, user.password || "");
if (!user && !isPassword) {
res.status(400).json({ error: "Invalid username or password" });
console.log("Invalid password or username");
}
generateTokensAndSetCookies(user._id, res);
res.status(201).json({
_id: user._id,
fullName: user.fullName,
userName: user.userName,
gender: user.gender,
profilePic: user.profilePic,
});
} catch (err) {
console.log(err);
}
};
10 changes: 10 additions & 0 deletions backend/db/user.auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import mongoose from "mongoose";

export const authConn = async () => {
try {
await mongoose.connect(process.env.MONGO_URI);
console.log("Mongoose connected.");
} catch (err) {
console.log(err);
}
};
14 changes: 14 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@ import dotenv from "dotenv";
dotenv.config();
import express from "express";

import { router } from "./routes/user.auth.js";
import { authConn } from "./db/user.auth.js";

const app = express();
const PORT = process.env.PORT || 5000;

app.use(express.json());

app.use("/auth", router);

app.listen(PORT, () => {
authConn();
console.log(`Listening to ${PORT}`);
app.use("/auth", router);
});
29 changes: 29 additions & 0 deletions backend/models/user.auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import mongoose from "mongoose";

const authSchema = mongoose.Schema({
fullName: {
type: String,
required: true,
},
userName: {
type: String,
required: true,
unique: true,
minLength: 3,
},
password: {
type: String,
required: true,
minLength: 4,
},
gender: {
type: String,
enum: ["male", "female"],
},
profilePic: {
type: "String",
default: "",
},
});

export const User = mongoose.model("User", authSchema);
8 changes: 8 additions & 0 deletions backend/routes/user.auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from "express";
import { Signup, Login } from "../controllers/user.auth.js";

export const router = express.Router();

router.post("/signup", Signup);

router.post("/login", Login);
Loading

0 comments on commit a8df507

Please sign in to comment.