The weeb for the next gen discord boat - Wamellow wamellow.com
bot discord
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

update spotify login (and actually fix it)

Luna 5131c2bd 39b81e31

+86 -92
+4 -2
app/login/route.ts
··· 91 91 92 92 93 93 const guildId = searchParams.get("guild_id"); 94 - const state = searchParams.get("state") || "/"; 94 + let redirectUrl = decodeURIComponent(searchParams.get("state") || "/"); 95 + 96 + if (redirectUrl.includes("://")) redirectUrl = "/"; 95 97 96 98 redirect( 97 99 guildId 98 100 ? `/dashboard/${guildId}` 99 - : decodeURIComponent(state) 101 + : redirectUrl 100 102 ); 101 103 102 104 }
+30
app/login/spotify/api.ts
··· 1 + import { RouteErrorResponse } from "@/typings"; 2 + 3 + export async function connectSpotify(code: string, session: string): Promise<true | RouteErrorResponse> { 4 + const res = await fetch(`${process.env.NEXT_PUBLIC_API}/users/@me/connections/spotify`, { 5 + method: "PUT", 6 + headers: { 7 + "Content-Type": "application/json", 8 + "Cookie": `session=${session}`, 9 + authorization: process.env.API_SECRET as string 10 + }, 11 + body: JSON.stringify({ 12 + code 13 + }) 14 + }); 15 + 16 + return res.ok ? true : await res.json(); 17 + } 18 + 19 + export async function disconnectSpotify(session: string): Promise<true | RouteErrorResponse> { 20 + const res = await fetch(`${process.env.NEXT_PUBLIC_API}/users/@me/connections/spotify`, { 21 + method: "DELETE", 22 + headers: { 23 + "Content-Type": "application/json", 24 + "Cookie": `session=${session}`, 25 + authorization: process.env.API_SECRET as string 26 + } 27 + }); 28 + 29 + return res.ok ? true : await res.json(); 30 + }
-90
app/login/spotify/page.tsx
··· 1 - "use client"; 2 - import { useRouter } from "next/navigation"; 3 - import { useEffect, useState } from "react"; 4 - 5 - import { User, userStore } from "@/common/user"; 6 - import Modal from "@/components/modal"; 7 - import { ScreenMessage } from "@/components/screen-message"; 8 - import { RouteErrorResponse } from "@/typings"; 9 - 10 - export default function Home() { 11 - const [error, setError] = useState<string>(); 12 - const router = useRouter(); 13 - 14 - useEffect(() => { 15 - 16 - userStore.setState(undefined); 17 - const params = new URLSearchParams(window.location.search); 18 - 19 - if (params.get("logout")) { 20 - 21 - fetch(`${process.env.NEXT_PUBLIC_API}/users/@me/connections/spotify`, { 22 - headers: { 23 - authorization: localStorage.getItem("token") as string 24 - }, 25 - method: "DELETE" 26 - }) 27 - .then(() => { 28 - 29 - const redirect = localStorage.getItem("lastpage"); 30 - window.location.href = redirect || "/"; 31 - 32 - }) 33 - .catch(() => null); 34 - 35 - return; 36 - } 37 - 38 - if (!params.get("code")) window.location.href = `${process.env.NEXT_PUBLIC_API}/connections/spotify`; 39 - else 40 - fetch(`${process.env.NEXT_PUBLIC_API}/users/@me/connections/spotify?${params.toString()}`, { 41 - headers: { 42 - authorization: localStorage.getItem("token") as string 43 - }, 44 - method: "PUT" 45 - }) 46 - .then(async (res) => { 47 - const response = await res.json().catch(() => null) as User; 48 - 49 - switch (res.status) { 50 - case 204: { 51 - const redirect = localStorage.getItem("lastpage"); 52 - router.push((redirect || "/") + "?spotify_login_success=true"); 53 - break; 54 - } 55 - default: { 56 - setError((response as unknown as RouteErrorResponse)?.message || "Error during authorization"); 57 - break; 58 - } 59 - } 60 - 61 - }) 62 - .catch(() => { 63 - setError("Error during authorization"); 64 - }); 65 - }, []); 66 - 67 - return ( 68 - <> 69 - <Modal 70 - title="OAuth2 error" 71 - show={!!error} 72 - buttonName="Try again" 73 - onClose={() => { 74 - setError(undefined); 75 - }} 76 - onSubmit={() => { 77 - router.refresh(); 78 - return undefined; 79 - }} 80 - > 81 - {error} 82 - </Modal> 83 - 84 - <ScreenMessage 85 - title="Authorizing.." 86 - description="We're just checking who you are" 87 - /> 88 - </> 89 - ); 90 - }
+52
app/login/spotify/route.ts
··· 1 + import { cookies } from "next/headers"; 2 + import { redirect } from "next/navigation"; 3 + 4 + import { connectSpotify, disconnectSpotify } from "./api"; 5 + 6 + export async function GET(request: Request) { 7 + const { searchParams } = new URL(request.url); 8 + const cookieStore = cookies(); 9 + 10 + const logout = searchParams.get("logout"); 11 + const session = cookieStore.get("session"); 12 + 13 + if (!session?.value) { 14 + redirect("/login?callback=" + encodeURIComponent(`/login/spotify${logout === "true" ? "?logout=true" : ""}`)); 15 + } 16 + 17 + if (logout) { 18 + 19 + const res = await disconnectSpotify(session.value); 20 + 21 + if ( 22 + (res !== true || typeof res !== "boolean" && "statusCode" in res) && 23 + res?.statusCode !== 401 24 + ) { 25 + const data = { statusCode: 500, message: res?.message || "An error occurred" }; 26 + console.log(data); 27 + return Response.json(data); 28 + } 29 + 30 + redirect("/?spotify_login_success=true"); 31 + } 32 + 33 + const code = searchParams.get("code"); 34 + 35 + if (!code) { 36 + redirect(`${process.env.NEXT_PUBLIC_API}/connections/spotify`); 37 + } 38 + 39 + const res = await connectSpotify(code, session.value); 40 + 41 + if ( 42 + (res !== true || typeof res !== "boolean" && "statusCode" in res) && 43 + res?.statusCode !== 401 44 + ) { 45 + const data = { statusCode: 500, message: res?.message || "An error occurred" }; 46 + console.log(data); 47 + return Response.json(data); 48 + } 49 + 50 + redirect("/?spotify_login_success=true"); 51 + 52 + }