Skip to content

Commit

Permalink
fix refresh token not persisting
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoMadera committed Apr 8, 2024
1 parent c8ec623 commit e615df1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
10 changes: 7 additions & 3 deletions pages/api/spotify-refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ export default async function refresh(
NEXT_PUBLIC_SPOTIFY_CLIENT_ID: client_id = "",
SPOTIFY_CLIENT_SECRET: client_secret = "",
} = process.env;
const body = req.body as IRefreshBody;
const context = { req, res };
const refreshTokenFromCookie = takeCookie(REFRESH_TOKEN_COOKIE, context);
const body = req.body as IRefreshBody;
const refreshToken = body.refreshToken ?? refreshTokenFromCookie;

try {
if (!body.refreshToken && !refreshTokenFromCookie) {
if (!refreshToken) {
throw new ApiError(400, "Bad Request");
}

Expand Down Expand Up @@ -76,12 +78,14 @@ export default async function refresh(
age: expireCookieDate.getTime(),
context,
});

makeCookie({
name: REFRESH_TOKEN_COOKIE,
value: body.refreshToken ?? refreshTokenFromCookie ?? "",
value: refreshToken,
age: expireCookieDate.getTime(),
context,
});

makeCookie({
name: EXPIRE_TOKEN_COOKIE,
value: data.expires_in.toString(),
Expand Down
5 changes: 0 additions & 5 deletions utils/spotifyCalls/refreshAccessToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ export async function refreshAccessToken(
const data = await handleJsonResponse<IRefreshAccessTokenResponse>(response);

if (data) {
makeCookie({
name: REFRESH_TOKEN_COOKIE,
value: data.refresh_token,
context,
});
makeCookie({
name: ACCESS_TOKEN_COOKIE,
value: data.access_token,
Expand Down

0 comments on commit e615df1

Please sign in to comment.