Skip to content

Commit

Permalink
Merge pull request #35 from DIMO-Network/development
Browse files Browse the repository at this point in the history
v0.0.11: Logout Support, Use Types, Cleanup Label #34
  • Loading branch information
MoizAhmedd authored Dec 5, 2024
2 parents 50442e1 + 1c3126c commit edfbeae
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 216 deletions.
14 changes: 2 additions & 12 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,7 @@

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright (c) 2024 DIMO-Network

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -199,3 +188,4 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Binary file modified example-dimo-auth/dimo-network-login-with-dimo-0.0.10.tgz
Binary file not shown.
Binary file not shown.
310 changes: 156 additions & 154 deletions example-dimo-auth/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example-dimo-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@types/node": "^16.18.114",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.3.1",
"@dimo-network/login-with-dimo": "file:./dimo-network-login-with-dimo-0.0.10.tgz",
"@dimo-network/login-with-dimo": "file:./dimo-network-login-with-dimo-0.0.11.tgz",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
Expand Down
29 changes: 17 additions & 12 deletions example-dimo-auth/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,15 @@ function App() {
<div>
<h3>Popup Example</h3>

{isAuthenticated ? (
<LoginWithDimo
mode="popup"
onSuccess={(authData: any) => console.log("Success:", authData)}
onError={(error: any) => console.error("Error:", error)}
permissionTemplateId={permissionsEnabled ? "1" : undefined}
// vehicles={["585","586"]}
/>

{isAuthenticated && (
<>
<ShareVehiclesWithDimo
mode="popup"
Expand All @@ -1143,8 +1151,11 @@ function App() {

<ExecuteAdvancedTransactionWithDimo
mode="popup"
onSuccess={(transactionHash: any) =>
console.log("Success:", transactionHash)
onSuccess={(transactionData: any) =>
console.log(
"Transaction Hash:",
transactionData.transactionHash
)
}
onError={(error: any) => console.error("Error:", error)}
address="0x21cFE003997fB7c2B3cfe5cf71e7833B7B2eCe10"
Expand All @@ -1154,14 +1165,6 @@ function App() {
args={["0x62b98e019e0d3e4A1Ad8C786202e09017Bd995e1", "0"]}
/>
</>
) : (
<LoginWithDimo
mode="popup"
onSuccess={(authData: any) => console.log("Success:", authData)}
onError={(error: any) => console.error("Error:", error)}
permissionTemplateId={permissionsEnabled ? "1" : undefined}
// vehicles={["585","586"]}
/>
)}
</div>

Expand Down Expand Up @@ -1195,7 +1198,9 @@ function App() {

<ExecuteAdvancedTransactionWithDimo
mode="redirect"
onSuccess={(authData: any) => console.log("Success:", authData)}
onSuccess={(transactionData: any) =>
console.log("Transaction Hash:", transactionData.transactionHash)
}
onError={(error: any) => console.error("Error:", error)}
address="0x21cFE003997fB7c2B3cfe5cf71e7833B7B2eCe10"
value="0"
Expand Down
6 changes: 3 additions & 3 deletions sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import {
<ExecuteAdvancedTransactionWithDimo
mode="popup"
onSuccess={(transactionHash: any) =>
onSuccess={(transactionData: any) =>
console.log("Success:", transactionHash)
}
onError={(error: any) => console.error("Error:", error)}
Expand Down Expand Up @@ -146,8 +146,8 @@ initializeDimoSDK({
<ExecuteAdvancedTransactionWithDimo
mode="popup"
onSuccess={(transactionHash: any) =>
console.log("Success:", transactionHash)
onSuccess={(transactionData: any) =>
console.log("Transaction Hash:", transactionData.transactionHash)
}
onError={(error: any) => console.error("Error:", error)}
address="0x21cFE003997fB7c2B3cfe5cf71e7833B7B2eCe10"
Expand Down
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dimo-network/login-with-dimo",
"version": "0.0.10",
"version": "0.0.11",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/components/LoginWithDimo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const LoginWithDimo: React.FC<LoginWithDimoProps> = ({
onSuccess={onSuccess}
onError={onError}
buttonLabel={(authenticated) =>
authenticated ? "Connected with DIMO" : "Continue with DIMO"
authenticated ? "Manage DIMO Account" : "Continue with DIMO"
} // Dynamic label based on auth state
disableIfAuthenticated={true} // Disable button when authenticated
disableIfAuthenticated={false} // Disable button when authenticated
payload={{ permissionTemplateId, vehicles, vehicleMakes, eventType: "SHARE_VEHICLES_DATA" }}
/>
);
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/enums/globalEnums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export enum EntryState {
export enum MessageEventType {
READY = "READY",
AUTH_INIT = "AUTH_INIT",
TRANSACTION_RESPONSE = "transactionResponse",
LOGOUT = "logout"
}

export enum DimoSDKModes {
Expand Down
6 changes: 6 additions & 0 deletions sdk/src/storage/storageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ export const getEmailFromLocalStorage = (): string | null => {
const email = localStorage.getItem("dimo_user_email");
return email;
};

export const clearSessionData = (): void => {
document.cookie = `dimo_auth_token=; Max-Age=0`; // Expire JWT cookie immediately
localStorage.removeItem(`dimo_wallet_address`); // Remove user data from localStorage
localStorage.removeItem(`dimo_user_email`); // Remove user data from localStorage
};
29 changes: 29 additions & 0 deletions sdk/src/utils/authUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
clearSessionData,
storeEmailInLocalStorage,
storeJWTInCookies,
storeWalletAddressInLocalStorage,
} from "../storage/storageManager";

export const processAuthResponse = (
{ token, walletAddress, email }: any,
setAuthenticated: (status: boolean) => void,
onSuccess: (data: {
token: string;
transactionHash?: string;
transactionReceipt?: any;
}) => void
) => {
if (walletAddress) storeWalletAddressInLocalStorage(walletAddress);
if (email) storeEmailInLocalStorage(email);
if (token) {
storeJWTInCookies(token);
setAuthenticated(true);
onSuccess({ token });
}
};

export const logout = (setAuthenticated: (status: boolean) => void) => {
clearSessionData();
setAuthenticated(false);
};
47 changes: 16 additions & 31 deletions sdk/src/utils/eventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { EntryState } from "../enums/globalEnums";
import {
storeEmailInLocalStorage,
storeJWTInCookies,
storeWalletAddressInLocalStorage,
} from "../storage/storageManager";
import { MessageEventType } from "../enums/globalEnums";
import { BasePayload } from "../types/BasePayload";
import { TransactionData } from "../types/TransactionData";
import { logout, processAuthResponse } from "./authUtils";

/**
* @file eventHandler.ts
Expand Down Expand Up @@ -47,24 +42,6 @@ const sendMessageToTarget = (
}
};

const processAuthResponse = (
{ token, walletAddress, email }: any,
setAuthenticated: (status: boolean) => void,
onSuccess: (data: {
token: string;
transactionHash?: string;
transactionReceipt?: any;
}) => void
) => {
if (walletAddress) storeWalletAddressInLocalStorage(walletAddress);
if (email) storeEmailInLocalStorage(email);
if (token) {
storeJWTInCookies(token);
setAuthenticated(true);
onSuccess({ token });
}
};

// Popup Handler
export const handleMessageForPopup = (
basePayload: BasePayload,
Expand Down Expand Up @@ -96,13 +73,13 @@ export const handleMessageForPopup = (
} = event.data;

if (mode === "popup") {
if (eventType === "READY") {
if (eventType === MessageEventType.READY) {
const initialMessage = {
clientId,
redirectUri,
apiKey,
entryState,
eventType: "AUTH_INIT",
eventType: MessageEventType.AUTH_INIT,
};
sendMessageToTarget(popup, initialMessage, expectedOrigin, onError);
}
Expand All @@ -121,10 +98,14 @@ export const handleMessageForPopup = (
if (popup && !popup.closed) popup.close();
}

if (eventType === "transactionResponse" && transactionHash) {
if (eventType === MessageEventType.TRANSACTION_RESPONSE && transactionHash) {
onSuccess({ token: "", transactionHash });
}

if (eventType === MessageEventType.LOGOUT) {
logout(setAuthenticated);
}

if (eventType === "DIMO_ERROR") {
onError(new Error(message));
}
Expand Down Expand Up @@ -165,13 +146,13 @@ export const handleMessageForEmbed = (basePayload: BasePayload, data: any) => {
} = event.data;

if (mode === "embed") {
if (eventType === "READY") {
if (eventType === MessageEventType.READY) {
const initialMessage = {
clientId,
redirectUri,
apiKey,
entryState,
eventType: "AUTH_INIT",
eventType: MessageEventType.AUTH_INIT,
};
//@ts-ignore
sendMessageToTarget(iframe?.contentWindow, initialMessage, dimoLogin, onError);
Expand All @@ -189,14 +170,18 @@ export const handleMessageForEmbed = (basePayload: BasePayload, data: any) => {
onSuccess
);

if (eventType === "transactionResponse") {
if (eventType === MessageEventType.TRANSACTION_RESPONSE) {
if (transactionHash || transactionReceipt) {
onSuccess({ token: "", transactionHash, transactionReceipt });
} else {
onError(new Error("Could not execute transaction"));
}
}

if (eventType === MessageEventType.LOGOUT) {
logout(setAuthenticated);
}

if (eventType === "DIMO_ERROR") {
onError(new Error(message));
}
Expand Down

0 comments on commit edfbeae

Please sign in to comment.