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

22 next auth routes #34

Merged
merged 7 commits into from
Feb 11, 2024
Merged
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
29 changes: 28 additions & 1 deletion frontend/src/app/(auth)/signup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,36 @@ export default function SignUpPage() {
setPasswordShown(!passwordShown);
};

const handleSignUp = () => {
const handleSignUp = async () => {
// CHANGE THIS TO NAVIGATE TO NEW PAGE
console.log("Email", email);
try {
const resUserExists = await fetch("api/userExists", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email }),
});

const { user } = await resUserExists.json();

if (user) {
// user exists so return
console.log("User already exists");
return;
}
// calling the registration api
await fetch("api/signUp", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email, password }),
});
} catch (error) {
console.log("Error during registration");
}
};

return (
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/app/api/checkUser/[email]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import connectDB from "@/database/db";
import User from "@/database/userSchema";
import { NextResponse } from "next/server";

type IParams = {
params: {
email: string;
};
};

export async function GET(req: Request, { params }: IParams) {
await connectDB();
const { email } = params;
try {
const user = await User.findOne({ email }).orFail();
return NextResponse.json(user);
} catch (err) {
return NextResponse.json("Error retrieving user.", { status: 404 });
}
}
18 changes: 18 additions & 0 deletions frontend/src/app/api/signUp/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import connectDB from "@/database/db";
import User from "@/database/userSchema";
import { NextResponse } from "next/server";

export async function POST(req: Request) {
try {
const { email, password } = await req.json();
await connectDB();
await User.create({ email, password });

return NextResponse.json({ message: "User registered." }, { status: 201 });
} catch (error) {
return NextResponse.json(
{ message: "An error occurred while registering." },
{ status: 500 },
);
}
}
15 changes: 15 additions & 0 deletions frontend/src/app/api/userExists/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import connectDB from "@/database/db";
import User from "@/database/userSchema";
import { NextResponse } from "next/server";

export async function POST(req: Request) {
try {
await connectDB();
// sending email in POST request
const { email } = await req.json();
const user = await User.findOne({ email }).select("_id");
return NextResponse.json({ user });
} catch (error) {
console.log(error);
}
}
9 changes: 9 additions & 0 deletions frontend/src/database/userSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import mongoose, { Schema } from "mongoose";

const UserSchema = new Schema({
name: { type: String, required: false },
email: { type: String, required: true },
password: { type: String, required: true },
});

export default mongoose.models.User || mongoose.model("User", UserSchema);
Loading