Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix docker compose file, spotify auth without network #46

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 9 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. <https://github.com/ufosc/Jukebox-Frontend>

## Project Install One-Liner

Expand Down Expand Up @@ -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 <http://localhost:8000/login>. 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.
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.network.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion server/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
98 changes: 48 additions & 50 deletions server/docs/swagger_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -795,8 +795,8 @@
]
}
},
"/api/group/{id}/spotify": {
"post": {
"/api/group/{id}/spotify/current-track": {
"get": {
"tags": [
"Group"
],
Expand All @@ -807,18 +807,6 @@
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"schema": {
"type": "object",
"properties": {
"spotifyEmail": {
"example": "any"
}
}
}
}
],
"responses": {
Expand Down Expand Up @@ -854,8 +842,8 @@
]
}
},
"/api/group/{id}/spotify/current-track": {
"get": {
"/api/group/{id}/spotify/state": {
"post": {
"tags": [
"Group"
],
Expand All @@ -866,6 +854,18 @@
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"schema": {
"type": "object",
"properties": {
"state": {
"example": "any"
}
}
}
}
],
"responses": {
Expand Down Expand Up @@ -901,8 +901,8 @@
]
}
},
"/api/group/{id}/spotify/state": {
"post": {
"/api/group/{id}/spotify/devices": {
"get": {
"tags": [
"Group"
],
Expand All @@ -913,18 +913,6 @@
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"schema": {
"type": "object",
"properties": {
"state": {
"example": "any"
}
}
}
}
],
"responses": {
Expand Down Expand Up @@ -960,8 +948,8 @@
]
}
},
"/api/group/{id}/spotify/devices": {
"get": {
"/api/group/{id}/spotify/default-device": {
"post": {
"tags": [
"Group"
],
Expand All @@ -972,6 +960,18 @@
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"schema": {
"type": "object",
"properties": {
"deviceId": {
"example": "any"
}
}
}
}
],
"responses": {
Expand Down Expand Up @@ -1007,8 +1007,8 @@
]
}
},
"/api/group/{id}/spotify/default-device": {
"post": {
"/api/group/{id}/spotify/auth": {
"get": {
"tags": [
"Group"
],
Expand All @@ -1019,18 +1019,6 @@
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"schema": {
"type": "object",
"properties": {
"deviceId": {
"example": "any"
}
}
}
}
],
"responses": {
Expand Down Expand Up @@ -1064,10 +1052,8 @@
"Bearer": []
}
]
}
},
"/api/group/{id}/spotify/auth": {
"get": {
},
"post": {
"tags": [
"Group"
],
Expand All @@ -1078,6 +1064,18 @@
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"schema": {
"type": "object",
"properties": {
"spotifyEmail": {
"example": "any"
}
}
}
}
],
"responses": {
Expand Down
13 changes: 13 additions & 0 deletions server/models/spotifyAuthModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof SpotifyAuth>
2 changes: 1 addition & 1 deletion server/routes/groupRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down