Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"clsx": "^2.1.1",
"gsap": "^3.12.5",
"input-otp": "^1.2.4",
"jsonwebtoken": "^9.0.2",
"ky": "^1.7.2",
"lucide-react": "^0.456.0",
"next": "14.2.10",
Expand All @@ -46,6 +47,7 @@
"@codedependant/semantic-release-docker": "^5.0.3",
"@eslint/js": "^9.14.0",
"@iconify/react": "^5.0.2",
"@types/jsonwebtoken": "^9.0.7",
"@types/node": "^22.9.0",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.3.0",
Expand Down
99 changes: 99 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/lib/queries/gists.queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import ky from "ky"
import { getBackendURL } from "../utils"
import { Gist } from "@/types"
import { keepPreviousData, useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query"

Check warning on line 5 in src/lib/queries/gists.queries.tsx

View workflow job for this annotation

GitHub Actions / Test linting

'keepPreviousData' is defined but never used
import { useEffect } from "react"
import { useGistStorage } from "../storage/gists"

Check warning on line 7 in src/lib/queries/gists.queries.tsx

View workflow job for this annotation

GitHub Actions / Test linting

'useGistStorage' is defined but never used

//types

Expand Down Expand Up @@ -36,7 +36,7 @@
nb_pages: number
}> => {
const json = await ky
.get(`${getBackendURL()}/gists?offset=${offset}&limit=${limit}`, {
.get(`${getBackendURL()}/gists?offset=${offset}&limit=${limit}&short=true`, {
credentials: "include",
})
.json<GistsWithPaginate>()
Expand Down
29 changes: 29 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { type ClassValue, clsx } from "clsx"
import jwt from "jsonwebtoken"
import { twMerge } from "tailwind-merge"
import { renewToken } from "./queries/auth.queries"

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
Expand All @@ -16,3 +18,30 @@ export function getRawGistURL(id: string) {
}
return getBackendURL() + "/gists/raw/" + id
}

const TOKEN_RENEWAL_THRESHOLD = 2 * 60

export async function tryRenewingToken(token: string) {
const decoded = jwt.decode(token)
if (!decoded) {
return
}

console.log(decoded)

// Check if token is nearing expiration
const currentTime = Math.floor(Date.now() / 1000)
const expiresIn =
(
decoded as {
exp: number
pub: string
email: string
}
).exp - currentTime

if (expiresIn < TOKEN_RENEWAL_THRESHOLD) {
// Renew the token if it is expired or will expire soon
await renewToken()
}
}
5 changes: 5 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { NextRequest } from "next/server"
import { tryRenewingToken } from "./lib/utils"

export function middleware(request: NextRequest) {
const accessToken = request.cookies.get("gists.access_token")?.value
const publicRoutes = ["/", "/login"] // Ajoutez ici d'autres routes publiques si nécessaire

if (accessToken) {
tryRenewingToken(accessToken)
}

if (!accessToken && !publicRoutes.includes(request.nextUrl.pathname)) {
return Response.redirect(new URL("/login", request.url))
}
Expand Down
Loading