removed ui, docs package
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
import {
|
||||
AuthOptions,
|
||||
getServerSession as getNextAuthServerSession,
|
||||
} from "next-auth";
|
||||
import { AuthOptions, getServerSession as getNextAuthServerSession } from "next-auth";
|
||||
import { PrismaAdapter } from "@next-auth/prisma-adapter";
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
import { PrismaClient } from "@repo/db";
|
||||
import { prisma } from "@repo/db";
|
||||
import bcrypt from "bcryptjs";
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export const options: AuthOptions = {
|
||||
providers: [
|
||||
@@ -15,7 +11,7 @@ export const options: AuthOptions = {
|
||||
email: { label: "Email", type: "email", placeholder: "E-Mail" },
|
||||
password: { label: "Password", type: "password" },
|
||||
},
|
||||
async authorize(credentials, req) {
|
||||
async authorize(credentials) {
|
||||
try {
|
||||
if (!credentials) throw new Error("No credentials provided");
|
||||
const user = await prisma.user.findFirstOrThrow({
|
||||
|
||||
@@ -1,92 +1,82 @@
|
||||
import axios, { AxiosError } from 'axios';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { DiscordAccount, PrismaClient } from '@repo/db';
|
||||
import { getServerSession } from '../auth/[...nextauth]/auth';
|
||||
import axios, { AxiosError } from "axios";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { DiscordAccount, prisma, PrismaClient } from "@repo/db";
|
||||
import { getServerSession } from "../auth/[...nextauth]/auth";
|
||||
|
||||
export const GET = async (req: NextRequest) => {
|
||||
const session = await getServerSession();
|
||||
const prisma = new PrismaClient();
|
||||
const code = req.nextUrl.searchParams.get('code');
|
||||
const session = await getServerSession();
|
||||
const code = req.nextUrl.searchParams.get("code");
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.redirect(`${process.env.NEXTAUTH_URL}/login`);
|
||||
}
|
||||
if (!session) {
|
||||
return NextResponse.redirect(`${process.env.NEXTAUTH_URL}/login`);
|
||||
}
|
||||
|
||||
if (
|
||||
!process.env.DISCORD_OAUTH_CLIENT_ID ||
|
||||
!process.env.DISCORD_OAUTH_SECRET ||
|
||||
!process.env.DISCORD_REDIRECT ||
|
||||
!code
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Discord OAuth not configured',
|
||||
},
|
||||
{
|
||||
status: 500,
|
||||
}
|
||||
);
|
||||
}
|
||||
if (
|
||||
!process.env.DISCORD_OAUTH_CLIENT_ID ||
|
||||
!process.env.DISCORD_OAUTH_SECRET ||
|
||||
!process.env.DISCORD_REDIRECT ||
|
||||
!code
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Discord OAuth not configured",
|
||||
},
|
||||
{
|
||||
status: 500,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const params = new URLSearchParams({
|
||||
client_id: process.env.DISCORD_OAUTH_CLIENT_ID,
|
||||
client_secret: process.env.DISCORD_OAUTH_SECRET,
|
||||
redirect_uri: process.env.DISCORD_REDIRECT,
|
||||
grant_type: 'authorization_code',
|
||||
code,
|
||||
});
|
||||
const params = new URLSearchParams({
|
||||
client_id: process.env.DISCORD_OAUTH_CLIENT_ID,
|
||||
client_secret: process.env.DISCORD_OAUTH_SECRET,
|
||||
redirect_uri: process.env.DISCORD_REDIRECT,
|
||||
grant_type: "authorization_code",
|
||||
code,
|
||||
});
|
||||
|
||||
const headers = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
};
|
||||
try {
|
||||
const { data: authData } = await axios.post(
|
||||
'https://discord.com/api/oauth2/token',
|
||||
params,
|
||||
{
|
||||
headers,
|
||||
}
|
||||
);
|
||||
const headers = {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
};
|
||||
try {
|
||||
const { data: authData } = await axios.post("https://discord.com/api/oauth2/token", params, {
|
||||
headers,
|
||||
});
|
||||
|
||||
const { data: discordUser } = await axios.get(
|
||||
'https://discord.com/api/users/@me',
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${authData.access_token}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
const { data: discordUser } = await axios.get("https://discord.com/api/users/@me", {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authData.access_token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const discordObject = {
|
||||
userId: session.user.id,
|
||||
accessToken: authData.access_token,
|
||||
refreshToken: authData.refresh_token,
|
||||
discordId: discordUser.id,
|
||||
email: discordUser.email,
|
||||
avatar: discordUser.avatar,
|
||||
username: discordUser.username,
|
||||
globalName: discordUser.global_name,
|
||||
verified: discordUser.verified,
|
||||
tokenType: authData.token_type,
|
||||
} as DiscordAccount;
|
||||
const discordObject = {
|
||||
userId: session.user.id,
|
||||
accessToken: authData.access_token,
|
||||
refreshToken: authData.refresh_token,
|
||||
discordId: discordUser.id,
|
||||
email: discordUser.email,
|
||||
avatar: discordUser.avatar,
|
||||
username: discordUser.username,
|
||||
globalName: discordUser.global_name,
|
||||
verified: discordUser.verified,
|
||||
tokenType: authData.token_type,
|
||||
} as DiscordAccount;
|
||||
|
||||
const discord = await prisma.discordAccount.upsert({
|
||||
where: { discordId: discordUser.id },
|
||||
update: discordObject, // Updates if found
|
||||
create: discordObject, // Creates if not found
|
||||
});
|
||||
return NextResponse.redirect(
|
||||
`${process.env.NEXTAUTH_URL}/settings/account`
|
||||
);
|
||||
} catch (error: any) {
|
||||
console.error(error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Couldn't connect to Discord",
|
||||
},
|
||||
{
|
||||
status: 500,
|
||||
}
|
||||
);
|
||||
}
|
||||
const discord = await prisma.discordAccount.upsert({
|
||||
where: { discordId: discordUser.id },
|
||||
update: discordObject, // Updates if found
|
||||
create: discordObject, // Creates if not found
|
||||
});
|
||||
return NextResponse.redirect(`${process.env.NEXTAUTH_URL}/settings/account`);
|
||||
} catch (error: any) {
|
||||
console.error(error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Couldn't connect to Discord",
|
||||
},
|
||||
{
|
||||
status: 500,
|
||||
},
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user