fixed 'added 'refreshToken', converted to 'httpOnly', added SSL
fixed 'gql-types' generator
This commit is contained in:
parent
d179b8c7c8
commit
c190d368de
20 changed files with 314 additions and 179 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { ApolloClient, ApolloProvider, InMemoryCache } from '@apollo/client';
|
||||
import { ApolloClient, ApolloProvider, HttpLink, InMemoryCache, from } from '@apollo/client';
|
||||
import { onError } from '@apollo/client/link/error';
|
||||
import '@client/assets/styles/index.css';
|
||||
import '@client/assets/styles/nerd-fonts-icons.css';
|
||||
import { Provider } from '@client/components/ui/provider';
|
||||
|
|
@ -8,8 +9,8 @@ import { StrictMode, createContext, useState } from 'react';
|
|||
import ReactDOM from 'react-dom/client';
|
||||
|
||||
// TanStackRouter - Import the generated route tree
|
||||
import { routeTree } from './routeTree.gen';
|
||||
import { Session } from 'inspector';
|
||||
import { routeTree } from './routeTree.gen';
|
||||
|
||||
// Create a new router instance
|
||||
const router = createRouter({ routeTree });
|
||||
|
|
@ -22,25 +23,43 @@ declare module '@tanstack/react-router' {
|
|||
}
|
||||
|
||||
// Environment
|
||||
const MODE = import.meta.env.VITE_MODE || 'prod';
|
||||
const isProd = MODE === 'prod';
|
||||
|
||||
const HOST = import.meta.env.VITE_HOST || 'localhost';
|
||||
const PORT = import.meta.env.VITE_PORT || '3000';
|
||||
const uri = `http://${HOST}:${PORT}/graphql`;
|
||||
const uri = `https://${HOST}:${PORT}/graphql`;
|
||||
|
||||
// Auth context
|
||||
export const AuthContext = createContext<any>(null);
|
||||
|
||||
function App() {
|
||||
const sessionRaw = localStorage.getItem('session');
|
||||
const session: Session | null = sessionRaw !== null ? JSON.parse(sessionRaw) : null;
|
||||
|
||||
const [authHeaders, setAuthHeaders] = useState<Session | null>(session);
|
||||
const [authHeaders, setAuthHeaders] = useState<Session | null>(null);
|
||||
const AuthProvider = AuthContext.Provider;
|
||||
|
||||
// if (authHeaders == null) refreshToken();
|
||||
|
||||
const httpLink = new HttpLink({
|
||||
uri,
|
||||
credentials: 'include',
|
||||
headers: authHeaders != null ? { authorization: JSON.stringify(authHeaders) } : undefined
|
||||
});
|
||||
|
||||
const errorLink = onError(({ graphQLErrors, networkError }) => {
|
||||
if (graphQLErrors)
|
||||
graphQLErrors.forEach(({ message, locations, path }) =>
|
||||
console.error(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
|
||||
);
|
||||
|
||||
if (networkError) console.error(`[Network error]: ${networkError}`);
|
||||
});
|
||||
|
||||
const client = new ApolloClient({
|
||||
uri,
|
||||
link: isProd ? undefined : from([errorLink, httpLink]),
|
||||
cache: new InMemoryCache(),
|
||||
connectToDevTools: true,
|
||||
headers: authHeaders != null ? { authorization: JSON.stringify(authHeaders) } : undefined
|
||||
connectToDevTools: !isProd,
|
||||
credentials: 'include'
|
||||
});
|
||||
|
||||
return (
|
||||
|
|
@ -49,7 +68,6 @@ function App() {
|
|||
value={{
|
||||
authHeaders,
|
||||
setHeaders: (session: Session) => {
|
||||
localStorage.setItem('session', JSON.stringify(session));
|
||||
setAuthHeaders(session);
|
||||
}
|
||||
}}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue