Skip to content

Commit 4231262

Browse files
committed
Improve panel auth handling
You are now forced to log in again if your token expires
1 parent f2048ff commit 4231262

3 files changed

Lines changed: 39 additions & 13 deletions

File tree

panel/src/app/(authenticated)/template.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export default function Template({ children }: { children: React.ReactNode }) {
1313
useEffect(() => {
1414
if (!auth) {
1515
const a = readAuthData()
16-
if (!a) {
17-
router.push("/auth/login")
18-
} else {
16+
if (a && a.isValid()) {
1917
setAuth(a)
18+
} else {
19+
router.push("/auth/login")
2020
}
2121
}
2222
}, [auth])

panel/src/app/auth/callback/page.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useEffect, useState } from "react";
44
import { getRedirectUrl } from "../auth";
55
import { getToken } from "./server_handler";
6-
import { LOCALSTORAGE_KEY } from "@/auth_context";
6+
import { ModfestAuth } from "@/auth_context";
77

88
export default function Home() {
99
const [failed, setFailed] = useState(false)
@@ -34,14 +34,14 @@ async function authWithModrinthOAuth(setFailed: (v: boolean) => void) {
3434
const urlParams = new URLSearchParams(window.location.search);
3535
const code = urlParams.get("code")
3636

37-
var token = await getToken(code!, getRedirectUrl())
38-
if (!("access_token" in token)) {
39-
// Fail!
37+
var modrinthToken = await getToken(code!, getRedirectUrl())
38+
if (!("access_token" in modrinthToken) || !("expires_in" in modrinthToken)) {
4039
setFailed(true)
4140
return
4241
}
4342

44-
localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(token))
43+
const token = new ModfestAuth(modrinthToken["access_token"], modrinthToken["expires_in"])
44+
token.saveLocalStorage()
4545

4646
// TODO redirect anywhere on site
4747
window.location.replace(window.location.origin)

panel/src/auth_context.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
"use client"
22

3-
export const LOCALSTORAGE_KEY = "mr-token"
3+
const LOCALSTORAGE_KEY = "mr-token"
44

55
export class ModfestAuth {
6+
/**
7+
* A modrinth token, which platform accepts as a valid means of authentication
8+
*/
69
private mrToken: string
10+
/**
11+
* Unix timestamp
12+
*/
13+
private validUntil: number
714

8-
private constructor(mrToken: string) {
15+
public constructor(mrToken: string, validUntil: number) {
916
this.mrToken = mrToken
17+
this.validUntil = validUntil
1018
}
1119

12-
static readAuthData(): ModfestAuth | undefined {
20+
static readLocalStorage(): ModfestAuth | undefined {
1321
const token = localStorage.getItem(LOCALSTORAGE_KEY)
1422
if (token == null) {
1523
return undefined
@@ -19,8 +27,26 @@ export class ModfestAuth {
1927
if (typeof mrToken !== "string") {
2028
return undefined
2129
}
30+
var validUntil = tokenData["valid_until"]
31+
if (typeof validUntil !== "number") {
32+
return undefined
33+
}
2234

23-
return new ModfestAuth(mrToken)
35+
return new ModfestAuth(mrToken, validUntil)
36+
}
37+
38+
public saveLocalStorage() {
39+
localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify({
40+
"access_token": this.mrToken,
41+
"valid_until": this.validUntil,
42+
}));
43+
}
44+
45+
/**
46+
* The modfest auth data
47+
*/
48+
public isValid(): boolean {
49+
return this.validUntil > Date.now()
2450
}
2551

2652
public configureFetch(): {headers: Record<string, string>} {
@@ -37,5 +63,5 @@ export function logout() {
3763
}
3864

3965
export function readAuthData(): ModfestAuth | undefined {
40-
return ModfestAuth.readAuthData()
66+
return ModfestAuth.readLocalStorage()
4167
}

0 commit comments

Comments
 (0)