mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
fixed a bit
This commit is contained in:
1 parent
c62413eafa
commit
4ed0480d08
10 files changed
+138
-149
No files matched your search
@@ -1,58 +1,64 @@
|
||||
// app/api/auth/[...nextauth]/route.js
|
||||
import NextAuth from 'next-auth';
|
||||
import CredentialsProvider from 'next-auth/providers/credentials';
|
||||
import { validateCredentials } from '../../../actions/auth';
|
||||
import NextAuth from "next-auth";
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import { validateCredentials } from "@/actions/auth";
|
||||
|
||||
export const authOptions = {
|
||||
providers: [
|
||||
CredentialsProvider({
|
||||
name: 'Credentials',
|
||||
credentials: {
|
||||
username: { label: "Username", type: "text" },
|
||||
password: { label: "Password", type: "password" }
|
||||
},
|
||||
async authorize(credentials) {
|
||||
if (!credentials?.username || !credentials?.password) {
|
||||
return null;
|
||||
}
|
||||
providers: [
|
||||
CredentialsProvider({
|
||||
id: "credentials",
|
||||
name: "Credentials",
|
||||
credentials: {
|
||||
username: { label: "Username", type: "text" },
|
||||
password: { label: "Password", type: "password" },
|
||||
},
|
||||
async authorize(credentials) {
|
||||
try {
|
||||
if (!credentials?.username || !credentials?.password) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const user = await validateCredentials(
|
||||
credentials.username,
|
||||
credentials.password
|
||||
);
|
||||
const user = await validateCredentials(
|
||||
credentials.username,
|
||||
credentials.password
|
||||
);
|
||||
|
||||
return user;
|
||||
}
|
||||
})
|
||||
],
|
||||
callbacks: {
|
||||
async jwt({ token, user }) {
|
||||
if (user) {
|
||||
token.id = user.id;
|
||||
token.role = user.role;
|
||||
token.username = user.username;
|
||||
token.teamId = user.teamId;
|
||||
}
|
||||
return token;
|
||||
},
|
||||
async session({ session, token }) {
|
||||
if (token) {
|
||||
session.user = session.user || {};
|
||||
session.user.id = token.id;
|
||||
session.user.role = token.role;
|
||||
session.user.username = token.username;
|
||||
session.user.teamId = token.teamId;
|
||||
}
|
||||
return session;
|
||||
return user;
|
||||
} catch (error) {
|
||||
console.error("Auth error:", error);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
async jwt({ token, user }) {
|
||||
if (user) {
|
||||
token.id = user.id;
|
||||
token.role = user.role;
|
||||
token.username = user.username;
|
||||
token.teamId = user.teamId;
|
||||
}
|
||||
return token;
|
||||
},
|
||||
pages: {
|
||||
signIn: '/login',
|
||||
async session({ session, token }) {
|
||||
if (token) {
|
||||
session.user = session.user || {};
|
||||
session.user.id = token.id;
|
||||
session.user.role = token.role;
|
||||
session.user.username = token.username;
|
||||
session.user.teamId = token.teamId;
|
||||
}
|
||||
return session;
|
||||
},
|
||||
session: {
|
||||
strategy: "jwt",
|
||||
}
|
||||
},
|
||||
pages: {
|
||||
signIn: "/login",
|
||||
},
|
||||
session: {
|
||||
strategy: "jwt",
|
||||
},
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
};
|
||||
|
||||
const handler = NextAuth(authOptions);
|
||||
export { handler as GET, handler as POST };
|
||||
export { handler as GET, handler as POST };
|
||||
@@ -1,9 +1,8 @@
|
||||
// app/api/teams/[teamId]/route.js
|
||||
import { queryOne, query } from "../../../../lib/db";
|
||||
import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "../../auth/[...nextauth]/route";
|
||||
|
||||
export async function GET(request, { params }) {
|
||||
export async function GET({ params }) {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
if (!session) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { queryOne, run } from "../../../../lib/db";
|
||||
import { queryOne, run } from "@/lib/db";
|
||||
import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "../../auth/[...nextauth]/route";
|
||||
import { nanoid } from "nanoid";
|
||||
@@ -7,9 +7,6 @@ export async function POST(request) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
// Debugging: Log the session
|
||||
console.log("Session:", session);
|
||||
|
||||
if (!session) {
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, error: "Unauthorised" }),
|
||||
@@ -17,7 +14,6 @@ export async function POST(request) {
|
||||
);
|
||||
}
|
||||
|
||||
// Ensure the user is not an admin
|
||||
if (session.user.role === "admin") {
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, error: "Admins cannot create teams" }),
|
||||
@@ -34,27 +30,19 @@ export async function POST(request) {
|
||||
);
|
||||
}
|
||||
|
||||
// Generate a unique team code (6 characters)
|
||||
const teamCode = nanoid(6).toUpperCase();
|
||||
|
||||
// Insert the new team
|
||||
const result = await run("INSERT INTO teams (name, code) VALUES (?, ?)", [
|
||||
name,
|
||||
teamCode,
|
||||
]);
|
||||
await run("INSERT INTO teams (name, code) VALUES (?, ?)", [name, teamCode]);
|
||||
|
||||
// Fetch the newly created team ID
|
||||
const newTeam = await queryOne("SELECT id FROM teams WHERE code = ?", [
|
||||
teamCode,
|
||||
]);
|
||||
|
||||
// Update the user's team_id
|
||||
await run("UPDATE users SET team_id = ? WHERE id = ?", [
|
||||
newTeam.id,
|
||||
userId,
|
||||
]);
|
||||
|
||||
// Update the session with the new teamId
|
||||
session.user.teamId = newTeam.id;
|
||||
|
||||
return new Response(JSON.stringify({ success: true, teamId: newTeam.id }), {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { queryOne, query } from "../../../../lib/db";
|
||||
import { queryOne } from "@/lib/db";
|
||||
import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "../../auth/[...nextauth]/route";
|
||||
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
||||
|
||||
export async function POST(request) {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
Reference in new issue
Block a user