Skip to content

Commit

Permalink
Reformat Code
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaynegi45 committed Nov 21, 2024
1 parent a5aed9b commit 1a7c4cd
Show file tree
Hide file tree
Showing 59 changed files with 2,762 additions and 2,823 deletions.
11 changes: 6 additions & 5 deletions src/app/about/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react'
import About from '@/components/about/About'

const page = () => {
return (
<div>
<About/>
</div>
)
return (
<div>
<About/>
</div>
)
}

export default page
4 changes: 2 additions & 2 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { handlers } from "@/auth"; // Referring to the auth.ts we just created
export const { GET, POST } = handlers;
import {handlers} from "@/auth"; // Referring to the auth.ts we just created
export const {GET, POST} = handlers;
149 changes: 72 additions & 77 deletions src/app/api/auth/register/route.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,84 @@

import { db } from "@/lib/drizzle";
import { otps, users } from "@/lib/schema";
import { signupSchema } from "@/lib/zod";
import { hash } from "bcryptjs";
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import {db} from "@/lib/drizzle";
import {otps, users} from "@/lib/schema";
import {signupSchema} from "@/lib/zod";
import {hash} from "bcryptjs";
import {NextRequest, NextResponse} from "next/server";
import {z} from "zod";

import otpEmailTemplate from "@/lib/templates/otp-template";
import mailer from "@/lib/mailer";



// `[email protected]` email is for development only
const senderEmail = process.env.SENDER_EMAIL || "[email protected]";



