diff --git a/example-dimo-auth/dimo-login-button-sdk-1.0.0.tgz b/example-dimo-auth/dimo-login-button-sdk-1.0.0.tgz new file mode 100644 index 0000000..c2c58d2 Binary files /dev/null and b/example-dimo-auth/dimo-login-button-sdk-1.0.0.tgz differ diff --git a/example-dimo-auth/package.json b/example-dimo-auth/package.json index f7ec90a..be31b41 100644 --- a/example-dimo-auth/package.json +++ b/example-dimo-auth/package.json @@ -10,7 +10,7 @@ "@types/node": "^16.18.114", "@types/react": "^18.3.11", "@types/react-dom": "^18.3.1", - "dimo-login-button-sdk": "file:../sdk", + "dimo-login-button-sdk": "file:dimo-login-button-sdk-1.0.0.tgz", "react": "^18.3.1", "react-dom": "^18.3.1", "react-scripts": "5.0.1", diff --git a/sdk/src/auth/embedAuth.ts b/sdk/src/auth/embedAuth.ts index 8c502a9..5d0a710 100644 --- a/sdk/src/auth/embedAuth.ts +++ b/sdk/src/auth/embedAuth.ts @@ -3,6 +3,7 @@ import { handleMessageForEmbed } from "../utils/eventHandler"; export const embedAuth = ( onSuccess: (authData: { token: string }) => void, onError: (error: Error) => void, + setAuthenticated: React.Dispatch>, dimoLogin: string, clientId?: string, redirectUri?: string, @@ -10,12 +11,11 @@ export const embedAuth = ( permissionTemplateId?: string, vehicles?: string[] ) => { - // Embed logic TBD - const cleanup = handleMessageForEmbed( dimoLogin, onSuccess, onError, + setAuthenticated, clientId, redirectUri, apiKey, diff --git a/sdk/src/auth/popupAuth.ts b/sdk/src/auth/popupAuth.ts index 2c1dcb2..d78a782 100644 --- a/sdk/src/auth/popupAuth.ts +++ b/sdk/src/auth/popupAuth.ts @@ -3,12 +3,13 @@ import { handleMessageForPopup } from "../utils/eventHandler"; export const popupAuth = ( onSuccess: (authData: { token: string }) => void, onError: (error: Error) => void, + setAuthenticated: React.Dispatch>, dimoLogin: string, clientId?: string, redirectUri?: string, apiKey?: string, permissionTemplateId?: string, - vehicles?: string[] + vehicles?: string[], ) => { try { const popup = window.open( @@ -26,6 +27,7 @@ export const popupAuth = ( dimoLogin, onSuccess, onError, + setAuthenticated, popup, clientId, redirectUri, diff --git a/sdk/src/components/LoginWithDimo.tsx b/sdk/src/components/LoginWithDimo.tsx index fe50a29..6be9ae6 100644 --- a/sdk/src/components/LoginWithDimo.tsx +++ b/sdk/src/components/LoginWithDimo.tsx @@ -1,10 +1,12 @@ // src/components/LoginWithDimo.tsx -import React from "react"; +import React, { useEffect, useState } from "react"; import { popupAuth } from "../auth/popupAuth"; import { embedAuth } from "../auth/embedAuth"; import { redirectAuth } from "../auth/redirectAuth"; import "../styles/LoginWithDimo.css"; +import { getJWTFromCookies } from "../storage/storageManager"; +import { isTokenExpired } from "../token/tokenManager"; interface LoginWithDimoProps { mode: "popup" | "embed" | "redirect"; // The mode of login @@ -29,20 +31,29 @@ const LoginWithDimo: React.FC = ({ vehicles, environment, }) => { + const dimoLogin = environment == "development" ? "https://login.dev.dimo.org" : "https://login.dimo.org"; + const [authenticated, setAuthenticated] = useState(false); + + useEffect(()=>{ + const jwt = getJWTFromCookies() + if ( jwt && !isTokenExpired(jwt) ) { + setAuthenticated(true); + } + },[authenticated]) - const dimoLogin = environment == "development" ? "https://login.dev.dimo.org" : "https://login.dimo.org"; //TODO: Pull from ENV const handleButtonClick = () => { switch (mode) { case "popup": popupAuth( onSuccess, onError, + setAuthenticated, dimoLogin, clientId, redirectUri, apiKey, permissionTemplateId, - vehicles + vehicles, ); break; case "redirect": @@ -54,7 +65,7 @@ const LoginWithDimo: React.FC = ({ redirectUri, apiKey, permissionTemplateId, - vehicles + vehicles, ); break; default: @@ -68,6 +79,7 @@ const LoginWithDimo: React.FC = ({ embedAuth( onSuccess, onError, + setAuthenticated, dimoLogin, clientId, redirectUri, @@ -78,7 +90,7 @@ const LoginWithDimo: React.FC = ({ } }; - // Renders iframe for embed mode +// Renders iframe for embed mode const renderEmbedIframe = () => (