diff --git a/package.json b/package.json index 26bc52e..b4fb6a5 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "axios-hooks": "^5.1.0", "framer-motion": "^11.15.0", "jsonwebtoken": "^9.0.2", - "moment": "^2.30.1", + "moment-timezone": "^0.5.46", "next": "15.0.3", "next-auth": "^4.24.10", "next-themes": "^0.4.4", diff --git a/src/authOptions.ts b/src/authOptions.ts index 45b760e..26e67d6 100644 --- a/src/authOptions.ts +++ b/src/authOptions.ts @@ -1,11 +1,11 @@ -import axios from "axios"; -import moment from "moment"; +import axios, { AxiosResponse } from "axios"; +import moment from "moment-timezone"; import { AuthOptions, Session } from "next-auth"; import { JWT } from "next-auth/jwt"; import jsonwebtoken from "jsonwebtoken"; import { JWTDecoded } from "./app/types/next-auth"; -moment.locale("fr"); +moment.tz.setDefault("America/New_York"); export const authOptions: AuthOptions = { providers: [ @@ -41,6 +41,7 @@ export const authOptions: AuthOptions = { }, }, ], + debug: process.env.NODE_ENV === "development", callbacks: { async jwt({ token, account, user: profile }) { // Initial sign in @@ -67,10 +68,6 @@ export const authOptions: AuthOptions = { return token; } - // Return previous token if the access token has not expired yet - if (moment().isBefore(moment(token.accessTokenExpires))) - return token; - if ( token.refreshTokenExpires && moment().isAfter(token.refreshTokenExpires) @@ -80,6 +77,10 @@ export const authOptions: AuthOptions = { error: "Refresh token has expired", }; + // Return previous token if the access token has not expired yet + if (moment().isBefore(moment(token.accessTokenExpires))) + return token; + // Access token has expired, try to refresh it return refreshAccessToken(token); }, @@ -140,7 +141,11 @@ const refreshAccessToken = async (token: JWT): Promise => { .subtract(5, "s"), refreshToken: response.data.refresh_token, }; - } catch { + } catch (e) { + console.error( + "Error refreshing access token", + (e as AxiosResponse).data, + ); return { ...token, error: "RefreshAccessTokenError", diff --git a/yarn.lock b/yarn.lock index 89211b0..c3a4fa9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4113,7 +4113,14 @@ minimist@^1.2.0, minimist@^1.2.6: resolved "https://registry.yarnpkg.com/minipass/-/minipass-7.1.2.tgz#93a9626ce5e5e66bd4db86849e7515e92340a707" integrity sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw== -moment@^2.30.1: +moment-timezone@^0.5.46: + version "0.5.46" + resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.46.tgz#a21aa6392b3c6b3ed916cd5e95858a28d893704a" + integrity sha512-ZXm9b36esbe7OmdABqIWJuBBiLLwAjrN7CE+7sYdCCx82Nabt1wHDj8TVseS59QIlfFPbOoiBPm6ca9BioG4hw== + dependencies: + moment "^2.29.4" + +moment@^2.29.4: version "2.30.1" resolved "https://registry.yarnpkg.com/moment/-/moment-2.30.1.tgz#f8c91c07b7a786e30c59926df530b4eac96974ae" integrity sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==