export async function POST(req: NextRequest) {
try {
const payload = await req.json();

const { email, name, password, username, alerts } = signupSchema.parse(payload)
if (!email || !name || !password || !username) {
return NextResponse.json(
{ error: "name, username, email and password are required." },
{ status: 400 }
);
}

const userWithUsername = await db.query.users.findFirst({
where: (users, { eq }) => eq(users.username, username),
});
if (userWithUsername) throw new Error("User with username already exists.");

const userWithEmail = await db.query.users.findFirst({
where: (users, { eq }) => eq(users.email, email),
});

if (userWithEmail) throw new Error("User with email already exists.");

const hashedPassword = await hash(password, 10);

await db.insert(users).values({
email,
name,
password: hashedPassword,
alerts,
username
});


const otp = Math.floor(100000 + Math.random() * 900000).toString(); // 6-digit OTP
// Set expiration time (e.g., 10 minutes from now)
const expiresAt = new Date(Date.now() + 10 * 60 * 1000); // 10 minutes in the future

const user = await db.query.users.findFirst({
where: (users, { eq }) => eq(users.email, email),
})

await db.insert(otps).values({
expiresAt,
otp,
userId: user!.id,
createdAt: new Date(),
});


await mailer.sendMail({
from: `Uttarakhand Culture <${senderEmail}>`,
to: [email],
subject: 'Verify you email with OTP',
html: otpEmailTemplate(name, otp),
})




return NextResponse.json({
message: `Registered. Now login!`,
});
} catch (error: any) {
if (error instanceof z.ZodError) {
return NextResponse.json({ error: error.errors }, { status: 500 });
try {
const payload = await req.json();

const {email, name, password, username, alerts} = signupSchema.parse(payload)
if (!email || !name || !password || !username) {
return NextResponse.json(
{error: "name, username, email and password are required."},
{status: 400}
);
}

const userWithUsername = await db.query.users.findFirst({
where: (users, {eq}) => eq(users.username, username),
});
if (userWithUsername) throw new Error("User with username already exists.");

const userWithEmail = await db.query.users.findFirst({
where: (users, {eq}) => eq(users.email, email),
});

if (userWithEmail) throw new Error("User with email already exists.");

const hashedPassword = await hash(password, 10);

await db.insert(users).values({
email,
name,
password: hashedPassword,
alerts,
username
});


const otp = Math.floor(100000 + Math.random() * 900000).toString(); // 6-digit OTP
// Set expiration time (e.g., 10 minutes from now)
const expiresAt = new Date(Date.now() + 10 * 60 * 1000); // 10 minutes in the future

const user = await db.query.users.findFirst({
where: (users, {eq}) => eq(users.email, email),
})

await db.insert(otps).values({
expiresAt,
otp,
userId: user!.id,
createdAt: new Date(),
});


await mailer.sendMail({
from: `Uttarakhand Culture <${senderEmail}>`,
to: [email],
subject: 'Verify you email with OTP',
html: otpEmailTemplate(name, otp),
})


return NextResponse.json({
message: `Registered. Now login!`,
});
} catch (error: any) {
if (error instanceof z.ZodError) {
return NextResponse.json({error: error.errors}, {status: 500});
}
console.log("[REGISTER_ERROR]: ", error);
return NextResponse.json({error: error.message}, {status: 500});
}
console.log("[REGISTER_ERROR]: ", error);
return NextResponse.json({ error: error.message }, { status: 500 });
}
}
112 changes: 56 additions & 56 deletions src/app/api/auth/resend-otp/route.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
import { db } from "@/lib/drizzle";
import { otps } from "@/lib/schema";
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import {db} from "@/lib/drizzle";
import {otps} from "@/lib/schema";
import {NextRequest, NextResponse} from "next/server";
import {z} from "zod";

import otpEmailTemplate from "@/lib/templates/otp-template";
import { auth } from "@/auth";
import {auth} from "@/auth";
import mailer from "@/lib/mailer";

// `[email protected]` email is for development only
const senderEmail = process.env.SENDER_EMAIL || "[email protected]";

export async function POST(req: NextRequest) {
try {
const session = await auth();
if (!session) throw new Error("Login first to verify email");
try {
const session = await auth();
if (!session) throw new Error("Login first to verify email");

const userWithEmail = await db.query.users.findFirst({
where: (users, { eq }) => eq(users.email, session.user.email!),
});
const userWithEmail = await db.query.users.findFirst({
where: (users, {eq}) => eq(users.email, session.user.email!),
});

if (!userWithEmail) throw new Error("User with email not exists.");
if (!userWithEmail) throw new Error("User with email not exists.");

const prevOtps = await db.query.otps.findMany({
where: (otps, { eq }) => eq(otps.userId, userWithEmail.id),
orderBy: (otps, { desc }) => desc(otps.expiresAt),
});
const prevOtps = await db.query.otps.findMany({
where: (otps, {eq}) => eq(otps.userId, userWithEmail.id),
orderBy: (otps, {desc}) => desc(otps.expiresAt),
});

// Check if there is a valid OTP
const now = new Date();
const validOtp = prevOtps.find((otp) => otp.expiresAt > now);
// Check if there is a valid OTP
const now = new Date();
const validOtp = prevOtps.find((otp) => otp.expiresAt > now);

if (validOtp) {
const remainingTimeInSeconds = Math.floor(
(validOtp.expiresAt.getTime() - now.getTime()) / 1000
); // Time in seconds
const minutes = Math.floor(remainingTimeInSeconds / 60); // Full minutes
const seconds = remainingTimeInSeconds % 60; // Remaining seconds
if (validOtp) {
const remainingTimeInSeconds = Math.floor(
(validOtp.expiresAt.getTime() - now.getTime()) / 1000
); // Time in seconds
const minutes = Math.floor(remainingTimeInSeconds / 60); // Full minutes
const seconds = remainingTimeInSeconds % 60; // Remaining seconds

return NextResponse.json(
{
message: `Please wait ${minutes} minutes and ${seconds} seconds before resending the OTP.`,
},
{ status: 429 }
);
}
return NextResponse.json(
{
message: `Please wait ${minutes} minutes and ${seconds} seconds before resending the OTP.`,
},
{status: 429}
);
}

const otp = Math.floor(100000 + Math.random() * 900000).toString(); // 6-digit OTP
// Set expiration time (e.g., 10 minutes from now)
const expiresAt = new Date(Date.now() + 10 * 60 * 1000); // 10 minutes in the future
const otp = Math.floor(100000 + Math.random() * 900000).toString(); // 6-digit OTP
// Set expiration time (e.g., 10 minutes from now)
const expiresAt = new Date(Date.now() + 10 * 60 * 1000); // 10 minutes in the future

await db.insert(otps).values({
expiresAt,
otp,
userId: userWithEmail.id,
createdAt: new Date(),
});
await db.insert(otps).values({
expiresAt,
otp,
userId: userWithEmail.id,
createdAt: new Date(),
});

await mailer.sendMail({
from: `Uttarakhand Culture <${senderEmail}>`,
to: [userWithEmail.email!],
subject: "Verify you email with OTP",
html: otpEmailTemplate(userWithEmail.name!, otp),
});
await mailer.sendMail({
from: `Uttarakhand Culture <${senderEmail}>`,
to: [userWithEmail.email!],
subject: "Verify you email with OTP",
html: otpEmailTemplate(userWithEmail.name!, otp),
});

return NextResponse.json({
message: `OTP has been resent to your email.`,
});
} catch (error: any) {
if (error instanceof z.ZodError) {
return NextResponse.json({ error: error.errors }, { status: 500 });
return NextResponse.json({
message: `OTP has been resent to your email.`,
});
} catch (error: any) {
if (error instanceof z.ZodError) {
return NextResponse.json({error: error.errors}, {status: 500});
}
console.log("[OTP_RESEND_ERROR]: ", error);
return NextResponse.json({error: error.message}, {status: 500});
}
console.log("[OTP_RESEND_ERROR]: ", error);
return NextResponse.json({ error: error.message }, { status: 500 });
}
}
Loading

0 comments on commit 1a7c4cd

Please sign in to comment.