From e277f44a02e07b206bb7695b27851dfff1907f53 Mon Sep 17 00:00:00 2001 From: Isaac Hunter Date: Thu, 19 Sep 2024 09:41:41 -0400 Subject: [PATCH] fix docker compose file, spotify auth without network --- README.md | 46 +++------------ docker-compose.network.yml | 4 +- server/config/constants.ts | 3 +- server/docs/swagger_output.json | 98 +++++++++++++++---------------- server/models/spotifyAuthModel.ts | 13 ++++ server/routes/groupRoutes.ts | 2 +- 6 files changed, 75 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index 8ce039e..b16e2b6 100644 --- a/README.md +++ b/README.md @@ -7,23 +7,18 @@ Welcome to the Jukebox Server! To get the server running, you only need Node.js ## Table of Contents - 1. Description - Brief Description of the Project - 2. Project Install One-Liner - Install the Project in One-Line - 3. Getting Started - How to download the project locally - 4. Technology Stack (documentation link) - 5. Workflow - Current Issues - 6. Setting up Spotify - 7. Testing - How to test new changes to your code +- [Table of Contents](#table-of-contents) +- [Description](#description) +- [Project Install One-Liner](#project-install-one-liner) +- [Getting started](#getting-started) +- [Technology Stack (with documentation link)](#technology-stack-with-documentation-link) +- [Workflow](#workflow) +- [Testing](#testing) ## Description - The Jukebox Server is a Node.js-based server that connects with Spotify's API, allowing users to manage and potentially play music and function like a Jukebox. Required software includes Docker, Docker Compose, and Git. - This project has two Github pages this is designated for the Backend Development. If you are interested in the Frontend Development please link here. https://github.com/ufosc/Jukebox-Frontend +The Jukebox Server is a Node.js-based server that connects with Spotify's API, allowing users to manage and potentially play music and function like a Jukebox. Required software includes Docker, Docker Compose, and Git. +This project has two Github pages this is designated for the Backend Development. If you are interested in the Frontend Development please link here. ## Project Install One-Liner @@ -83,29 +78,6 @@ After you are satisfied, push your branch to GitHub and submit a pull request fo The pull request will then be tested by a maintainer, and merged into the main branch. -## Setting up Spotify - -The server gets its services from Spotify, as such using its developer account allows the server access to Spotify's artists, playlists, and algorithms - -### 1. Create spotify app - -A Spotify app is essentially a spotify developer account where you can access their api and other developer services. After you get access to the dashboard you can create additional apps in the future for other personal projects. - -To create an app refer to their guide: [Developer.Spotify.com](https://developer.spotify.com/documentation/general/guides/authorization/app-settings/). During setup, here are the recommended settings: - -- The name can be anything, I chose OSC Jukebox -- For Website, enter `http://localhost:8000` as this is what they will test for when you access their api -- For Redirect URI enter `http://localhost:8000/spotify-login-callback`, this is the redirect url that spotify will send you to once you are authenticated. This is also the url will give you your spotify auth token. -- Ignore Bundle IDs and Android Packages - -### 2. Getting The Spotify Auth Code - -Once you have the Spotify App and the project set up, the last step is to authenticate your account directly with Spotify. To do so, you must visit . This will redirect you to the `/spotify-token` route which will display your new **access token** in JSON format. - -Take the Access Token and enter it into the `.env` file at the root of the project in the variable named `SP_ACCESS`. This will allow you to access all of Spotify's API routes. - -In the project, `/login` has implemented Spotify's access token authorization code they provide on GitHub, you can look over it [here](https://github.com/spotify/web-api-examples/blob/master/authentication/authorization_code/app.js). - ## Testing When creating unit tests please use Mocha Formatting. REMEMBER TO HAVE AT LEAST 3 UNIT TESTS. diff --git a/docker-compose.network.yml b/docker-compose.network.yml index c937bb7..2046e55 100644 --- a/docker-compose.network.yml +++ b/docker-compose.network.yml @@ -13,11 +13,11 @@ services: - JWT_SECRET_KEY=changeme - TOKEN_HEADER_KEY=Authorization - MONGO_URI=mongodb://root:changeme@mongo-network-jbx:27017 - - SP_ID=${SP_ID} - - SP_SECRET=${SP_SECRET} - LOG_LEVEL=debug - KAFKA_BROKERS=kafka-jbx:9092 - BASE_URL=http://localhost:8080 + - SPOTIFY_CLIENT_ID=${SPOTIFY_CLIENT_ID} + - SPOTIFY_CLIENT_SECRET=${SPOTIFY_CLIENT_SECRET} ports: - 9000:9000 depends_on: diff --git a/server/config/constants.ts b/server/config/constants.ts index 3fea327..66eaba2 100644 --- a/server/config/constants.ts +++ b/server/config/constants.ts @@ -16,7 +16,8 @@ export const AUTH_TOKEN_COOKIE_NAME = 'dev-auth-token' export const SPOTIFY_CLIENT_ID = process.env.SPOTIFY_CLIENT_ID || 'changeme' export const SPOTIFY_CLIENT_SECRET = process.env.SPOTIFY_CLIENT_SECRET || 'changeme' -export const SPOTIFY_REDIRECT_URI = process.env.SPOITFY_REDIRECT_URI || 'http://localhost:8000/api/spotify/login-callback/' +export const SPOTIFY_REDIRECT_PATH = '/api/spotify/login-callback/' +export const SPOTIFY_REDIRECT_URI = BASE_URL + SPOTIFY_REDIRECT_PATH // export const LOG_LEVEL = process.env.LOG_LEVEL || 'warn' // export const LOG_NS = process.env.LOG_NS || 'server' diff --git a/server/docs/swagger_output.json b/server/docs/swagger_output.json index 05693df..a823ac7 100644 --- a/server/docs/swagger_output.json +++ b/server/docs/swagger_output.json @@ -795,8 +795,8 @@ ] } }, - "/api/group/{id}/spotify": { - "post": { + "/api/group/{id}/spotify/current-track": { + "get": { "tags": [ "Group" ], @@ -807,18 +807,6 @@ "in": "path", "required": true, "type": "string" - }, - { - "name": "body", - "in": "body", - "schema": { - "type": "object", - "properties": { - "spotifyEmail": { - "example": "any" - } - } - } } ], "responses": { @@ -854,8 +842,8 @@ ] } }, - "/api/group/{id}/spotify/current-track": { - "get": { + "/api/group/{id}/spotify/state": { + "post": { "tags": [ "Group" ], @@ -866,6 +854,18 @@ "in": "path", "required": true, "type": "string" + }, + { + "name": "body", + "in": "body", + "schema": { + "type": "object", + "properties": { + "state": { + "example": "any" + } + } + } } ], "responses": { @@ -901,8 +901,8 @@ ] } }, - "/api/group/{id}/spotify/state": { - "post": { + "/api/group/{id}/spotify/devices": { + "get": { "tags": [ "Group" ], @@ -913,18 +913,6 @@ "in": "path", "required": true, "type": "string" - }, - { - "name": "body", - "in": "body", - "schema": { - "type": "object", - "properties": { - "state": { - "example": "any" - } - } - } } ], "responses": { @@ -960,8 +948,8 @@ ] } }, - "/api/group/{id}/spotify/devices": { - "get": { + "/api/group/{id}/spotify/default-device": { + "post": { "tags": [ "Group" ], @@ -972,6 +960,18 @@ "in": "path", "required": true, "type": "string" + }, + { + "name": "body", + "in": "body", + "schema": { + "type": "object", + "properties": { + "deviceId": { + "example": "any" + } + } + } } ], "responses": { @@ -1007,8 +1007,8 @@ ] } }, - "/api/group/{id}/spotify/default-device": { - "post": { + "/api/group/{id}/spotify/auth": { + "get": { "tags": [ "Group" ], @@ -1019,18 +1019,6 @@ "in": "path", "required": true, "type": "string" - }, - { - "name": "body", - "in": "body", - "schema": { - "type": "object", - "properties": { - "deviceId": { - "example": "any" - } - } - } } ], "responses": { @@ -1064,10 +1052,8 @@ "Bearer": [] } ] - } - }, - "/api/group/{id}/spotify/auth": { - "get": { + }, + "post": { "tags": [ "Group" ], @@ -1078,6 +1064,18 @@ "in": "path", "required": true, "type": "string" + }, + { + "name": "body", + "in": "body", + "schema": { + "type": "object", + "properties": { + "spotifyEmail": { + "example": "any" + } + } + } } ], "responses": { diff --git a/server/models/spotifyAuthModel.ts b/server/models/spotifyAuthModel.ts index f76f3f5..aa6f603 100644 --- a/server/models/spotifyAuthModel.ts +++ b/server/models/spotifyAuthModel.ts @@ -51,5 +51,18 @@ export const SpotifyAuthSchema = new Schema< } }) +SpotifyAuthSchema.methods.serialize = function () { + return { + id: this.id, + accessToken: this.accessToken, + refreshToken: this.refreshToken, + userId: this.userId, + spotifyEmail: this.spotifyEmail, + expiresIn: this.expiresIn, + tokenType: this.tokenType, + expiresAt: this.expiresAt + } +} + export const SpotifyAuth = model('SpotifyAuth', SpotifyAuthSchema) export type SpotifyAuth = InstanceType diff --git a/server/routes/groupRoutes.ts b/server/routes/groupRoutes.ts index aaa4a8d..3a74dd4 100644 --- a/server/routes/groupRoutes.ts +++ b/server/routes/groupRoutes.ts @@ -4,12 +4,12 @@ import * as views from '../views/groupViews' const router = Router() -router.post('/:id/spotify', isAuthenticated, views.assignSpotifyAccountView) router.get('/:id/spotify/current-track', isAuthenticated, views.getGroupCurrentTrackView) router.post('/:id/spotify/state', isAuthenticated, views.setGroupPlayerStateView) router.get('/:id/spotify/devices', isAuthenticated, views.getGroupDevicesView) router.post('/:id/spotify/default-device', isAuthenticated, views.setGroupDefaultDeviceView) router.get('/:id/spotify/auth', isAuthenticated, views.getGroupSpotifyAuthView) +router.post('/:id/spotify/auth', isAuthenticated, views.assignSpotifyAccountView) router.post('/groups', isAuthenticated, views.groupCreateView) router.get('/groups', isAuthenticated, views.groupListView)