Skip to content

Commit

Permalink
#7 Updated backend and frontend
Browse files Browse the repository at this point in the history
Updated backend and frontend to handle the new inputs.
  • Loading branch information
praneeth776 committed Dec 28, 2024
1 parent f45f892 commit e69df54
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
8 changes: 8 additions & 0 deletions client/src/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ export type Event = {
startHour: number;
endHour: number;
};

export type UserInfo = {
email: string;
password: string;
first_name: string;
last_name: string;
major: string;
}
31 changes: 17 additions & 14 deletions client/src/components/credentialsFormDark.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import prisma from "@/lib/prisma";
import { signIn } from "next-auth/react";
import { redirect } from "next/navigation";
import { useState } from "react";
import bcrypt from "bcryptjs";
import { loginAuthentication, signup } from "@/utils/login-utils";
import { signup } from "@/utils/login-utils";
import { UserInfo } from "@/app/types";
import "../app/signup/styles.css"

interface CredentialsFormProps {
Expand Down Expand Up @@ -44,27 +44,30 @@ export function CredentialsFormDark(props: CredentialsFormProps) {
// setError("Your email or password was incorrect.");
// }

const email = data.get("email") as String;
const password = data.get("password") as String;
if(props.buttonText==='Sign up'){
const response: any = await signup(email,password);
const email = data.get("email") as string;
const password = data.get("password") as string;
const first_name = data.get("first name") as string;
const last_name = data.get("last name") as string;
const major = data.get("major") as string;
const confirm_password = data.get("confirm password") as string;
// Do something when password and confirm_password do not match.
// Can have a function that deals with this and shows appropraite message.
// LETS HAVE A STATE MESSAGE THAT IS EMPTY STRING INITIALLY AND SHOWS THE MESSAGE TO USER IN VARIOUS SCENARIOS SUCH AS 'PASSWORDS DONT MATCH', ETC
const userData: UserInfo = {email:email,password:password,first_name:first_name,last_name:last_name,major:major};


const response: any = await signup(userData);
if(response.status !== 200){// Response not Okay so the status is 401
setError(response.message);
}
// TASK: DO SOMETHING ON SUCCESSFUL SIGNUP
} else{
const response: any = await loginAuthentication(email,password);
if(response.status !== 200){// Response not Okay so the status is 401
setError(response.message);
}
// TASK: DO SOMETHING ON SUCCESSFUL LOGIN
}

};

return (
<form className="w-full bg-blue rounded-sm" onSubmit={handleSubmit}>
<input
type="name" // should these just be names?
type="name" // should these just be names? ANS: "text" should be fine
name="first name"
placeholder="First Name"
required
Expand Down
8 changes: 4 additions & 4 deletions client/src/utils/login-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios';
import { UserInfo } from '@/app/types';


const SERVER_URL = "http://localhost:8080";
Expand All @@ -24,16 +25,15 @@ export async function loginAuthentication(email: String, password: String): Prom
* @param password - Password submitted by the user
* @returns
*/
export async function signup(email: String, password: String): Promise<{ status: number; message: string }>{
export async function signup(userData: UserInfo): Promise<{ status: number; message: string }>{
try{
const data = {email, password};
console.log(`Signup function called with ${data}`)
console.log(`Signup function called with ${userData}`)
const response: Response = await fetch(`${SERVER_URL}/auth/signup`,{
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data)
body: JSON.stringify(userData)
});
const responseData = await response.json();
return {
Expand Down
5 changes: 4 additions & 1 deletion server/models/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import mongoose, { Schema } from 'mongoose';
// Includes username, email id, and password.
const user = new Schema({
emailId: {type:String, required: true, unique: true},
password: {type: String, required: true}
password: {type: String, required: true},
first_name: {type: String, required: true},
last_name: {type: String, required: true},
major: {type: String, required: true},
});

const User = mongoose.model('User',user);
Expand Down
16 changes: 10 additions & 6 deletions server/routes/auth.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import express, {Request, Response} from 'express';
import bcrypt from "bcryptjs";
import User from '../models/Users';
import { UserInfo } from '../types/types';

const router = express.Router();

router.put("/signup",async(req: Request,res: Response)=>{
const {email,password} = req.body;
console.log(`Request received with email:${email} and password:${password}`);
const userData : UserInfo = req.body;
console.log(`Request received with userData:${userData}`);
try{
// Check if account already exists.
const user = await User.findOne({emailId: email});
const user = await User.findOne({emailId: userData.email});
if (user) {
return res.status(400).json({ message: "Email has already been taken" });
}

// Create a new user
const hashedPassword = await bcrypt.hash(password,10); // Salting and Hashing password
const hashedPassword = await bcrypt.hash(userData.password,10); // Salting and Hashing password
const newUser = new User({
emailId: email,
password: hashedPassword
emailId: userData.email,
password: hashedPassword,
first_name: userData.first_name,
last_name: userData.last_name,
major: userData.major
})
await newUser.save();
} catch(err){
Expand Down
7 changes: 7 additions & 0 deletions server/types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type UserInfo = {
email: string;
password: string;
first_name: string;
last_name: string;
major: string;
}

0 comments on commit e69df54

Please sign in to comment.