Skip to content

Commit

Permalink
Merge pull request #6 from sagar-deriv/prodUrl/refactor
Browse files Browse the repository at this point in the history
refactor: update prod Url
  • Loading branch information
sagar-deriv authored Dec 13, 2024
2 parents e947d61 + 929e530 commit 9d1a846
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
15 changes: 13 additions & 2 deletions src/components/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@ import { useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import useDerivAccounts from '../hooks/useDerivAccounts'

// Production fallback values
const PROD_APP_ID = '66435'
const PROD_OAUTH_URL = 'https://oauth.deriv.com'

const Login = () => {
const APP_ID = import.meta.env.VITE_APP_ID
const OAUTH_URL = 'https://oauth.deriv.com'
// Use env variables with fallback to production values
const APP_ID = import.meta.env.VITE_APP_ID || PROD_APP_ID
const OAUTH_URL = import.meta.env.VITE_OAUTH_URL || PROD_OAUTH_URL

console.log('OAuth Configuration:', {
APP_ID,
OAUTH_URL,
FINAL_URL: `${OAUTH_URL}/oauth2/authorize?app_id=${APP_ID}`
})

const navigate = useNavigate()
const { defaultAccount, isLoading } = useDerivAccounts()
Expand Down
39 changes: 19 additions & 20 deletions src/hooks/useDerivWebSocket.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { useState, useEffect, useCallback, useRef } from 'react'
import useDerivAccounts from './useDerivAccounts'

// Add console.log to debug env variables
console.log('Environment variables:', {
WS_URL: import.meta.env.VITE_WS_URL,
APP_ID: import.meta.env.VITE_APP_ID
})
// Production fallback values
const PROD_WS_URL = 'wss://green.derivws.com/websockets/v3'
const PROD_APP_ID = '66435'

// Construct WS_URL after ensuring env variables exist
const WS_URL = import.meta.env.VITE_WS_URL && import.meta.env.VITE_APP_ID
? `${import.meta.env.VITE_WS_URL}?app_id=${import.meta.env.VITE_APP_ID}`
: null
// Use env variables with fallback to production values
const WS_URL = import.meta.env.VITE_WS_URL || PROD_WS_URL
const APP_ID = import.meta.env.VITE_APP_ID || PROD_APP_ID

if (!WS_URL) {
console.error('WebSocket URL could not be constructed. Missing environment variables:', {
WS_URL: import.meta.env.VITE_WS_URL,
APP_ID: import.meta.env.VITE_APP_ID
})
}
// Construct WebSocket URL
const WEBSOCKET_URL = `${WS_URL}?app_id=${APP_ID}`

console.log('WebSocket Configuration:', {
WS_URL,
APP_ID,
FINAL_URL: WEBSOCKET_URL
})

// Singleton WebSocket instance
let globalWs = null
Expand Down Expand Up @@ -57,22 +56,22 @@ const useDerivWebSocket = () => {

// Initialize WebSocket connection
useEffect(() => {
if (!defaultAccount?.token || !WS_URL) {
if (!defaultAccount?.token || !WEBSOCKET_URL) {
console.log('Cannot initialize WebSocket:', {
hasToken: Boolean(defaultAccount?.token),
hasWsUrl: Boolean(WS_URL),
wsUrl: WS_URL
hasWsUrl: Boolean(WEBSOCKET_URL),
wsUrl: WEBSOCKET_URL
})
return
}

console.log('Initializing WebSocket with URL:', WS_URL)
console.log('Initializing WebSocket with URL:', WEBSOCKET_URL)
console.log('Using token:', defaultAccount.token)

const initializeWebSocket = () => {
if (!globalWs) {
console.log('Creating new WebSocket connection')
globalWs = new WebSocket(WS_URL)
globalWs = new WebSocket(WEBSOCKET_URL)

globalWs.onopen = () => {
console.log('WebSocket connected, readyState:', globalWs.readyState)
Expand Down
18 changes: 11 additions & 7 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import react from '@vitejs/plugin-react'
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')

// Production fallback values
const PROD_APP_ID = '66435'
const PROD_WS_URL = 'wss://green.derivws.com/websockets/v3'
const PROD_OAUTH_URL = 'https://oauth.deriv.com'

console.log('Loaded environment variables:', {
APP_ID: env.VITE_APP_ID,
WS_URL: env.VITE_WS_URL
APP_ID: env.VITE_APP_ID || PROD_APP_ID,
WS_URL: env.VITE_WS_URL || PROD_WS_URL,
OAUTH_URL: env.VITE_OAUTH_URL || PROD_OAUTH_URL
})

return {
Expand All @@ -16,11 +22,9 @@ export default defineConfig(({ mode }) => {
},
base: '/copy-trading/',
define: {
'import.meta.env.VITE_APP_ID': JSON.stringify(env.VITE_APP_ID),
'import.meta.env.VITE_WS_URL': JSON.stringify(env.VITE_WS_URL),
'import.meta.env.VITE_OAUTH_URL': JSON.stringify(env.VITE_OAUTH_URL),
'import.meta.env.VITE_REDIRECT_URL': JSON.stringify(env.VITE_REDIRECT_URL),
'import.meta.env.VITE_BASE_URL': JSON.stringify(env.VITE_BASE_URL),
'import.meta.env.VITE_APP_ID': JSON.stringify(env.VITE_APP_ID || PROD_APP_ID),
'import.meta.env.VITE_WS_URL': JSON.stringify(env.VITE_WS_URL || PROD_WS_URL),
'import.meta.env.VITE_OAUTH_URL': JSON.stringify(env.VITE_OAUTH_URL || PROD_OAUTH_URL),
__APP_ENV__: env.APP_ENV
}
}
Expand Down

0 comments on commit 9d1a846

Please sign in to comment.