Skip to content

Commit

Permalink
I pull User Profile for Spotify and toast name to welcome player.
Browse files Browse the repository at this point in the history
  • Loading branch information
sweetmantech committed Nov 11, 2023
1 parent 5dd2b76 commit 50b7ef7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/spotify/getUser.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const getCurrentUserProfile = async (accessToken) => {
const response = await fetch(`https://api.spotify.com/v1/me`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
})

if (!response.ok) {
throw new Error(`Error: ${response.status}`)
}

const data = await response.json()

return data
}

export default getCurrentUserProfile
10 changes: 9 additions & 1 deletion providers/SpotifyProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import saveTrack from "../lib/spotify/saveTrack"
import getAccessToken from "../lib/spotify/getAccessToken"
import createPlayer from "../lib/spotify/createPlayer"
import login from "../lib/spotify/login"
import getCurrentUserProfile from "../lib/spotify/getUser"
import { toast } from "react-toastify"

const SpotifyContext = createContext(null)

Expand All @@ -25,8 +27,14 @@ const SpotifyProvider = ({ children }) => {
}

useEffect(() => {
const init = async () => {
const response = await getCurrentUserProfile(accessToken)
toast.success(`Welcome ${response.display_name}`)
console.log("SWEETS RESPONSE", response)
createPlayer(accessToken, { onReady })
}
if (!accessToken) return
createPlayer(accessToken, { onReady })
init()
}, [accessToken])

useEffect(() => {
Expand Down

0 comments on commit 50b7ef7

Please sign in to comment.