Skip to content

Commit

Permalink
Update preflight auth
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Jul 30, 2024
1 parent dfbef84 commit 3c7f3f2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
5 changes: 2 additions & 3 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useEffect } from "react";
import { Navbar } from "./components/Navbar";
import { preflightAuth } from "./api/auth";
import { preflightAuth } from './api/auth';
import { Navbar } from './components/Navbar';

export const App = () => {
preflightAuth();
Expand Down
8 changes: 5 additions & 3 deletions web/src/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* eslint-disable no-undef */
import { create } from 'zustand';

export const preflightAuth = async () => {
// If query params includes `token`, set it using setAuthToken and remove it from url
const { setAuthToken } = useAuth();

const token = new URLSearchParams(window.location.search).get('token');

console.log('PREFLIGHT AUTH', token);

if (token) {
setAuthToken(token);
useAuth.getState().setAuthToken(token);
window.history.replaceState(
{},
document.title,
Expand Down Expand Up @@ -45,6 +47,6 @@ export const useAuth = create<{
localStorage.removeItem('property.v3x.token');
}

set({ token: null });
set({ token: undefined });
},
}));
30 changes: 17 additions & 13 deletions web/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import { RouterProvider, createRouter } from '@tanstack/react-router'
import './index.css'
import './index.css';

import { createRouter, RouterProvider } from '@tanstack/react-router';
import React from 'react';
import ReactDOM from 'react-dom/client';

import { preflightAuth } from './api/auth';
// Import the generated route tree
import { routeTree } from './routeTree.gen'
import { routeTree } from './routeTree.gen';

// Create a new router instance
const router = createRouter({ routeTree })
const router = createRouter({ routeTree });

// Register the router instance for type safety
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
interface Register {
router: typeof router;
}
}

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>,
preflightAuth();

ReactDOM.createRoot(document.querySelector('#root')!).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);

0 comments on commit 3c7f3f2

Please sign in to comment.