Next.js Discord

Discord Forum

NextAuth - where do I find the token?

Unanswered
Grand Griffon Vendéen posted this in #help-forum
Open in Discord
Grand Griffon VendéenOP
I'm using nextauth for google oauth2. I can successfully sign in. But I want to read/write files in the accounts google drive, so I need access to the token. How do I get the token after nextauth google sign in and consent screen?

2 Replies

Grand Griffon VendéenOP
[...nextauth]/route.tsx
import NextAuth from "next-auth"
import type { NextAuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google"

const authOptions: NextAuthOptions = {
    secret: process.env.NEXTAUTH_SECRET,
    providers: [
        GoogleProvider({
            clientId: process.env.GOOGLE_ID as string,
            clientSecret: process.env.GOOGLE_SECRET as string,
        }),
    ],
    pages: {
        signIn: "/"
    }
    }

const handler = NextAuth(authOptions)

export { handler as GET, handler as POST };
Code to create file in google drive doesn't work because I'm not passing in the token
'use server'
import { drive as googleDrive, auth } from '@googleapis/drive'

export async function createFile() {
    const client = new auth.OAuth2(
        process.env.GOOGLE_ID as string,
        process.env.GOOGLE_SECRET as string,
        "http://localhost:3000/api/auth/callback/google",
    );

    const drive = googleDrive(
        {
            version: "v3",
            auth: client
        }
    )

    const newFile = drive.files.create({
        requestBody: {
            name: "My first Docs file!!!!",
            mimeType: 'text/plain'
        },
        media: {
            mimeType: 'text/plain',
            body: "Some Docs content :)"
        }
    })
}