From dc3d5b9b3395c1a30b36da0ee221937065e2e678 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 24 Oct 2023 07:47:11 +0100 Subject: [PATCH] add playerId cookie to set where the landing page should be --- package-lock.json | 6 ++++++ package.json | 1 + src/middleware.ts | 11 +++++++++-- src/store/settingsStore.ts | 6 ++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8a79e07..a26bfca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "scoresaber-reloadedv2", "version": "0.1.0", "dependencies": { + "@boiseitguru/cookie-cutter": "^0.2.1", "@heroicons/react": "^2.0.18", "@sentry/nextjs": "^7.74.1", "bluebird": "^3.7.2", @@ -76,6 +77,11 @@ "node": ">=6.9.0" } }, + "node_modules/@boiseitguru/cookie-cutter": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@boiseitguru/cookie-cutter/-/cookie-cutter-0.2.1.tgz", + "integrity": "sha512-MLYTqtyECvjq+Wjq2k++bXReIlClTrtpSWSXeamp868MWKggn6SanFBZRVhI61isf3ETuvmDJObexW36Ck4SCQ==" + }, "node_modules/@eslint-community/eslint-utils": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.4.0.tgz", diff --git a/package.json b/package.json index 7d8bc96..be4b742 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "lint": "next lint" }, "dependencies": { + "@boiseitguru/cookie-cutter": "^0.2.1", "@heroicons/react": "^2.0.18", "@sentry/nextjs": "^7.74.1", "bluebird": "^3.7.2", diff --git a/src/middleware.ts b/src/middleware.ts index dc448b6..5e878a6 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -3,10 +3,17 @@ import { NextResponse } from "next/server"; export function middleware(request: NextRequest) { const { pathname } = request.nextUrl; + const cookies = request.cookies; - // todo: make this redirect to the users profile if they have a profile selected + const playerIdCookie = cookies.get("playerId"); if (pathname == "/") { - return NextResponse.redirect(new URL("/search", request.url)); + if (playerIdCookie) { + return NextResponse.redirect( + new URL(`/player/${playerIdCookie}`, request.url), + ); + } else { + return NextResponse.redirect(new URL("/search", request.url)); + } } if (pathname == "/ranking/global") { diff --git a/src/store/settingsStore.ts b/src/store/settingsStore.ts index 4d4ed3f..f11250e 100644 --- a/src/store/settingsStore.ts +++ b/src/store/settingsStore.ts @@ -9,6 +9,9 @@ import { createJSONStorage, persist } from "zustand/middleware"; import { IDBStorage } from "./IndexedDBStorage"; import { useScoresaberScoresStore } from "./scoresaberScoresStore"; +// it has no typescript support +const cookieCutter = require("@boiseitguru/cookie-cutter"); + interface SettingsStore { player: ScoresaberPlayer | undefined; friends: ScoresaberPlayer[]; @@ -40,6 +43,8 @@ export const useSettingsStore = create()( set({ player: playerData, }); + + cookieCutter.set("playerId", playerData.id); }, async addFriend(friendId: string) { @@ -97,6 +102,7 @@ export const useSettingsStore = create()( if (newPlayer != undefined && newPlayer != null) { console.log("Updated player data for", newPlayer.name); set({ player: newPlayer }); + cookieCutter.set("playerId", newPlayer.id); } }