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

created apis for handling login and signup #51

Open
wants to merge 1 commit 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
44 changes: 44 additions & 0 deletions server/controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import User from "../models/User.js";

const signup=async(req,res)=>{
try {
const{username,email,password}=req.body;
if(!(username&&email&&password)){
return res.status(400).json({success:false,message:"all fields are necessary"});
}
const finduser= await User.findOne({email});
if(finduser){
return res.status(400).json({success:false,message:"user already exists"});
}
const user= new User({username,email,password});
const newUser= await user.save();
console.log("new user registered successfully");
newUser.password="";
return res.status(200).json({success:true,message:"user registered successfully"});
} catch (error) {
console.log(error);
res.status(500).json({success:false,message:"something went wrong"});
}
}

const login=async(req,res)=>{
try {
const{email,password}=req.body;
if(!(email&&password)){
return res.status(400).json({success:false,message:"all fields are necessary"});
}
const finduser= await User.findOne({email});
if(!finduser){
return res.status(400).json({success:false,message:"user does not exists"});
}
if(password!==finduser.password){
return res.status(400).json({success:false,message:"wrong password"});
}
return res.status(200).json({success:true,message:"user login successfully"});
} catch (error) {
console.log(error);
res.status(500).json({success:false,message:"something went wrong"});
}
}

export {signup,login};
35 changes: 32 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
// const { Server } = require("socket.io");
import { Server } from "socket.io";
import express from 'express';
import http from "http";
import mongoose from "mongoose";
import authRouter from "./routes/auth.route.js";
import dotenv from 'dotenv';
dotenv.config();

const io = new Server(8000, {
const dbconnect=async()=>{
try {
await mongoose.connect(process.env.MONGODB_URL);
console.log("connected to database");
} catch (error) {
throw error;
}
}

const app=express();
const PORT=8000;
const server=http.createServer(app);

app.use(express.json());
//auth api

app.use("/api",authRouter)


const io = new Server(server, {
cors: true,
});

Expand All @@ -10,7 +35,7 @@ const io = new Server(8000, {

io.on("connection", (socket) => {

// console.log(`Socket Connected: ${socket.id}`);
console.log(`Socket Connected: ${socket.id}`);
// socket.on("Send_RoomJoin_Req", ({ roomCode, uuid }) => {

// if (RoomU1[roomCode] && RoomU1[roomCode] != uuid) {
Expand Down Expand Up @@ -54,4 +79,8 @@ io.on("connection", (socket) => {

});

console.log("Server Start");
server.listen(PORT,()=>{
dbconnect();
console.log("Server Start");
})

21 changes: 21 additions & 0 deletions server/models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import mongoose from "mongoose";

const userSchema= new mongoose.Schema({
username:{
require:true,
type:String,
unique:true
},
email:{
require:true,
type:String,
unique:true
},
password:{
require:true,
type:String,
}
})

const User=mongoose.model("User",userSchema);
export default User;
Loading