ic-auth
Version:
A simple to use, modular package for integrating Internet Computer authentication providers into your app.
164 lines (163 loc) • 6.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateAnonAgent = exports.CreateActor = exports.IdentityLogin = exports.NFIDLogin = exports.PlugLogin = exports.StoicLogin = exports.HelloIDL = exports.Types = void 0;
const principal_1 = require("@dfinity/principal");
const agent_1 = require("@dfinity/agent");
const auth_client_1 = require("@dfinity/auth-client");
const types = require("./frontend/types");
exports.Types = types;
const hello = require("./frontend/interfaces/hello");
//@ts-ignore
const ic_stoic_identity_1 = require("ic-stoic-identity");
exports.HelloIDL = hello.idlFactory;
// A simple Stoic login flow with simplified return data.
const StoicLogin = async () => {
let identity;
//@ts-ignore
identity = await ic_stoic_identity_1.StoicIdentity.load().then(async (identity) => {
const userIdentity = await ic_stoic_identity_1.StoicIdentity.connect();
return userIdentity;
});
const agent = new agent_1.HttpAgent({ identity, host: "https://ic0.app" });
const principal = identity.getPrincipal().toText();
const returnObject = {
principal: principal,
agent: agent,
provider: "Stoic"
};
console.log("Connected!");
console.log(returnObject);
return returnObject;
};
exports.StoicLogin = StoicLogin;
// Full featured Plug wallet login with simplified return data.
const PlugLogin = async (whitelist) => {
const isConnected = await window.ic.plug.isConnected();
if (isConnected) {
await window.ic.plug.createAgent({ whitelist });
const agent = window.ic.plug.agent;
const principal = (await agent.getPrincipal()).toText();
const returnObject = {
principal: principal,
agent: agent,
provider: "Plug"
};
console.log("Connected!");
console.log(returnObject);
return returnObject;
}
else {
await window.ic.plug.requestConnect({ whitelist });
const agent = window.ic.plug.agent;
const principal = (await agent.getPrincipal()).toText();
const returnObject = {
principal: principal,
agent: agent,
provider: "Plug"
};
console.log("Connected!");
console.log(returnObject);
return returnObject;
}
};
exports.PlugLogin = PlugLogin;
// A full featured NFID login flow with simplified return data.
const NFIDLogin = async () => {
return new Promise(async (resolve, reject) => {
let identity;
const appName = "wallet-testing";
const appLogo = "https://nfid.one/icons/favicon-96x96.png";
const authPath = "/authenticate/?applicationName=" + appName + "&applicationLogo=" + appLogo + "#authorize";
const authUrl = "https://nfid.one" + authPath;
let userObject = {
principal: "Not Connected.",
agent: undefined,
provider: "N/A",
};
const authClient = await auth_client_1.AuthClient.create();
await authClient.login({
identityProvider: authUrl,
onSuccess: async () => {
try {
identity = authClient.getIdentity();
const agent = new agent_1.HttpAgent({ identity: identity, host: "https://ic0.app" });
console.log(await agent.getPrincipal());
userObject = {
principal: principal_1.Principal.from(identity.getPrincipal()).toText(),
agent: agent,
provider: "NFID",
};
console.log("Connected!");
resolve(userObject);
}
catch (error) {
console.error("Error in onSuccess:", error);
reject(error);
}
},
onError: async (error) => {
console.log("Login Failed:\n\n" + error);
reject(error);
},
});
});
};
exports.NFIDLogin = NFIDLogin;
// A fully featured Internet Identity login flow with simplified return data.
const IdentityLogin = async () => {
return new Promise(async (resolve, reject) => {
let identity;
let userObject = {
principal: "Not Connected.",
agent: undefined,
provider: "N/A",
};
try {
const authClient = await auth_client_1.AuthClient.create();
await authClient.login({
identityProvider: "https://identity.ic0.app",
onSuccess: async () => {
identity = authClient.getIdentity();
userObject = {
principal: principal_1.Principal.from(identity.getPrincipal()).toText(),
agent: new agent_1.HttpAgent({ identity, host: "https://ic0.app" }),
provider: "Internet Identity",
};
console.log("Connected!");
console.log(userObject);
resolve(userObject);
},
onError: async (error) => {
userObject = {
principal: "Not Connected.",
agent: undefined,
provider: "N/A",
};
console.log("Error Logging In");
reject(error);
},
});
}
catch (error) {
console.error("Error creating AuthClient:", error);
reject(error);
}
});
};
exports.IdentityLogin = IdentityLogin;
// A basic actor creation flow for calling canisters.
const CreateActor = async (agent, idl, canisterId) => {
const actor = agent_1.Actor.createActor(idl, {
agent: agent,
canisterId: canisterId
});
console.log(actor);
return actor;
};
exports.CreateActor = CreateActor;
// Creates an agent with no identity, can be passed into CreateActor for making non permissioned canister calls.
const CreateAnonAgent = async () => {
const agent = new agent_1.HttpAgent({ host: "https://ic0.app" });
return agent;
};
exports.CreateAnonAgent = CreateAnonAgent;