@uniquei/react 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # @uniquei/react
2
+
3
+ A lightweight SSO authentication SDK for Anywhere platform.
4
+ Easily integrate login and user fetching in React applications with just a few lines of code.
5
+
6
+ ---
7
+
8
+ ## Features
9
+
10
+ - One-line login redirect (`AnywhereAuth.login()`)
11
+ - Automatically handles SSO token from URL and localStorage
12
+ - Fetch user profile (`AnywhereAuth.getUser()`)
13
+ - Logout (`AnywhereAuth.logout()`)
14
+ - Optional React hook for automatic user state (`useAnywhereUser()`)
15
+ - Built-in TypeScript support
16
+
17
+ ---
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install @uniquei/react
23
+ # or
24
+ yarn add @uniquei/react
@@ -0,0 +1,21 @@
1
+ type User = {
2
+ $id: string;
3
+ name: string;
4
+ username: string;
5
+ email: string;
6
+ bio: string;
7
+ avatar: string;
8
+ $createdAt: string;
9
+ };
10
+ declare class AnywhereAuthClass {
11
+ private get token();
12
+ private set token(value);
13
+ private handleTokenFromUrl;
14
+ init(): void;
15
+ getUser(): Promise<User | null>;
16
+ login(): void;
17
+ logout(): void;
18
+ }
19
+ declare const AnywhereAuth: AnywhereAuthClass;
20
+
21
+ export { AnywhereAuth, type User };
@@ -0,0 +1,21 @@
1
+ type User = {
2
+ $id: string;
3
+ name: string;
4
+ username: string;
5
+ email: string;
6
+ bio: string;
7
+ avatar: string;
8
+ $createdAt: string;
9
+ };
10
+ declare class AnywhereAuthClass {
11
+ private get token();
12
+ private set token(value);
13
+ private handleTokenFromUrl;
14
+ init(): void;
15
+ getUser(): Promise<User | null>;
16
+ login(): void;
17
+ logout(): void;
18
+ }
19
+ declare const AnywhereAuth: AnywhereAuthClass;
20
+
21
+ export { AnywhereAuth, type User };
package/dist/index.mjs ADDED
@@ -0,0 +1,49 @@
1
+ // src/index.ts
2
+ var CLIENT_URL = "https://act-sys.vercel.app";
3
+ var DEVS_URL = "https://actx.vercel.app";
4
+ var AnywhereAuthClass = class {
5
+ get token() {
6
+ if (typeof window === "undefined") return null;
7
+ return localStorage.getItem("anywhere_sso_token");
8
+ }
9
+ set token(value) {
10
+ if (typeof window === "undefined") return;
11
+ if (value) localStorage.setItem("anywhere_sso_token", value);
12
+ else localStorage.removeItem("anywhere_sso_token");
13
+ }
14
+ handleTokenFromUrl() {
15
+ if (typeof window === "undefined") return;
16
+ const params = new URLSearchParams(window.location.search);
17
+ const token = params.get("token");
18
+ if (token) {
19
+ this.token = token;
20
+ window.history.replaceState({}, document.title, window.location.pathname);
21
+ }
22
+ }
23
+ init() {
24
+ this.handleTokenFromUrl();
25
+ }
26
+ async getUser() {
27
+ if (!this.token) return null;
28
+ const res = await fetch(`${DEVS_URL}/api/users/fetch`, {
29
+ headers: {
30
+ Authorization: `Bearer ${this.token}`
31
+ }
32
+ });
33
+ if (!res.ok) return null;
34
+ return res.json();
35
+ }
36
+ login() {
37
+ const redirect = encodeURIComponent(
38
+ `${window.location.origin}/`
39
+ );
40
+ window.location.href = `${CLIENT_URL}/api/sso?redirect=${redirect}`;
41
+ }
42
+ logout() {
43
+ this.token = null;
44
+ }
45
+ };
46
+ var AnywhereAuth = new AnywhereAuthClass();
47
+ export {
48
+ AnywhereAuth
49
+ };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@uniquei/react",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
7
7
  "dist"
8
8
  ],
9
9
  "scripts": {
10
- "build": "tsup src/index.ts"
10
+ "build": "tsup src/index.ts --format esm,cjs --dts"
11
11
  },
12
12
  "devDependencies": {
13
13
  "tsup": "^8.5.1",