Added login to RadioTower
This commit is contained in:
parent
fb355414ab
commit
50f9d7ae4f
7 changed files with 67 additions and 47 deletions
|
@ -14,7 +14,7 @@ function App() {
|
||||||
<Route path="/" element={<Home />} />
|
<Route path="/" element={<Home />} />
|
||||||
<Route path="/create-station" element={<CreateStation />} />
|
<Route path="/create-station" element={<CreateStation />} />
|
||||||
<Route path="/join-station" element={<JoinStation />} />
|
<Route path="/join-station" element={<JoinStation />} />
|
||||||
<Route path="/station/:id/:user_name" element={<StationPage />} />
|
<Route path="/station/:id" element={<StationPage />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</Router>
|
</Router>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -3,3 +3,6 @@ export const RADIOSTATION_URL = "http://localhost:8080/api";
|
||||||
export const CREATE_RADIOSTATION_ENDPOINT = "/radio-stations";
|
export const CREATE_RADIOSTATION_ENDPOINT = "/radio-stations";
|
||||||
export const LIST_RADIOSTATIONS_ENDPOINT = "/radio-stations";
|
export const LIST_RADIOSTATIONS_ENDPOINT = "/radio-stations";
|
||||||
export const ADD_CLIENT_ENDPOINT = "/clients/connect";
|
export const ADD_CLIENT_ENDPOINT = "/clients/connect";
|
||||||
|
|
||||||
|
export const SPOTIFY_PREMIUM_TOKEN =
|
||||||
|
"BQCEnWrYVdmta4Eekdkewazun99IuocRAyPEbwPSrHuGYgZYg7JGlXG2BLmRL6fwjzPJkrmHiqlTLSn1yqR36awA9Rv4n9dwvTBv1DjAitsuzaEVg7PtYdbUHXqP2HJJ4dDDvTtvUfWIBDY_Afa7WgY1cyRbl-p4VobNHXUR9N3Ye1qBTgH3RZ5ziIbIoNWe_JrxYvcedkvr23zXUVabOyahTgt_YdmnCWt2Iu8XT8sjhSyc8tOCqbs_KqE-Qe1WSPUCrGS8";
|
||||||
|
|
|
@ -6,9 +6,10 @@ import { createStation } from "../utils/StationsCreate";
|
||||||
function CreateStation() {
|
function CreateStation() {
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
const [description, setDescription] = useState("");
|
const [description, setDescription] = useState("");
|
||||||
|
const [joinCode, setJoinCode] = useState("");
|
||||||
|
|
||||||
const handleCreateStation = () => {
|
const handleCreateStation = () => {
|
||||||
createStation(name, description);
|
setJoinCode(createStation(name, description));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -28,6 +29,8 @@ function CreateStation() {
|
||||||
>
|
>
|
||||||
Create Radio Station
|
Create Radio Station
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
<p>your joinCode: {joinCode}</p>
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,33 +1,35 @@
|
||||||
import React from 'react';
|
import React from "react";
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
function Home() {
|
function Home() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const handleCreateStation = () => {
|
const handleCreateStation = () => {
|
||||||
navigate('/create-station');
|
navigate("/create-station");
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleJoinStation = () => {
|
const handleJoinStation = () => {
|
||||||
navigate('/join-station');
|
navigate("/join-station");
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="home-container">
|
<div className="home-container">
|
||||||
<div className="content">
|
<div className="content">
|
||||||
<h1 className="title">Radio Station Hub</h1>
|
<h1 className="title">Radio Station Hub</h1>
|
||||||
<p className="subtitle">Create or join a radio station to share music with friends</p>
|
<p className="subtitle">
|
||||||
|
Create or join a radio station to share music with friends
|
||||||
|
</p>
|
||||||
|
|
||||||
<div className="button-container">
|
<div className="button-container">
|
||||||
<button
|
<button
|
||||||
className="action-button primary"
|
className="action-button primary"
|
||||||
onClick={handleCreateStation}
|
onClick={handleCreateStation}
|
||||||
>
|
>
|
||||||
Create Radio Station
|
Create Radio Station
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
className="action-button secondary"
|
className="action-button secondary"
|
||||||
onClick={handleJoinStation}
|
onClick={handleJoinStation}
|
||||||
>
|
>
|
||||||
Join Radio Station
|
Join Radio Station
|
||||||
|
|
|
@ -5,18 +5,16 @@ import { getStations } from "../utils/GetStations";
|
||||||
function JoinStation() {
|
function JoinStation() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [stations, setStations] = useState([]);
|
const [stations, setStations] = useState([]);
|
||||||
const [displayName, setDisplayName] = useState("");
|
|
||||||
|
|
||||||
const handleJoinStation = (stationID) => {
|
const handleJoinStation = (stationID) => {
|
||||||
// console.log("Joining station with ID:" + stationID);
|
// console.log("Joining station with ID:" + stationID);
|
||||||
if (!displayName) return; // USER HAS TO HAVE DISPLAY NAME
|
|
||||||
|
|
||||||
navigate(`/station/${stationID}/${displayName}`);
|
navigate(`/station/${stationID}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchStations() {
|
async function fetchStations() {
|
||||||
const result = await getStations(); // assuming getStations is async
|
const result = await getStations();
|
||||||
setStations(result);
|
setStations(result);
|
||||||
}
|
}
|
||||||
fetchStations();
|
fetchStations();
|
||||||
|
@ -29,7 +27,6 @@ function JoinStation() {
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main className="join-station-content">
|
<main className="join-station-content">
|
||||||
<textarea onChange={(e) => setDisplayName(e.target.value)} />
|
|
||||||
{stations.map((station, index) => {
|
{stations.map((station, index) => {
|
||||||
return (
|
return (
|
||||||
<div key={index}>
|
<div key={index}>
|
||||||
|
|
|
@ -5,28 +5,33 @@ import LoginButton from "../components/LoginButton";
|
||||||
import { addClientToStation } from "../utils/AddClientToStation";
|
import { addClientToStation } from "../utils/AddClientToStation";
|
||||||
|
|
||||||
function StationPage() {
|
function StationPage() {
|
||||||
const { joinCode, username } = useParams();
|
const { stationID } = useParams();
|
||||||
const [clientToken, setClientToken] = useState("");
|
const [clientToken, setClientToken] = useState("");
|
||||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||||
const [accessDenied, setAccessDenied] = useState(true);
|
const [accessDenied, setAccessDenied] = useState(true);
|
||||||
|
|
||||||
useEffect(() => {
|
const [userName, setUserName] = useState("");
|
||||||
const auth = async () => {
|
const [joinCode, setJoinCode] = useState("");
|
||||||
try {
|
|
||||||
const token = await addClientToStation(username, joinCode);
|
|
||||||
setClientToken(token);
|
|
||||||
setAccessDenied(false);
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Auth failed:", error);
|
|
||||||
setAccessDenied(true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
auth();
|
const auth = async () => {
|
||||||
}, [joinCode, username]);
|
try {
|
||||||
|
const token = await addClientToStation(userName, joinCode);
|
||||||
|
setClientToken(token);
|
||||||
|
setAccessDenied(false);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Auth failed:", error);
|
||||||
|
setAccessDenied(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (accessDenied) {
|
if (accessDenied) {
|
||||||
return <p>Waiting for authentication</p>;
|
return (
|
||||||
|
<div>
|
||||||
|
<textarea onChange={(e) => setUserName(e.target.value)} />
|
||||||
|
<textarea onChange={(e) => setJoinCode(e.target.value)} />
|
||||||
|
<button onClick={auth}>Access RadioTower</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const recommendedSongs = [];
|
const recommendedSongs = [];
|
||||||
|
|
|
@ -11,18 +11,28 @@ export async function createStation(name, description) {
|
||||||
description: description,
|
description: description,
|
||||||
};
|
};
|
||||||
|
|
||||||
fetch(RADIOSTATION_URL + CREATE_RADIOSTATION_ENDPOINT, {
|
try {
|
||||||
method: "POST",
|
const response = await fetch(
|
||||||
headers: {
|
RADIOSTATION_URL + CREATE_RADIOSTATION_ENDPOINT,
|
||||||
"Content-Type": "application/json",
|
{
|
||||||
},
|
method: "POST",
|
||||||
body: JSON.stringify(body),
|
headers: {
|
||||||
})
|
"Content-Type": "application/json",
|
||||||
.then((response) => response.json())
|
},
|
||||||
.then((data) => {
|
body: JSON.stringify(body),
|
||||||
console.log("Station created:", data);
|
}
|
||||||
})
|
);
|
||||||
.catch((error) => {
|
|
||||||
console.error("Error creating station:", error);
|
const data = await response.json();
|
||||||
});
|
|
||||||
|
if (!response.ok || !data.success) {
|
||||||
|
console.error("Failed to create station:", data.message);
|
||||||
|
throw new Error(data.message || "Unknown error");
|
||||||
|
}
|
||||||
|
|
||||||
|
return data.data.station.joinCode;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error connecting client to station:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue