fix(tesla): skip OAuth on car after pair-code if desktop already linked Tesla
The pair-code flow always called startTeslaConnect() afterwards, which redirected the car browser to auth.tesla.com even when the server already held tokens from the desktop OAuth. Now connectIfNeeded() re-fetches status post-login and short-circuits when connected: true. The car flow becomes: enter 6-digit code → 'Tesla connected · Carried over from your other device — ready to go.' No second OAuth.
This commit is contained in:
Binary file not shown.
@@ -1,7 +1,7 @@
|
||||
import React, { useState } from 'react';
|
||||
import { toast } from 'sonner';
|
||||
import { MapContainer, TileLayer, Marker, Polyline, Popup, useMap } from 'react-leaflet';
|
||||
import { useTesla, startTeslaConnect, disconnectTesla, sendToTeslaNav, wakeTesla, loginOwner, mintOwnerPairCode, redeemOwnerPairCode, type TeslaActiveRoute } from '../lib/tesla';
|
||||
import { useTesla, startTeslaConnect, disconnectTesla, sendToTeslaNav, wakeTesla, loginOwner, mintOwnerPairCode, redeemOwnerPairCode, fetchTeslaStatus, type TeslaActiveRoute } from '../lib/tesla';
|
||||
import { isMockEnabled, getMockScenario, setMockScenario, resetMockDrive } from '../lib/teslaMock';
|
||||
import { detectInCar } from '../lib/incar';
|
||||
import { computeBatteryPlan, type BatteryAtStop, type BatteryRisk } from '../lib/batteryPlan';
|
||||
@@ -2452,16 +2452,28 @@ export default function TeslaTripPlanner() {
|
||||
teslaStatus={tesla.status}
|
||||
teslaState={tesla.state}
|
||||
onConnectTesla={async () => {
|
||||
// Pending action that runs after owner-login succeeds. Re-checks
|
||||
// the server: if a Tesla token is already stored (e.g. the user
|
||||
// OAuthed on their desktop and just paired this device), we skip
|
||||
// the Tesla OAuth round-trip entirely.
|
||||
const connectIfNeeded = async () => {
|
||||
const fresh = await fetchTeslaStatus();
|
||||
if (fresh.connected) {
|
||||
await tesla.refreshStatus();
|
||||
toast.success('Tesla connected', {
|
||||
description: 'Carried over from your other device — ready to go.',
|
||||
});
|
||||
return;
|
||||
}
|
||||
try { await startTeslaConnect(); }
|
||||
catch { toast.error('Could not start Tesla OAuth'); }
|
||||
};
|
||||
if (tesla.owner?.required && !tesla.owner.authenticated) {
|
||||
ownerLoginThenRef.current = async () => {
|
||||
try { await startTeslaConnect(); }
|
||||
catch { toast.error('Could not start Tesla OAuth'); }
|
||||
};
|
||||
ownerLoginThenRef.current = connectIfNeeded;
|
||||
setOwnerLoginOpen(true);
|
||||
return;
|
||||
}
|
||||
try { await startTeslaConnect(); }
|
||||
catch { toast.error('Could not start Tesla OAuth'); }
|
||||
await connectIfNeeded();
|
||||
}}
|
||||
onDisconnectTesla={async () => {
|
||||
await disconnectTesla();
|
||||
|
||||
Reference in New Issue
Block a user