Skip to content

Commit

Permalink
create api for refreshing spotify access token
Browse files Browse the repository at this point in the history
  • Loading branch information
IkeHunter committed Sep 20, 2024
1 parent e277f44 commit cd11730
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docker-compose.network.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ services:
restart: always
build:
context: .
dockerfile: ./server.Dockerfile
# dockerfile: ./server/Dockerfile
# dockerfile: ./server.Dockerfile
dockerfile: ./server/Dockerfile
container_name: server-jbx
environment:
- NODE_ENV=network
Expand Down
3 changes: 2 additions & 1 deletion proxy/default.conf.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ server {
proxy_set_header X-Forwarded-For "$proxy_add_x_forwarded_for";
proxy_set_header Token "$http_token";
proxy_pass_header Token;
proxy_pass_header Token;
proxy_pass_header Authorization;
}

location /socket.io {
Expand Down
9 changes: 8 additions & 1 deletion server/controllers/groupController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ export const assignSpotifyToGroup = async (
return group
}

export const getGroupSpotifyAuth = async (groupId) => {
export const getGroupSpotifyAuth = async (groupId: string) => {
const group = await getOrError(groupId, Group)
const auth = await getOrError(group.spotifyAuthId?.toString() ?? '', SpotifyAuth)

if (auth.isExpired()) {
const spotify = await SpotifyService.connect(auth.spotifyEmail)
const updatedAccessToken = await spotify.getAccessToken()
auth.accessToken = updatedAccessToken
auth.save()
}

return auth
}

Expand Down
2 changes: 1 addition & 1 deletion server/docs/swagger_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"title": "Jukebox API",
"description": "Documentation automatically generated by the <b>swagger-autogen</b> module."
},
"host": "localhost:8000",
"host": "localhost:8080",
"basePath": "/",
"tags": [
{
Expand Down
10 changes: 9 additions & 1 deletion server/models/spotifyAuthModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export interface ISpotifyAuthFields extends Omit<ISpotifyAuth, 'id' | 'userId'>
userId: typeof Types.ObjectId
}

export interface ISpotifyAuthMethods extends IModelMethods<ISpotifyAuth> {}
export interface ISpotifyAuthMethods extends IModelMethods<ISpotifyAuth> {
isExpired: () => boolean
}

export type ISpotifyAuthModel = Model<ISpotifyAuth, any, ISpotifyAuthMethods>

Expand Down Expand Up @@ -51,6 +53,12 @@ export const SpotifyAuthSchema = new Schema<
}
})

SpotifyAuthSchema.methods.isExpired = function () {
const expiresAt: Date = this.expiresAt

return expiresAt.getTime() <= Date.now()
}

SpotifyAuthSchema.methods.serialize = function () {
return {
id: this.id,
Expand Down
6 changes: 5 additions & 1 deletion server/services/spotifyService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Device } from '@spotify/web-api-ts-sdk'
import {
authenticateSpotify,
getSpotifyEmail,
Expand Down Expand Up @@ -76,6 +75,11 @@ export class SpotifyService {
return await this.sdk.currentUser.profile()
}

public async getAccessToken() {
const tokens = await this.sdk.getAccessToken()
return tokens?.access_token ?? ''
}

// public async getActiveDevice(failSilently: true): Promise<Device | null>
// public async getActiveDevice(failSilently?: false): Promise<Device>
// public async getActiveDevice(failSilently = false) {
Expand Down
7 changes: 5 additions & 2 deletions server/views/userViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
requestPasswordReset,
resetPassword
} from 'server/controllers'
import { cleanUser, User, type IUser } from 'server/models'
import { cleanUser, Group, User, type IUser } from 'server/models'
import { apiAuthRequest, apiRequest, httpCreated, Viewset, type ApiArgs } from 'server/utils'

export const registerUserView = apiRequest(
Expand Down Expand Up @@ -43,8 +43,11 @@ export const currentUserView = apiAuthRequest(async (req, res, next) => {
*/
const { user } = res.locals
const userSerialized: IUser = user.serialize()
const userGroups = await Group.find({ ownerId: user._id })

const groups = userGroups.map((group) => ({ id: group._id, name: group.name }))

return userSerialized
return { ...userSerialized, groups }
})

export const requestPasswordResetView = apiRequest(async (req, res, next) => {
Expand Down

0 comments on commit cd11730

Please sign in to comment.