Created
October 11, 2023 14:53
-
-
Save eyenalxai/925f5f97e57aa727e2937f2525847c42 to your computer and use it in GitHub Desktop.
OAUTH_CALLBACK_ERROR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { saveUser } from '@/lib/fetch/user' | |
import { NextAuthOptions } from 'next-auth' | |
import Spotify from 'next-auth/providers/spotify' | |
export const authOptions: NextAuthOptions = { | |
providers: [ | |
Spotify({ | |
clientId: process.env.SPOTIFY_CLIENT_ID!, | |
clientSecret: process.env.SPOTIFY_CLIENT_SECRET! | |
}) | |
], | |
callbacks: { | |
signIn: async ({ user, account }) => { | |
if (!account) { | |
console.log(`No account found for ${user.email}`) | |
return false | |
} | |
if (account.provider === 'spotify') { | |
const userPayload = { | |
spotifyUserId: account.providerAccountId, | |
displayName: user.name, | |
email: user.email, | |
avatarUrl: user.image, | |
accessToken: account.access_token, | |
refreshToken: account.refresh_token | |
} | |
const jwt = await saveUser(userPayload) | |
user.accessToken = jwt.value | |
console.log('User saved successfully') | |
return true | |
} | |
console.log(`No handler for ${account.provider}`) | |
return false | |
}, | |
jwt: async ({ token, user, account }) => { | |
if (user?.accessToken) { | |
token = { accessToken: user.accessToken } | |
console.log(`User ${user.name} has a valid access token`) | |
return token | |
} | |
console.log(`User ${user?.name} does not have a valid access token`) | |
return token | |
}, | |
session: async ({ session, token }) => { | |
try { | |
console.log(`Session for ${session.user.name} has been updated`) | |
session.user.accessToken = token.accessToken | |
console.log(`User ${session.user.name} has a valid access token`) | |
return session | |
} catch (e) { | |
console.log(`Caught error: ${e}`) | |
} | |
return session | |
} | |
}, | |
cookies: { | |
csrfToken: { | |
name: 'next-auth.csrf-token', | |
options: { | |
httpOnly: true, | |
sameSite: 'none', | |
path: '/', | |
secure: true | |
} | |
}, | |
pkceCodeVerifier: { | |
name: 'next-auth.pkce.code_verifier', | |
options: { | |
httpOnly: true, | |
sameSite: 'none', | |
path: '/', | |
secure: true | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment