-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: spotify refresh token flow (#79)
- Loading branch information
1 parent
c0e813e
commit 26c124d
Showing
4 changed files
with
85 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,59 @@ | ||
import { env } from '@/env.mjs'; | ||
import { DatabaseError } from '@/lib/errors'; | ||
import { cookies } from 'next/headers'; | ||
import { ofetch } from 'ofetch'; | ||
|
||
import { fetchAccount } from '../account'; | ||
import { createSupabaseServerClient } from '../supabase/server'; | ||
|
||
type RefreshTokenResponse = { | ||
access_token: string; | ||
expires_in: number; | ||
scope: string; | ||
token_type: string; | ||
}; | ||
|
||
const SPOTIFY_BASE_URL = 'https://api.spotify.com/v1'; | ||
|
||
export const spotifyFetchClient = ofetch.create({ | ||
baseURL: SPOTIFY_BASE_URL, | ||
params: { limit: 50, offset: 0 }, | ||
}); | ||
|
||
export const refreshToken = async (providerRefreshToken: string) => { | ||
const supabase = createSupabaseServerClient(cookies()); | ||
const account = await fetchAccount(); | ||
|
||
const bearer = Buffer.from( | ||
`${env.SPOTIFY_CLIENT_ID}:${env.SPOTIFY_CLIENT_SECRET}`, | ||
).toString('base64'); | ||
|
||
try { | ||
const response = await ofetch<RefreshTokenResponse>( | ||
'https://accounts.spotify.com/api/token', | ||
{ | ||
body: `grant_type=refresh_token&refresh_token=${providerRefreshToken}`, | ||
headers: { | ||
Authorization: `Basic ${bearer}`, | ||
'content-type': 'application/x-www-form-urlencoded', | ||
}, | ||
method: 'post', | ||
}, | ||
); | ||
|
||
const { data, error } = await supabase | ||
.from('account') | ||
.update({ provider_token: response.access_token }) | ||
.eq('id', account.id) | ||
.select() | ||
.single(); | ||
|
||
if (error) { | ||
throw new DatabaseError(error); | ||
} | ||
|
||
return data.provider_token; | ||
} catch (error) { | ||
throw new Error('Could not get new access token'); | ||
} | ||
}; |
This file contains 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
26c124d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deployment failed with the following error: