mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 21:21:02 +00:00
feat(api): fetch listens from spotify
This commit is contained in:
parent
f253a66f86
commit
f2065d3f1f
54 changed files with 1180 additions and 256 deletions
|
|
@ -1,22 +1,17 @@
|
|||
import React from "react";
|
||||
import { Route, Router, Switch } from "react-router-dom";
|
||||
import NavBar from "./components/NavBar";
|
||||
import PrivateRoute from "./components/PrivateRoute";
|
||||
import Profile from "./views/Profile";
|
||||
import ExternalApi from "./views/ExternalApi";
|
||||
import history from "./utils/history";
|
||||
import { NavBar } from "./components/NavBar";
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<div className="App">
|
||||
<Router history={history}>
|
||||
<header>
|
||||
<NavBar />
|
||||
<NavBar></NavBar>
|
||||
</header>
|
||||
<Switch>
|
||||
<Route path="/" exact />
|
||||
<PrivateRoute path="/profile" component={Profile} />
|
||||
<PrivateRoute path="/external-api" component={ExternalApi} />
|
||||
</Switch>
|
||||
</Router>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
{
|
||||
"domain": "listory.eu.auth0.com",
|
||||
"clientId": "pYSTOYOsmenpCYj6TsRNQ8dprlqoQTt3",
|
||||
"audience": "https://listory.apricote.de/api/v1"
|
||||
}
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
import React from "react";
|
||||
import { useAuth0 } from "../react-auth0-spa";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
const NavBar = () => {
|
||||
const { isAuthenticated, loginWithRedirect, logout } = useAuth0();
|
||||
|
||||
return (
|
||||
<div>
|
||||
{!isAuthenticated && (
|
||||
<button onClick={() => loginWithRedirect({})}>Log in</button>
|
||||
)}
|
||||
|
||||
{isAuthenticated && <button onClick={() => logout()}>Log out</button>}
|
||||
|
||||
{isAuthenticated && (
|
||||
<span>
|
||||
<Link to="/">Home</Link>
|
||||
<Link to="/profile">Profile</Link>
|
||||
<Link to="/external-api">External API</Link>
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default NavBar;
|
||||
9
frontend/src/components/NavBar.jsx
Normal file
9
frontend/src/components/NavBar.jsx
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import React from "react";
|
||||
|
||||
export function NavBar() {
|
||||
return (
|
||||
<div>
|
||||
<a href="/api/v1/auth/spotify">Login with Spotify</a>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
import React, { useEffect } from "react";
|
||||
import { Route } from "react-router-dom";
|
||||
import { useAuth0 } from "../react-auth0-spa";
|
||||
|
||||
const PrivateRoute = ({ component: Component, path, ...rest }) => {
|
||||
const { loading, isAuthenticated, loginWithRedirect } = useAuth0();
|
||||
|
||||
useEffect(() => {
|
||||
if (loading || isAuthenticated) {
|
||||
return;
|
||||
}
|
||||
const fn = async () => {
|
||||
await loginWithRedirect({
|
||||
appState: { targetUrl: path }
|
||||
});
|
||||
};
|
||||
fn();
|
||||
}, [loading, isAuthenticated, loginWithRedirect, path]);
|
||||
|
||||
const render = props =>
|
||||
isAuthenticated === true ? <Component {...props} /> : null;
|
||||
|
||||
return <Route path={path} render={render} {...rest} />;
|
||||
};
|
||||
|
||||
export default PrivateRoute;
|
||||
|
|
@ -2,31 +2,7 @@ import React from "react";
|
|||
import ReactDOM from "react-dom";
|
||||
import App from "./App";
|
||||
import * as serviceWorker from "./serviceWorker";
|
||||
import { Auth0Provider } from "./react-auth0-spa";
|
||||
import config from "./auth_config.json";
|
||||
import history from "./utils/history";
|
||||
|
||||
// A function that routes the user to the right place
|
||||
// after login
|
||||
const onRedirectCallback = appState => {
|
||||
history.push(
|
||||
appState && appState.targetUrl
|
||||
? appState.targetUrl
|
||||
: window.location.pathname
|
||||
);
|
||||
};
|
||||
|
||||
ReactDOM.render(
|
||||
<Auth0Provider
|
||||
domain={config.domain}
|
||||
client_id={config.clientId}
|
||||
redirect_uri={window.location.origin}
|
||||
audience={config.audience}
|
||||
onRedirectCallback={onRedirectCallback}
|
||||
>
|
||||
<App />
|
||||
</Auth0Provider>,
|
||||
document.getElementById("root")
|
||||
);
|
||||
ReactDOM.render(<App />, document.getElementById("root"));
|
||||
|
||||
serviceWorker.unregister();
|
||||
|
|
|
|||
89
frontend/src/react-auth0-spa.js
vendored
89
frontend/src/react-auth0-spa.js
vendored
|
|
@ -1,89 +0,0 @@
|
|||
import React, { useState, useEffect, useContext } from "react";
|
||||
import createAuth0Client from "@auth0/auth0-spa-js";
|
||||
|
||||
const DEFAULT_REDIRECT_CALLBACK = () =>
|
||||
window.history.replaceState({}, document.title, window.location.pathname);
|
||||
|
||||
export const Auth0Context = React.createContext();
|
||||
export const useAuth0 = () => useContext(Auth0Context);
|
||||
export const Auth0Provider = ({
|
||||
children,
|
||||
onRedirectCallback = DEFAULT_REDIRECT_CALLBACK,
|
||||
...initOptions
|
||||
}) => {
|
||||
const [isAuthenticated, setIsAuthenticated] = useState();
|
||||
const [user, setUser] = useState();
|
||||
const [auth0Client, setAuth0] = useState();
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [popupOpen, setPopupOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const initAuth0 = async () => {
|
||||
const auth0FromHook = await createAuth0Client(initOptions);
|
||||
setAuth0(auth0FromHook);
|
||||
|
||||
if (
|
||||
window.location.search.includes("code=") &&
|
||||
window.location.search.includes("state=")
|
||||
) {
|
||||
const { appState } = await auth0FromHook.handleRedirectCallback();
|
||||
onRedirectCallback(appState);
|
||||
}
|
||||
|
||||
const isAuthenticated = await auth0FromHook.isAuthenticated();
|
||||
|
||||
setIsAuthenticated(isAuthenticated);
|
||||
|
||||
if (isAuthenticated) {
|
||||
const user = await auth0FromHook.getUser();
|
||||
setUser(user);
|
||||
}
|
||||
|
||||
setLoading(false);
|
||||
};
|
||||
initAuth0();
|
||||
// eslint-disable-next-line
|
||||
}, []);
|
||||
|
||||
const loginWithPopup = async (params = {}) => {
|
||||
setPopupOpen(true);
|
||||
try {
|
||||
await auth0Client.loginWithPopup(params);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
setPopupOpen(false);
|
||||
}
|
||||
const user = await auth0Client.getUser();
|
||||
setUser(user);
|
||||
setIsAuthenticated(true);
|
||||
};
|
||||
|
||||
const handleRedirectCallback = async () => {
|
||||
setLoading(true);
|
||||
await auth0Client.handleRedirectCallback();
|
||||
const user = await auth0Client.getUser();
|
||||
setLoading(false);
|
||||
setIsAuthenticated(true);
|
||||
setUser(user);
|
||||
};
|
||||
return (
|
||||
<Auth0Context.Provider
|
||||
value={{
|
||||
isAuthenticated,
|
||||
user,
|
||||
loading,
|
||||
popupOpen,
|
||||
loginWithPopup,
|
||||
handleRedirectCallback,
|
||||
getIdTokenClaims: (...p) => auth0Client.getIdTokenClaims(...p),
|
||||
loginWithRedirect: (...p) => auth0Client.loginWithRedirect(...p),
|
||||
getTokenSilently: (...p) => auth0Client.getTokenSilently(...p),
|
||||
getTokenWithPopup: (...p) => auth0Client.getTokenWithPopup(...p),
|
||||
logout: (...p) => auth0Client.logout(...p)
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</Auth0Context.Provider>
|
||||
);
|
||||
};
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
import React, { useState } from "react";
|
||||
import { useAuth0 } from "../react-auth0-spa";
|
||||
|
||||
const ExternalApi = () => {
|
||||
const [showResult, setShowResult] = useState(false);
|
||||
const [apiMessage, setApiMessage] = useState("");
|
||||
const { getTokenSilently } = useAuth0();
|
||||
|
||||
const callApi = async () => {
|
||||
try {
|
||||
const token = await getTokenSilently();
|
||||
|
||||
const response = await fetch("/api/v1/connections", {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
const responseData = await response.json();
|
||||
|
||||
setShowResult(true);
|
||||
setApiMessage(responseData);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<h1>External API</h1>
|
||||
<button onClick={callApi}>Ping API</button>
|
||||
{showResult && <code>{JSON.stringify(apiMessage, null, 2)}</code>}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default ExternalApi;
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
import React, { Fragment } from "react";
|
||||
import { useAuth0 } from "../react-auth0-spa";
|
||||
|
||||
const Profile = () => {
|
||||
const { loading, user } = useAuth0();
|
||||
|
||||
if (loading || !user) {
|
||||
return <div>Loading...</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<Fragment>
|
||||
<img src={user.picture} alt="Profile" />
|
||||
|
||||
<h2>{user.name}</h2>
|
||||
<p>{user.email}</p>
|
||||
<code>{JSON.stringify(user, null, 2)}</code>
|
||||
</Fragment>
|
||||
);
|
||||
};
|
||||
|
||||
export default Profile;
|
||||
Loading…
Add table
Add a link
Reference in a new issue