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

Feature/jukebox auth #114

Merged
merged 3 commits into from
Oct 17, 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
136 changes: 77 additions & 59 deletions docker-compose.network.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ services:
####################################
# Server & Network Config
####################################
server:
jukebox:
restart: always
image: ikehunter5/jukebox-server:latest
container_name: server-jbx-fe
container_name: jbx-server-fe
environment:
- NODE_ENV=network
- PORT=9000
Expand All @@ -32,18 +32,23 @@ services:

- JWT_SECRET_KEY=changeme
- TOKEN_HEADER_KEY=Authorization

- MONGO_URI=mongodb://root:changeme@mongo-network-jbx-fe:27017

- KAFKA_BROKERS=kafka-jbx-fe:9092

- SPOTIFY_CLIENT_ID=${SPOTIFY_CLIENT_ID}
- SPOTIFY_CLIENT_SECRET=${SPOTIFY_CLIENT_SECRET}
- SPOTIFY_REDIRECT_URI=http://localhost:8000/api/spotify/login-callback/

- PROXY_URL=http://proxy:8080
- DB_HOST=jbx-network-db-fe
- DB_PORT=5432
- DB_USER=devuser
- DB_PASS=devpass
- DB_NAME=devdatabase
ports:
- 9000:9000
depends_on:
- mongodb
- kafka
- postgres-jukebox
# - kafka
networks:
- cluster
command: npm run start:network
Expand All @@ -56,67 +61,67 @@ services:
networks:
- cluster
depends_on:
- server
- jukebox
- club-manager
restart: always
volumes:
- client-dist-jbx:/vol/client
- club-static:/vol/static
environment:
- SERVER_URI=server-jbx-fe:9000
- WEBSOCKET_URI=server-jbx-fe:9000
- SERVER_URI=jbx-server-fe:9000
- WEBSOCKET_URI=jbx-server-fe:9000
- CLUB_MANAGER_URI=club-app-network-fe:9000
# attach: false

mongodb:
image: mongo:6.0.9
restart: always
container_name: mongo-network-jbx-fe
ports:
- 27017:27017
volumes:
- mongo-data-fe:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=changeme
command: mongod --quiet --logpath /dev/null --bind_ip_all
networks:
- cluster
# mongodb:
# image: mongo:6.0.9
# restart: always
# container_name: mongo-network-jbx-fe
# ports:
# - 27017:27017
# volumes:
# - mongo-data-fe:/data/db
# environment:
# - MONGO_INITDB_ROOT_USERNAME=root
# - MONGO_INITDB_ROOT_PASSWORD=changeme
# command: mongod --quiet --logpath /dev/null --bind_ip_all
# networks:
# - cluster

####################################
# Kafka Config
####################################
zookeeper:
image: confluentinc/cp-zookeeper:latest
container_name: zookeeper-jbx-fe
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ports:
- 2181:2181
networks:
- cluster
attach: false
# zookeeper:
# image: confluentinc/cp-zookeeper:latest
# container_name: zookeeper-jbx-fe
# environment:
# ZOOKEEPER_CLIENT_PORT: 2181
# ZOOKEEPER_TICK_TIME: 2000
# ports:
# - 2181:2181
# networks:
# - cluster
# attach: false

kafka:
image: confluentinc/cp-kafka:latest
container_name: kafka-jbx-fe
depends_on:
- zookeeper
ports:
- 9092:9092
- 29092:29092
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper-jbx-fe:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_AUTO_CREATE_TOPICS_ENABLE: true
networks:
- cluster
attach: false
# kafka:
# image: confluentinc/cp-kafka:latest
# container_name: kafka-jbx-fe
# depends_on:
# - zookeeper
# ports:
# - 9092:9092
# - 29092:29092
# environment:
# KAFKA_BROKER_ID: 1
# KAFKA_ZOOKEEPER_CONNECT: zookeeper-jbx-fe:2181
# KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
# KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
# KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
# KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
# KAFKA_AUTO_CREATE_TOPICS_ENABLE: true
# networks:
# - cluster
# attach: false

####################################
# Web Socket Config
Expand Down Expand Up @@ -170,16 +175,27 @@ services:
- DJANGO_SUPERUSER_EMAIL=${DJANGO_SUPERUSER_EMAIL:[email protected]}
- DJANGO_SUPERUSER_PASS=${DJANGO_SUPERUSER_PASS:-changeme}
depends_on:
- postgres
- postgres-clubs
networks:
- cluster

postgres-jukebox:
image: postgres:13-alpine
container_name: jbx-network-db-fe
volumes:
- postgres-jukebox:/var/lib/postgresql/data
environment:
- POSTGRES_DB=devdatabase
- POSTGRES_USER=devuser
- POSTGRES_PASSWORD=devpass
networks:
- cluster

postgres:
postgres-clubs:
image: postgres:13-alpine
container_name: club-app-network-db-fe
volumes:
- postgres-osc-network:/var/lib/postgresql/data
- postgres-clubs:/var/lib/postgresql/data
environment:
- POSTGRES_DB=devdatabase
- POSTGRES_USER=devuser
Expand All @@ -193,8 +209,10 @@ volumes:
mongo-data-fe:
club-static:
name: club-static-fe
postgres-osc-network:
name: postgres-osc-network-fe
postgres-clubs:
name: postgres-clubs-network-fe
postgres-jukebox:
name: postgres-jukebox-network-fe

networks:
cluster:
Expand Down
12 changes: 6 additions & 6 deletions src/mock/mock-clubs.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
export const mockClubs: IClub[] = [
{
id: 'club-1',
id: 0,
name: 'Test Club 1',
ownerId: '12345',
ownerId: 0,
},
{
id: 'club-2',
id: 1,
name: 'Test Club 2',
ownerId: '98765',
ownerId: 1,
},
{
id: 'club-3',
id: 2,
name: 'Test Club 3',
ownerId: '98765',
ownerId: 1,
},
]
4 changes: 2 additions & 2 deletions src/mock/mock-user.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { mockClubs } from './mock-clubs'

export const mockUser: IUser & { token: string } = {
id: '12345',
id: 0,
token: 'test-token',
email: '[email protected]',
firstName: 'John',
lastName: 'Doe',
clubs: mockClubs.map((club) => ({
id: club.id,
name: club.name,
ownerId: '12345',
ownerId: 0,
})),
image:
'https://static.vecteezy.com/system/resources/thumbnails/001/840/618/small_2x/picture-profile-icon-male-icon-human-or-people-sign-and-symbol-free-vector.jpg',
Expand Down
72 changes: 51 additions & 21 deletions src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ import {
import { NetworkRoutes } from './routes'
import type { NetworkResponse } from './types'

interface SpotifyLink {
id: number
access_token: string
user_id: number
spotify_email: string
expires_in: number
expires_at: string
token_type: string
}

interface Jukebox {
id: number
name: string
club_id: number
spotify_links: SpotifyLink[]
active_spotify_link?: SpotifyLink
}

export class Network {
static instance: Network
protected routes: typeof NetworkRoutes
Expand Down Expand Up @@ -108,7 +126,7 @@ export class Network {
return ok(token)
}

const res = await this.sendRequest(NetworkRoutes.user.token, 'POST', {
const res = await this.sendRequest(this.routes.user.token, 'POST', {
email,
password,
})
Expand All @@ -135,7 +153,7 @@ export class Network {
if (this.env === 'dev') {
await sleep(1000)
return {
id: Date.now().toString(),
id: Date.now(),
email: mockUser.email,
firstName: mockUser.firstName,
lastName: mockUser.lastName,
Expand All @@ -144,62 +162,74 @@ export class Network {
clubs: mockUser.clubs,
}
}
const res = await this.sendRequest(NetworkRoutes.user.details)
const res = await this.sendRequest(this.routes.user.details)
console.log('user res:', res)

return {
id: res?.data.id,
email: res?.data.email,
firstName: res?.data.firstName,
lastName: res?.data.lastName,
firstName: res?.data.first_name,
lastName: res?.data.last_name,
image:
res?.data.image ??
'https://alliancebjjmn.com/wp-content/uploads/2019/07/placeholder-profile-sq-491x407.jpg',
clubs: Array.from(res?.data.clubs).map((club: any) => ({
id: String(club.id),
id: club.id,
name: String(club.name),
ownerId: String(club.ownerId),
ownerId: club.owner_id,
})),
}
}

public async sendGetClubInfo(clubId: string): Promise<IClub> {
public async sendGetClubInfo(clubId: number): Promise<IClub> {
if (this.env === 'dev') {
throw new NotImplementedError('network.sendGetClubInfo')
}

const res = await this.sendRequest(NetworkRoutes.club.info(clubId))
const res = await this.sendRequest(this.routes.club.info(clubId))
return {
id: res.data.id,
name: res.data.name,
ownerId: res.data.ownerId,
ownerId: res.data.owner_id,
}
}

public async sendGetSpotifyToken(clubId: string): Promise<ISpotifyAuth> {
public async sendGetSpotifyToken(clubId: number): Promise<ISpotifyAuth> {
if (this.env === 'dev') {
await sleep(1000)

return {
id: '66e72f18a7c93a68835d630d',
id: 0,
accessToken: String(import.meta.env.VITE_SPOTIFY_ACCESS_TOKEN),
userId: '66da2b580235f4ff7270460d',
userId: 0,
spotifyEmail: '[email protected]',
expiresIn: 3600,
tokenType: 'Bearer',
expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24).getTime(),
}
}

const res = await this.sendRequest(NetworkRoutes.club.spotifyAuth(clubId))
const res = await this.sendRequest(this.routes.jukebox.list)
const jukeboxes = res.data as Jukebox[]
const selectedJbx = jukeboxes.find((jbx) => +jbx.club_id === +clubId)

// TODO: Handle jukeboxes separately from clubs
if (!selectedJbx?.active_spotify_link) {
throw new Error('No spotify connections found.')
}

const refreshedRes = await this.sendRequest(
this.routes.jukebox.activeLink(selectedJbx.id),
)

return {
id: res?.data.id,
accessToken: res?.data.accessToken,
userId: res?.data.userId,
spotifyEmail: res?.data.spotifyEmail,
expiresIn: res?.data.expiresIn,
tokenType: res?.data.tokenType,
expiresAt: res?.data.expiresAt,
id: refreshedRes.data?.id,
accessToken: refreshedRes.data?.access_token,
userId: refreshedRes.data?.user_id,
spotifyEmail: refreshedRes.data?.spotify_email,
expiresIn: refreshedRes.data?.expires_in,
tokenType: refreshedRes.data?.token_type,
expiresAt: new Date(refreshedRes.data?.expires_at).getTime(),
}
}
}
7 changes: 5 additions & 2 deletions src/network/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export const NetworkRoutes = {
details: `${base}/user/me/`,
},
club: {
info: (id: string) => `${base}/club/clubs/${id}`,
spotifyAuth: (id: string) => `${base}/club/${id}/spotify/auth`,
info: (id: number) => `${base}/club/clubs/${id}`,
},
jukebox: {
list: `${base}/jukebox/jukeboxes`,
activeLink: (id: number) => `${base}/jukebox/${id}/active-link/`,
},
}
Loading
Loading