Skip to content

Commit

Permalink
removed economy page and extracted authOptions to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
pvk05 committed Nov 19, 2024
1 parent 2009232 commit 24c01b5
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 264 deletions.
9 changes: 0 additions & 9 deletions app/(pages)/(main)/volunteering/economy/layout.js

This file was deleted.

145 changes: 0 additions & 145 deletions app/(pages)/(main)/volunteering/economy/page.js

This file was deleted.

2 changes: 1 addition & 1 deletion app/api/utils/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getServerSession } from "next-auth";
import { authOptions } from "../v2/auth/[...nextauth]/route";
import { authOptions } from "../utils/authOptions";
import { NextRequest, NextResponse } from "next/server";

const NOT_AUTHORIZED = NextResponse.json({error: "Not authorized"}, {status: 403})
Expand Down
95 changes: 95 additions & 0 deletions app/api/utils/authOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

import Email from "next-auth/providers/email";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import prisma from "@/prisma/prismaClient";

export const authOptions = {
providers: [
Email({
server: {
host: "smtp.gmail.com",
port: 587,
auth: {
user: process.env.NODEMAILER_NOREPLY_USER,
pass: process.env.NODEMAILER_NOREPLY_PASSWORD,
},
},
from: process.env.NODEMAILER_NOREPLY_USER,
}),
],
callbacks: {
async signIn({ user }) {
const cybUser = await prisma.user.findFirst({
where: {
email: user.email,
},
});

if (!cybUser) {
throw "No user found";
}

if (!cybUser.active) {
throw "Email has not been verified";
}

return true;
},
async session({ session }) {
const cybUser = await prisma.user.findFirst({
where: {
email: session.user.email,
},
select: {
id: true,
firstName: true,
lastName: true,
recruitedByUser: true,
recruitedUsers: true,
roles: {
select: {
role: {
select: {
name: true,
},
},
},
},
},
});

const semesters = await prisma.semester.findMany({
orderBy: {
id: "desc",
},
take: 1,
});
const currentSemester = await semesters[0];

if (cybUser) {
session.user.name = `${cybUser.firstName} ${cybUser.lastName}`;
delete session.user.name;

session.user = {
...session.user,
...cybUser,
roles: cybUser.roles.map((e) => e.role.name),
name: `${cybUser.firstName} ${
cybUser.lastName ? cybUser.lastName : ""
}`,
};
}

// console.log(currentSemester);
if (currentSemester) {
session.semester = currentSemester;
}

return session;
},
},
pages: {
signIn: "/auth/signIn",
},
adapter: PrismaAdapter(prisma),
};
96 changes: 1 addition & 95 deletions app/api/v2/auth/[...nextauth]/route.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,6 @@


import NextAuth from "next-auth";
import Email from "next-auth/providers/email";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import prisma from "@/prisma/prismaClient";

export const authOptions = {
providers: [
Email({
server: {
host: "smtp.gmail.com",
port: 587,
auth: {
user: process.env.NODEMAILER_NOREPLY_USER,
pass: process.env.NODEMAILER_NOREPLY_PASSWORD,
},
},
from: process.env.NODEMAILER_NOREPLY_USER
}),
],
callbacks: {
async signIn({ user }) {

const cybUser = await prisma.user.findFirst({
where: {
email: user.email
}
})

if (!cybUser) {
throw "No user found"
}

if (!cybUser.active) {
throw "Email has not been verified"
}

return true
},
async session({ session }) {

const cybUser = await prisma.user.findFirst({
where: {
email: session.user.email,
},
select: {
id: true,
firstName: true,
lastName: true,
recruitedByUser: true,
recruitedUsers: true,
roles: {
select: {
role: {
select: {
name: true
}
},
},
},
},
});

const semesters = await prisma.semester.findMany({
orderBy: {
id: "desc"
},
take: 1,
})
const currentSemester = await semesters[0];

if (cybUser) {
session.user.name = `${cybUser.firstName} ${cybUser.lastName}`
delete session.user.name;

session.user = {
...session.user,
...cybUser,
roles: cybUser.roles.map((e) => e.role.name),
name: `${cybUser.firstName} ${cybUser.lastName ? cybUser.lastName : ""}`
}
}

// console.log(currentSemester);
if (currentSemester) {
session.semester = currentSemester;
}

return session
}
},
pages: {
signIn: "/auth/signIn"
},
adapter: PrismaAdapter(prisma),
}
import { authOptions } from "@/app/api/utils/authOptions";

const handler = NextAuth(authOptions);

Expand Down
4 changes: 1 addition & 3 deletions app/api/v2/memberships/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { NextResponse } from "next/server";
import prisma from "@/prisma/prismaClient";
import { Auth } from "../../utils/auth";
import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/v2/auth/[...nextauth]/route";


import { authOptions } from "@/app/api/utils/authOptions";


export async function GET(req) {
Expand Down
2 changes: 1 addition & 1 deletion app/api/v2/recruitGraph/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NextResponse } from "next/server";
import prisma from "@/prisma/prismaClient";
import { Auth } from "../../utils/auth";
import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/v2/auth/[...nextauth]/route";
import { authOptions } from "@/app/api/utils/authOptions";



Expand Down
2 changes: 1 addition & 1 deletion app/api/v2/roles/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NextResponse } from "next/server";
import prisma from "@/prisma/prismaClient";
import { Auth } from "../../utils/auth";
import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/v2/auth/[...nextauth]/route";
import { authOptions } from "@/app/api/utils/authOptions";



Expand Down
Loading

0 comments on commit 24c01b5

Please sign in to comment.