Skip to content

Commit

Permalink
feat(GIST-100): shortent gists content (#42)
Browse files Browse the repository at this point in the history
* feat(GIST-76): wip

* feat(GIST-100): add short param to fetch all gists
  • Loading branch information
Courtcircuits authored Nov 14, 2024
1 parent 99721c3 commit d198c6e
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
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 @@ -36,7 +36,7 @@ const fetchGists = async ({
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

0 comments on commit d198c6e

Please sign in to comment.