I am trying to deploy my application. When I try to build on my local machine it works fine, but when the build runs on GitHub it is not working.
For information, I am using yarn and yarn build to build the project.
According to most of my research, it should not be an issue. Maybe it comes from typescript, or I made a mistake I do not see, or it could be a wrong configuration of the GitHub CI? I do not know...
my code for [...nextauth].ts
import NextAuth, { NextAuthOptions } from "next-auth"
import GoogleProvider from "next-auth/providers/google"
const GOOGLE_AUTHORIZATION_URL =
"https://accounts.google.com/o/oauth2/v2/auth?" +
new URLSearchParams({
prompt: "consent",
access_type: "offline",
response_type: "code",
})
/**
* Takes a token, and returns a new token with updated
* `accessToken` and `accessTokenExpires`. If an error occurs,
* returns the old token and an error property
*/
async function refreshAccessToken(token: any) {
try {
const url =
"https://oauth2.googleapis.com/token?" +
new URLSearchParams({
client_id: process.env.GOOGLE_CLIENT_ID!.toString(),
client_secret: process.env.GOOGLE_CLIENT_SECRET!.toString(),
grant_type: "refresh_token",
refresh_token: token.refreshToken,
})
const response = await fetch(url, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
method: "POST",
})
const refreshedTokens = await response.json()
if (!response.ok) {
throw refreshedTokens
}
return {
...token,
accessToken: refreshedTokens.access_token,
accessTokenExpires: Date.now() + refreshedTokens.expires_at * 1000,
refreshToken: refreshedTokens.refresh_token ?? token.refreshToken, // Fall back to old refresh token
}
} catch (error) {
console.error("Oups", error)
return {
...token,
error: "RefreshAccessTokenError",
}
}
}
// For more information on each option (and a full list of options) go to
// https://next-auth.js.org/configuration/options
export const authOptions: NextAuthOptions = {
// https://next-auth.js.org/configuration/providers/oauth
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
authorization: GOOGLE_AUTHORIZATION_URL,
}),
],
theme: {
colorScheme: "auto", // "auto" | "dark" | "light"
brandColor: "#004821", // Hex color code
logo: "XXX", // Absolute URL to image
},
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async jwt({ token, user, account }) {
// Initial sign in
if (account && user) {
return {
accessToken: account.access_token,
accessTokenExpires: Date.now() + account!.expires_at! * 1000,
refreshToken: account.refresh_token,
user,
}
}
// Return previous token if the access token has not expired yet
if (Date.now() < token.accessTokenExpires!) {
return token
}
// Access token has expired, try to update it
return refreshAccessToken(token)
},
async session({ session, token }) {
session.user = token.user!,
session.accessToken = token.accessToken
session.error = token.error
return session
},
},
}
export default NextAuth(authOptions)
And the error I have is the following:
Failed to compile.
./src/pages/api/auth/[...nextauth].ts:99:15
Type error: Property 'accessToken' does not exist on type 'Session'.
97 | async session({ session, token }) {
98 | session.user = token.user!,
> 99 | session.accessToken = token.accessToken
| ^
100 | session.error = token.error
101 |
102 | return session
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Error: Process completed with exit code 1.
EDIT: So with the different comments I got, I think that the issue was due to an issue with the next auth version.