'Spotify with NextAuth doesn't return profile picture
I'm using NextAuth v4 and after authenticating with Spotify, I don't have access to the user's profile picture. Here's the information I get from Spotify:
{
name: '***',
email: '***',
sub: '***',
accessToken: '***',
refreshToken: '***',
username: '***',
accessTokenExpires: ***,
iat: ***,
exp: ***,
jti: '***'
}
And here's my code in /pages/api/auth/[...nextauth].js:
import NextAuth from 'next-auth/next'
import SpotifyProvider from 'next-auth/providers/spotify'
import spotifyApi, { LOGIN_URL } from '../../../lib/spotify'
async function refreshAccessToken(token) {
try {
spotifyApi.setAccessToken(token.accessToken)
spotifyApi.setRefreshToken(token.refreshToken)
const { body: refreshedToken } = await spotifyApi.refreshAccessToken()
console.log(`REFRESH TOKEN IS: ${refreshedToken}...`)
return {
...token,
accessToken: refreshedToken.access_token,
accessTokenExpires: Date.now + refreshedToken.expires_in + 1000, // = 1 hour as 3600 returns from spotify API
refreshToken: refreshedToken.refresh_token ?? token.refreshToken, // = the refresh token it never ends whether the company doesn't modify it or stop it=
}
} catch (error) {
console.log(error)
return {
...token,
error: 'RefreshAccessTokenError',
}
}
}
export default NextAuth({
// Configure one or more authentication providers (step 1)
providers: [
SpotifyProvider({
clientId: process.env.NEXT_PUBLIC_CLIENT_ID,
clientSecret: process.env.NEXT_PUBLIC_CLIENT_SECRET,
authorization: LOGIN_URL,
}),
],
secret: process.env.JWT_SECRET,
pages: {
signIn: '/login',
},
callbacks: {
async jwt({ token, account, user }) {
// initial sign in
if (account && user) {
return {
...token,
accessToken: account.access_token,
refreshToken: account.refresh_token,
username: account.providerAccountId,
// we are handling expiry times in Miliseconds hence * 1000
accessTokenExpires: account.expires_at * 1000,
}
}
// refresh token
// Return previous token if the access has not expired yet
if (Date.now() < token.accessTokenExpires) {
console.log('EXISTING ACCESS TOKEN IS VALID')
return token
}
// Access token has expired, so we have to refresh it...
console.log('ACCESS TOKEN HAS EXPIRED, REFRESHING...')
return await refreshAccessToken(token)
},
async session({ session, token }) {
session.user.accessToken = token.accessToken
session.user.refreshToken = token.refreshToken
session.user.username = token.username
console.log('Async session callback', token)
return session
},
},
})
Do I need to do anything to get the user's profile picture, or does Spotify not return it? I've done some research and couldn't find this issue anywhere.
Thanks, DillonB07
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
