├── .prettierrc
├── .gitignore
├── tsconfig.json
├── package.json
├── LICENSE
├── README.md
└── index.ts
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": false,
3 | "trailingComma": "all",
4 | "semi": false
5 | }
6 |
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | .DS_Store
3 | dist
4 | dist-ssr
5 | *.local
6 | .dfx
7 | todo.txt
8 | canister_ids.json
9 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "ESNext",
4 | "module": "CommonJS",
5 | "moduleResolution": "Node",
6 | "outDir": "./dist",
7 | "declaration": true,
8 | "strict": true,
9 | "esModuleInterop": true,
10 | "forceConsistentCasingInFileNames": true,
11 | "skipLibCheck": true
12 | },
13 | "include": ["index.ts"]
14 | }
15 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ic-auth",
3 | "version": "1.1.0",
4 | "description": "A simple, modular package for integrating Internet Computer authentication providers into your app.",
5 | "author": "Daniel McCoy (https://danielmccoy.us/)",
6 | "license": "MIT",
7 | "keywords": [
8 | "ic",
9 | "internet computer",
10 | "auth",
11 | "authentication",
12 | "identity",
13 | "stoic",
14 | "dfinity"
15 | ],
16 | "repository": {
17 | "type": "git",
18 | "url": "https://github.com/id-daniel-mccoy/ic-auth.git"
19 | },
20 | "main": "dist/index.js",
21 | "types": "dist/index.d.ts",
22 | "scripts": {
23 | "build": "tsc"
24 | },
25 | "dependencies": {
26 | "@dfinity/agent": "2.4.1",
27 | "@dfinity/assets": "^2.4.1",
28 | "@dfinity/auth-client": "2.4.1",
29 | "@dfinity/candid": "2.4.1",
30 | "@dfinity/identity": "2.4.1",
31 | "@dfinity/principal": "2.4.1",
32 | "@nfid/identitykit": "^1.0.14",
33 | "ic-stoic-identity": "6.0.0"
34 | },
35 | "devDependencies": {
36 | "@types/node": "^16.11.6",
37 | "typescript": "^4.4.3"
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2024–2025 Daniel McCoy
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # IC-Auth
2 | ## Version 1.1.0
3 |
4 |
5 |
6 |
7 | **Welcome to IC-Auth! The easiest way to integrate Plug, Stoic, Internet Identity, and NFID authentication for your Internet Computer apps.**
8 |
9 |
10 |
11 | ## 📌 What can you do with IC-Auth?
12 |
13 | **IC-Auth** is a lightweight, modular TypeScript package that helps developers plug wallet authentication directly into their DFINITY dapps.\
14 | It gives you simple, type-safe helpers for the **most common IC wallets**, so you can focus on your frontend and canister logic to get up and running quickly.
15 |
16 |
17 |
18 | ## ✅ **Currently Supported Wallets**
19 |
20 | - **Plug Wallet**
21 | - **Internet Identity**
22 | - **NFID**
23 | - **Stoic**
24 |
25 | ---
26 |
27 | ## ⚠️ **Known Issues**
28 |
29 | - **Stoic:** A known issue with cookie storage affects Stoic logins in some browsers. This will be addressed in a future Stoic release.
30 | - **NFID:** Supports anonymous mode only for now — so canister calls work, but **payments and session delegation** are limited. This will be expanded soon.
31 |
32 | ---
33 |
34 | ## 🚀 **What’s New In Verson 1.x**
35 |
36 | - Fully modernized: Uses **latest DFINITY agent APIs** (`Agent` instead of `HttpAgent`).
37 | - Dependencies updated to **latest stable versions**.
38 | - **No more embedded frontend** — the package is framework-agnostic.
39 | - **Cleaner exports:** `UserObject` type is inline.
40 | - Local dev now supported for all providers via `host` parameter.
41 | - Removed legacy `hello` canister and embedded assets.
42 | - Designed to pair cleanly with any custom canister IDL.
43 |
44 | ---
45 |
46 | ## 📦 **Installation**
47 |
48 | ```bash
49 | npm install ic-auth
50 | ```
51 |
52 | ---
53 |
54 | ## ✨ **Build Your Login**
55 | These steps are for creating your own simple login UI/UX using the modular functions. A more advanced design will be shown below.
56 |
57 |
58 | ### ✅ **Step 1: Import**
59 |
60 | Import the login functions you want and the universal `UserObject` type:
61 |
62 | ```ts
63 | import { PlugLogin, StoicLogin, NFIDLogin, IdentityLogin, CreateActor, UserObject } from "ic-auth";
64 | ```
65 |
66 | ---
67 |
68 | ### ✅ **Step 2: Handle Your Login**
69 |
70 | Create a function to trigger the login and catch the data afterwards. If you're using Plug, it requires a list of canister addresses that you plan to make calls to. This is only required for Plug so we will show that here.
71 |
72 | You will also need to define the host URL that the authentication will be looking towards for login. Again, if not using Plug you only need
73 | to toss the host as a string.
74 |
75 | ```ts
76 | const canisterID = "oyjva-2yaaa-aaaam-qbaya-cai";
77 | const whitelist = [canisterID];
78 | const host = "https://icp0.io" // "https://localhost:XXXX" for local dfx instances.
79 |
80 | const handleLogin = async() => {
81 | const userObject = await PlugLogin(whitelist, host);
82 | console.log(userObject);
83 | // Handle code will go here...
84 | }
85 | ```
86 |
87 | ---
88 |
89 | ### ✅ **Step 3: Attach To Your UI**
90 |
91 | Take the login handler and attach it to a UI element of your choice.
92 |
93 | ```html
94 |
95 | ```
96 |
97 | ---
98 |
99 | ### ✅ **Using The Login - What is a UserObject?**
100 |
101 | After a successful login you should receive the `UserObject`, which looks like this:
102 |
103 | ```ts
104 | type UserObject = {
105 | principal: string,
106 | agent: Agent | undefined,
107 | provider: string
108 | }
109 | ```
110 |
111 | Reminder: You can import this specific type by adding `UserObject` into your imports directly from `ic-auth`.
112 |
113 | The `UserObject` can be used to create an actor for canister calls, display the user's principal address, or trigger code depending on the provider chosen. This next example shows how to create an actor using the `CreateActor` function.
114 |
115 | To create an actor you will need to pass in the canister address you wish to call, the associated canister interface description language (IDL) file, and the agent from the UserObject. You can generate the interfaces folder by running `dfx generate` after building your backend canister, then import from that folder.
116 |
117 | ```ts
118 | import { PlugLogin, StoicLogin, NFIDLogin, IdentityLogin, UserObject, CreateActor } from 'ic-auth';
119 | import { idlFactory as YourIDL } from "./interfaces/your_canister";
120 |
121 | const canisterID = "oyjva-2yaaa-aaaam-qbaya-cai";
122 | const whitelist = [canisterID];
123 | const host = "https://icp0.io"; // or your local dev host
124 |
125 | const handleLogin = async() => {
126 | const userObject = await PlugLogin(whitelist, host);
127 | console.log(userObject);
128 | const actor = await CreateActor(userObject.agent!, YourIDL, canisterID);
129 |
130 | // Handle code will go here...
131 | }
132 | ```
133 |
134 | Now you can style the elements, add more providers, or continue on to see a more complex and full featured example.
135 |
136 | ---
137 |
138 | ## 🎛️ **Multi-Provider Login Example**
139 |
140 | This example will bring you through the steps of creating a multi-wallet supported login menu.
141 |
142 | ---
143 |
144 | ### **1️⃣ Import + Define Everything**
145 |
146 | ```ts
147 | import { PlugLogin, StoicLogin, NFIDLogin, IdentityLogin, UserObject, CreateActor } from 'ic-auth';
148 |
149 | const canisterID = "oyjva-2yaaa-aaaam-qbaya-cai";
150 | const whitelist = [canisterID];
151 | const host = "https://icp0.io"; // or your local dev host
152 | ```
153 |
154 | ---
155 |
156 | ### **2️⃣ Create a Unified Handler**
157 |
158 | ```ts
159 | const handleLogin = async (provider: string) => {
160 | let user: UserObject = {
161 | principal: "Not Connected.",
162 | agent: undefined,
163 | provider: "N/A"
164 | };
165 |
166 | if (provider === "Plug") {
167 | user = await PlugLogin(whitelist, host);
168 | } else if (provider === "Stoic") {
169 | user = await StoicLogin(host);
170 | } else if (provider === "NFID") {
171 | user = await NFIDLogin(host);
172 | } else if (provider === "Identity") {
173 | user = await IdentityLogin(host);
174 | }
175 |
176 | console.log(user);
177 |
178 | const actor = await CreateActor(user.agent!, YourIDL, canisterID);
179 | // Interact with your canister here
180 | };
181 | ```
182 |
183 | ---
184 |
185 | ### **3️⃣ Attach Buttons to Your UI**
186 |
187 | ```html
188 |