@code_district/doorman
Version:
Doorman: A comprehensive React package for seamless authentication and authorization management. Easily integrate secure user authentication and access control in your applications. Streamline user verification, role-based permissions, and secure data han
401 lines (361 loc) • 13.3 kB
JavaScript
// import logo from "./logo.svg";
import React, { useState } from "react";
import Cookies from "js-cookie";
import axios from "axios";
import "./App.css";
import { DoormanLogin } from "./features/login/login";
import { DoormanLoginNoUI } from "./features/login/loginNoUI";
import { DoormanSignup } from "./features/register/register";
import { DoormanSignupNoUI } from "./features/register/registerNoUI";
import { DoormanVerifyEmail } from "./features/register/verifyEmail";
import { DoormanVerifyEmailNoUI } from "./features/register/verifyEmailNoUI";
import { DoormanForgotPassword } from "./features/forgotPassword/forgotPassword";
import { DoormanForgotPasswordNoUI } from "./features/forgotPassword/forgotPasswordNoUI";
import { DoormanSetPassword } from "./features/forgotPassword/setPassword";
import { DoormanSetPasswordNoUI } from "./features/forgotPassword/setPasswordNoUI";
import { DoormanChangePassword } from "./features/changePassword/changePassword";
import { DoormanChangePasswordNoUI } from "./features/changePassword/changePasswordNoUI";
import { DoormanConfigure } from "./config/config";
import { DoormanTenantInfoByDomainPrefix } from "./features/tenant/tenantInfoByDomainPrefix";
import { DoormanInitializeUser } from "./features/user/initializeUser";
import { DoormanGetLoggedInUserTenant } from "./features/user/getLoggedInUserTenant";
import { DoormanGetUserFromCookie } from "./features/tenant/getDecryptedUserFromCookie";
// let config = {
// userPoolId: "us-east-1_dJWIGVkJI",
// clientId: "2593usjcqoqc2ik7hh6bg6a0pg",
// cookieDomain: "localhost",
// apiGatewayUrl: "https://283ptq2qbc.execute-api.us-east-1.amazonaws.com/dev",
// poolName: "",
// // userPoolId: "us-east-1_mdk8mFWNv",
// // clientId: "1liau61478ov95tcd3p3u9j3kt",
// // cookieDomain: "localhost",
// // style: {
// // inputStyle: {
// // backgroundColor: "white",
// // color: "black",
// // fontStyle: "italic",
// // fontSize: "14px",
// // },
// // buttonStyle: {
// // backgroundColor: "darkslategray",
// // },
// // boxStyle: { backgroundColor: "gainsboro" },
// // primaryColor: "purple",
// // },
// };
let config = {
// Required
// userPoolId: "us-east-1_dJWIGVkJI",
// clientId: "2593usjcqoqc2ik7hh6bg6a0pg",
// cookieDomain: "110.39.11.117",
// cookieDomain: "localhost",
userPoolId: "us-east-1_mdk8mFWNv",
clientId: "1liau61478ov95tcd3p3u9j3kt",
cookieDomain: "localhost",
apiGatewayUrl: "https://283ptq2qbc.execute-api.us-east-1.amazonaws.com/dev",
// Optional
style: {
inputStyle: {
backgroundColor: "#F9F9FC",
height: "40px",
borderRadius: "8px",
},
buttonStyle: {
height: "40px",
borderRadius: "8px",
},
boxStyle: {
border: "none",
margin: "auto",
backgroundColor: "transparent",
},
primaryColor: "#883DCF",
},
//Add more configuration options as needed
};
DoormanConfigure(config);
function App() {
const [email, setEmail] = useState("");
const [cognitoId, setCognitoId] = useState("");
const onLoginSuccess = async (tokens, permissions) => {
console.log("Tokens", tokens);
console.log("Permissions from onAuthSuccess", permissions);
// new Promise((resolve, reject) => {
// DoormanInitializeUser()
// .then((userPermissions) => {
// // Resolve the promise with the userPermissions data
// console.log("user permissions", userPermissions);
// resolve(userPermissions);
// })
// .catch((error) => {
// // Reject the promise with the error
// reject(error);
// });
// });
alert("User logged in successfully");
const body = {};
// const idToken = val?.idToken?.jwtToken;
console.log("Val", val);
const idToken = Cookies.get("doorman_user");
console.log("ID token", idToken);
await axios
.post(
"https://uv71v862e8.execute-api.us-east-1.amazonaws.com/dev-08-01/auth/signin",
body,
{
headers: {
Authorization: `Bearer ${idToken}`,
},
}
)
.then((res) => {
console.log("res", res.data.message);
alert(res.data.message);
})
.catch((err) => {
console.log("errr", err);
});
console.log("Driver Success: ", val);
};
const onLoginFail = (err) => {
console.error("Driver Fail: ", err);
// alert(err);
};
const onVerifySuccess = async (res) => {
console.log("res", res);
// const body = {
// email,
// };
// const idToken = Cookies.get("doorman_user");
// await axios.patch(
// "https://uv71v862e8.execute-api.us-east-1.amazonaws.com/dev-08-01/auth/verify-email",
// body,
// {
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ZCh6sWONR0fYdWtg1x2L8-_MQ2daaxs86ynRIojcM4dOYfkwui5qF9uCtMc_V8BfMZ7tFMdVYayxxnBaWWOf1b4jKQ",
// },
// }
// );
};
const onCodeResendSuccess = (res) => {
console.log("resend success", res);
};
const onVerifyFail = (err) => {
console.log("on verify email fail", err);
};
const handleLogoutClick = async () => {
console.log("In handle logout");
const logoutMessage = await DoormanLogout();
console.log("Message returned from component: ", logoutMessage);
// return (
// <DoormanLogout
// onSuccess={(msg) => console.log("Message from component", msg)}
// />
// DoormanLogout();
// );
};
const checkAllPermissions = () => {
// const hasPermission = DoormanCheckUserPermissions("CPA-0");
const hasPermission = DoormanUserHasAllPermissions([
"CPA-01",
"APA-01",
"APA-05",
]);
console.log("Has All Permission", hasPermission);
};
const checkAnyPermission = () => {
// const hasPermission = DoormanCheckUserPermissions("CPA-0");
const hasPermission = DoormanUserHasAnyPermission(["CPA-01", "sd"]);
console.log("Has Any Permission", hasPermission);
};
const getLoggedInUserTenantInfo = async () => {
// Example usage to check if the logged-in user has a tenant
const tenantInfo = DoormanGetLoggedInUserTenant();
const publictenantInfo = await DoormanTenantInfoByDomainPrefix({
domainPrefix: "Tkxel",
});
console.log("public Tenent Info", publictenantInfo);
if (tenantInfo) {
console.log("Tenant info retrieved successfully:", tenantInfo);
} else {
console.log("No tenant found for the logged-in user.");
}
};
const getTenantInfoByDomainPrefix = async () => {
// Example usage to check if the logged-in user has a tenant
console.log("Get Tenant Info By Domain Prefix");
try {
const tenantInfo = await DoormanTenantInfoByDomainPrefix({
domainPrefix: "Tkxel",
apiGatewayUrl: config.apiGatewayUrl,
});
console.log("Tenant info retrieved successfully:", tenantInfo);
} catch (error) {
console.log("No tenant found for the logged-in user.");
}
};
const getDoormanCookie = async () => {
// Example usage to check if the logged-in user has a tenant
console.log("Get Cookie User ");
const doormanInfo = DoormanGetUserFromCookie();
if (doormanInfo) {
console.log(" info retrieved successfully:", doormanInfo);
} else {
console.log("No found for the logged-in user.");
}
};
let onAuthSuccess = (authData, permissions) => {
// Handle successful login
console.log("Login successful:", authData, permissions);
};
let onAuthFail = (error) => {
// Handle login failure
console.error("Login failed:", error);
};
const onSuccess = (message) => {
// Handle successful password reset initiation
console.log("Password reset successful:", message);
};
const onFail = (error) => {
// Handle password reset initiation failure
console.error("Password reset failed:", error);
};
const onSignupSuccess = (message) => {
// Handle successful signup
console.log("Signup successful:", message);
};
const onSignupFail = (error) => {
// Handle signup failure
console.error("Signup failed:", error);
};
/* No UI Components
DoormanLoginNoUI({
email: "saim.ashfaq@codedistrict.com",
password: "Codedistrict@123",
// newPassword: "jk",
onAuthSuccess,
onAuthFail,
});
DoormanSignupNoUI({
email: "saim.ashfaq+100@codedistrict.com",
password: "Codedistrict@123",
onSuccess: onSignupSuccess,
onFail: onSignupFail,
});
DoormanResendCodeNoUI({
email: "saim.ashfaq+101@codedistrict.com",
onSuccess,
onFail,
});
DoormanVerifyEmailNoUI({
email: "saim.ashfaq+101@codedistrict.com",
verificationCode: "175038",
onSuccess: onVerifySuccess,
onFail: onVerifyFail,
});
DoormanChangePasswordNoUI({
oldPassword: "Codedistrict@123",
newPassword: "Codedistrict@123",
onSuccess,
onFail,
});
DoormanForgotPasswordNoUI({
email: "saim.ashfaq+789@codedistrict.com",
onSuccess,
onFail,
});
DoormanSetPasswordNoUI({
email: "saim.ashfaq+789@codedistrict.com",
verificationCode: "212933",
password: "Codedistrict@123",
onSuccess,
onFail,
}); */
return (
<div className="App">
<DoormanLogin
onAuthSuccess={onLoginSuccess}
onAuthFail={onLoginFail}
showSignupLink={true}
/>
<button onClick={getLoggedInUserTenantInfo}>
Get Logged In User Tenant Info
</button>
<button onClick={getTenantInfoByDomainPrefix}>
Get Tenant Info By Doamin Prefix
</button>
<button onClick={getDoormanCookie}>Get User Cookie</button>
<button
// onClick={() => {
// console.log("In logout");
// DoormanLogout();
// }}
onClick={handleLogoutClick}
>
Logout
</button>
{/* <DoormanLogout
onSuccess={(res) => console.log("res", res)}
onFail={(err) => console.log("Err", err)}
/> */}
<DoormanSignup onSuccess={onSignupSuccess} onFail={onSignupFail} />
<DoormanVerifyEmail
email={"waliullah.asim+01@codedistrict.com"}
onVerifySuccess={onVerifySuccess}
onResendSuccess={onCodeResendSuccess}
onVerifyFail={onVerifyFail}
onResendFail={(err) => console.log("Err resending code", err)}
/>
{/* <DoormanForgotPassword
onSuccess={(val) => console.log("val", val)}
onFail={(err) => console.log("err in forgot password", err)}
/> */}
<DoormanSetPassword
onSuccess={(val) => {
console.log("Val in set password", val);
alert(val);
}}
onResendSuccess={(res) => console.log("Resend success", res)}
onFail={(err) => console.log("Err in set password", err)}
onResendFail={(err) => console.log("Err resending code", err)}
email={"waliullah.asim+01@codedistrict.com"}
/>
<DoormanChangePassword
onSuccess={(val) => console.log("val", val)}
onFail={(err) => console.log("err changing password", err)}
// email={"saim.ashfaq@codedistrict.com"}
/>
{/* <DoormanLogin onAuthSuccess={onLoginSuccess} onAuthFail={onLoginFail} />
<DoormanSignup onSuccess={onSignupSuccess} onFail={onSignupFail} />
<DoormanVerifyEmail
onVerifySuccess={onVerifySuccess}
onVerifyFail={onVerifyFail}
onResendSuccess={onCodeResendSuccess}
onResendFail={(err) => console.log("err", err)}
email={"saim.ashfaq+171@codedistrict.com"}
/>
<DoormanForgotPassword
onSuccess={(val) => console.log("val", val)}
onFail={(err) => console.log("err in forgot password", err)}
/>
<DoormanSetPassword
onSuccess={(val) => {
console.log("Val in set password", val);
alert(val);
}}
onResendSuccess={(res) => console.log("Resend success", res)}
onResendFail={(err) => console.log("err", err)}
onFail={(err) => console.log("Err in set password", err)}
email={"saim.ashfaq+101@codedistrict.com"}
/>
<DoormanChangePassword
onSuccess={(val) => console.log("val", val)}
onFail={(err) => console.log("err changing password", err)}
email={"saim.ashfaq+101@codedistrict.com"}
/> */}
</div>
);
}
export default App;