more env stuff
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { createContext, useCallback, useEffect, useState, PropsWithChildren } from 'react';
|
import { createContext, useCallback, useEffect, useState, PropsWithChildren } from 'react';
|
||||||
import { SpotifyAuth } from '@/lib/spotify/auth';
|
import { SpotifyAuth } from '@/lib/spotify/auth';
|
||||||
import { useAuth } from './auth-context';
|
import { useAuth } from './auth-context';
|
||||||
|
import { useConfig } from '@/hooks/use-config';
|
||||||
import type {
|
import type {
|
||||||
SpotifyContextType,
|
SpotifyContextType,
|
||||||
SpotifyAuthState,
|
SpotifyAuthState,
|
||||||
@@ -23,6 +24,7 @@ export const SpotifyContext = createContext<SpotifyContextType | null>(null);
|
|||||||
export const SpotifyProvider: React.FC<PropsWithChildren> = ({ children }) => {
|
export const SpotifyProvider: React.FC<PropsWithChildren> = ({ children }) => {
|
||||||
const { roles } = useAuth();
|
const { roles } = useAuth();
|
||||||
const isAdmin = roles?.includes('Admin') || false;
|
const isAdmin = roles?.includes('Admin') || false;
|
||||||
|
const config = useConfig();
|
||||||
|
|
||||||
const [authState, setAuthState] = useState<SpotifyAuthState>(defaultSpotifyState);
|
const [authState, setAuthState] = useState<SpotifyAuthState>(defaultSpotifyState);
|
||||||
|
|
||||||
@@ -40,8 +42,8 @@ export const SpotifyProvider: React.FC<PropsWithChildren> = ({ children }) => {
|
|||||||
const [isResumeLoading, setIsResumeLoading] = useState(false);
|
const [isResumeLoading, setIsResumeLoading] = useState(false);
|
||||||
|
|
||||||
const spotifyAuth = new SpotifyAuth(
|
const spotifyAuth = new SpotifyAuth(
|
||||||
import.meta.env.VITE_SPOTIFY_CLIENT_ID!,
|
config.spotifyClientId,
|
||||||
import.meta.env.VITE_SPOTIFY_REDIRECT_URI!
|
config.spotifyRedirectUri
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
12
src/hooks/use-config.ts
Normal file
12
src/hooks/use-config.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { useSuspenseQuery } from '@tanstack/react-query'
|
||||||
|
import { getConfig } from '@/lib/config'
|
||||||
|
|
||||||
|
export function useConfig() {
|
||||||
|
const { data } = useSuspenseQuery({
|
||||||
|
queryKey: ['config'],
|
||||||
|
queryFn: () => getConfig(),
|
||||||
|
staleTime: Infinity,
|
||||||
|
})
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
19
src/lib/config.ts
Normal file
19
src/lib/config.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { createServerFn } from '@tanstack/react-start'
|
||||||
|
|
||||||
|
export const getConfig = createServerFn({ method: 'GET' }).handler(async () => {
|
||||||
|
return {
|
||||||
|
apiDomain: process.env.VITE_API_DOMAIN || 'http://localhost:3000',
|
||||||
|
websiteDomain: process.env.VITE_WEBSITE_DOMAIN || 'http://localhost:3000',
|
||||||
|
spotifyClientId: process.env.VITE_SPOTIFY_CLIENT_ID || '',
|
||||||
|
spotifyRedirectUri: process.env.VITE_SPOTIFY_REDIRECT_URI || '',
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
export const serverConfig = {
|
||||||
|
apiDomain: process.env.VITE_API_DOMAIN || 'http://localhost:3000',
|
||||||
|
websiteDomain: process.env.VITE_WEBSITE_DOMAIN || 'http://localhost:3000',
|
||||||
|
supertokensUri: process.env.SUPERTOKENS_URI || 'http://localhost:3567',
|
||||||
|
pocketbaseUrl: process.env.POCKETBASE_URL || 'http://localhost:8090',
|
||||||
|
spotifyClientId: process.env.VITE_SPOTIFY_CLIENT_ID || '',
|
||||||
|
spotifyClientSecret: process.env.SPOTIFY_CLIENT_SECRET || '',
|
||||||
|
}
|
||||||
@@ -1,7 +1,14 @@
|
|||||||
|
const getOrigin = (): string => {
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
return window.location.origin;
|
||||||
|
}
|
||||||
|
return process.env.VITE_API_DOMAIN || 'http://localhost:3000';
|
||||||
|
};
|
||||||
|
|
||||||
export const appInfo = {
|
export const appInfo = {
|
||||||
appName: 'FLXN',
|
appName: 'FLXN',
|
||||||
apiDomain: process.env.VITE_API_DOMAIN || 'http://localhost:3000',
|
apiDomain: getOrigin(),
|
||||||
websiteDomain: process.env.VITE_WEBSITE_DOMAIN || 'http://localhost:3000',
|
websiteDomain: getOrigin(),
|
||||||
apiBasePath: '/api/auth',
|
apiBasePath: '/api/auth',
|
||||||
websiteBasePath: '/auth',
|
websiteBasePath: '/auth',
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user