generated from muhtadeetaron/nextjs-template
initial commit
This commit is contained in:
36
torpedo/app/api/auth/[...nextauth]/route.ts
Normal file
36
torpedo/app/api/auth/[...nextauth]/route.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import NextAuth, { NextAuthOptions } from "next-auth";
|
||||
import GoogleProvider from "next-auth/providers/google";
|
||||
import { JWT } from "next-auth/jwt";
|
||||
import { Session } from "next-auth";
|
||||
|
||||
export const authOptions: NextAuthOptions = {
|
||||
providers: [
|
||||
GoogleProvider({
|
||||
clientId: process.env.GOOGLE_CLIENT_ID!,
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
||||
authorization: {
|
||||
params: {
|
||||
scope:
|
||||
"openid profile email https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets.readonly",
|
||||
},
|
||||
},
|
||||
}),
|
||||
],
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
callbacks: {
|
||||
async session({ session, token }: { session: Session; token: JWT }) {
|
||||
session.accessToken = token.accessToken as string;
|
||||
return session;
|
||||
},
|
||||
async jwt({ token, account }: { token: JWT; account: any }) {
|
||||
if (account) {
|
||||
token.accessToken = account.access_token;
|
||||
}
|
||||
return token;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const handler = NextAuth(authOptions);
|
||||
|
||||
export { handler as GET, handler as POST };
|
||||
42
torpedo/app/api/fetchcsv/route.ts
Normal file
42
torpedo/app/api/fetchcsv/route.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { google } from 'googleapis';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { authOptions } from '../auth/[...nextauth]/route';
|
||||
|
||||
async function listCSVFileNames(accessToken: string): Promise<string[]> {
|
||||
const auth = new google.auth.OAuth2();
|
||||
console.log(accessToken)
|
||||
auth.setCredentials({ access_token: accessToken });
|
||||
|
||||
const drive = google.drive({ version: 'v3', auth });
|
||||
|
||||
try {
|
||||
const res = await drive.files.list({
|
||||
q: "mimeType='text/csv'",
|
||||
fields: 'files(name)',
|
||||
});
|
||||
|
||||
const fileNames = res.data.files?.map(file => file.name) || [];
|
||||
|
||||
return fileNames
|
||||
} catch (error) {
|
||||
console.error('Error listing CSV files:', error);
|
||||
throw new Error('Failed to list CSV files');
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const session = await getServerSession({ req: request, authOptions });
|
||||
|
||||
|
||||
if (!session || !session.accessToken) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const fileNames = await listCSVFileNames(session.accessToken);
|
||||
return NextResponse.json({ fileNames });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: error.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user