2020-05-02 03:03:19 +02:00
|
|
|
import React, { createContext, useContext, useEffect, useState } from "react";
|
|
|
|
|
import { getUsersMe, UnauthenticatedError } from "../api/api";
|
2020-05-02 21:46:41 +02:00
|
|
|
import { User } from "../api/entities/user";
|
2020-05-02 03:03:19 +02:00
|
|
|
|
|
|
|
|
const authContext = createContext<AuthContext>(
|
|
|
|
|
(undefined as any) as AuthContext
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Provider component that wraps your app and makes auth object ...
|
|
|
|
|
// ... available to any child component that calls useAuth().
|
|
|
|
|
|
|
|
|
|
interface AuthContext {
|
|
|
|
|
user: { id: string; displayName: string } | null;
|
|
|
|
|
isLoaded: boolean;
|
|
|
|
|
loginWithSpotifyProps: () => { href: string };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const ProvideAuth: React.FC = ({ children }) => {
|
|
|
|
|
const auth = useProvideAuth();
|
|
|
|
|
|
|
|
|
|
return <authContext.Provider value={auth}>{children}</authContext.Provider>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function useAuth() {
|
|
|
|
|
return useContext(authContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function useProvideAuth(): AuthContext {
|
|
|
|
|
const [isLoaded, setIsLoaded] = useState(false);
|
|
|
|
|
const [user, setUser] = useState<User | null>(null);
|
|
|
|
|
|
|
|
|
|
const loginWithSpotifyProps = () => ({ href: "/api/v1/auth/spotify" });
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
(async () => {
|
|
|
|
|
try {
|
|
|
|
|
const currentUser = await getUsersMe();
|
|
|
|
|
setUser(currentUser);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof UnauthenticatedError) {
|
|
|
|
|
// User is not logged in
|
|
|
|
|
} else {
|
|
|
|
|
console.error("Error while checking login state:", err);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
setIsLoaded(true);
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return { isLoaded, user, loginWithSpotifyProps };
|
|
|
|
|
}
|