@oneid-sdk/react 0.1.0
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/dist/index.d.ts +2 -0
- package/dist/index.js +7 -0
- package/dist/provider.d.ts +36 -0
- package/dist/provider.js +103 -0
- package/dist/useOneID.d.ts +32 -0
- package/dist/useOneID.js +12 -0
- package/package.json +25 -0
- package/src/index.ts +2 -0
- package/src/provider.tsx +140 -0
- package/src/useOneID.ts +10 -0
- package/tsconfig.json +13 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOneID = exports.OneIDProvider = void 0;
|
|
4
|
+
var provider_1 = require("./provider");
|
|
5
|
+
Object.defineProperty(exports, "OneIDProvider", { enumerable: true, get: function () { return provider_1.OneIDProvider; } });
|
|
6
|
+
var useOneID_1 = require("./useOneID");
|
|
7
|
+
Object.defineProperty(exports, "useOneID", { enumerable: true, get: function () { return useOneID_1.useOneID; } });
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
type OneIDConfig = {
|
|
3
|
+
appId: string;
|
|
4
|
+
apiBase?: string;
|
|
5
|
+
namespaceId?: string;
|
|
6
|
+
organizationId?: string;
|
|
7
|
+
environment?: string;
|
|
8
|
+
};
|
|
9
|
+
type UserData = {
|
|
10
|
+
id: string;
|
|
11
|
+
email: string;
|
|
12
|
+
namespaceId?: string;
|
|
13
|
+
environmentId?: string;
|
|
14
|
+
roles?: string[];
|
|
15
|
+
};
|
|
16
|
+
type Session = {
|
|
17
|
+
token: string | null;
|
|
18
|
+
user?: UserData;
|
|
19
|
+
};
|
|
20
|
+
type OneIDContextValue = {
|
|
21
|
+
config: OneIDConfig;
|
|
22
|
+
session: Session;
|
|
23
|
+
user: UserData | null;
|
|
24
|
+
token: string | null;
|
|
25
|
+
isLoading: boolean;
|
|
26
|
+
login: (email: string, password: string) => Promise<void>;
|
|
27
|
+
register: (email: string, password: string, organizationId?: string) => Promise<void>;
|
|
28
|
+
logout: () => void;
|
|
29
|
+
refresh: () => Promise<void>;
|
|
30
|
+
};
|
|
31
|
+
export declare const OneIDContext: React.Context<OneIDContextValue | undefined>;
|
|
32
|
+
export declare const OneIDProvider: ({ children, config, }: {
|
|
33
|
+
children: React.ReactNode;
|
|
34
|
+
config: OneIDConfig;
|
|
35
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
36
|
+
export {};
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.OneIDProvider = exports.OneIDContext = void 0;
|
|
7
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
+
const react_1 = require("react");
|
|
9
|
+
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
10
|
+
const js_cookie_1 = __importDefault(require("js-cookie"));
|
|
11
|
+
exports.OneIDContext = (0, react_1.createContext)(undefined);
|
|
12
|
+
const OneIDProvider = ({ children, config, }) => {
|
|
13
|
+
const apiBase = config.apiBase || "http://localhost:4100";
|
|
14
|
+
const environment = config.environment || "dev";
|
|
15
|
+
const tokenKey = `oneid_token_${environment}`;
|
|
16
|
+
const [session, setSession] = (0, react_1.useState)({ token: null });
|
|
17
|
+
const [isLoading, setIsLoading] = (0, react_1.useState)(true);
|
|
18
|
+
(0, react_1.useEffect)(() => {
|
|
19
|
+
const existing = js_cookie_1.default.get(tokenKey);
|
|
20
|
+
if (existing) {
|
|
21
|
+
setSession({ token: existing });
|
|
22
|
+
// Verify token/get user info
|
|
23
|
+
(0, cross_fetch_1.default)(`${apiBase}/v1/auth/me`, {
|
|
24
|
+
headers: { Authorization: `Bearer ${existing}` },
|
|
25
|
+
})
|
|
26
|
+
.then((res) => {
|
|
27
|
+
if (res.ok)
|
|
28
|
+
return res.json();
|
|
29
|
+
throw new Error("Session invalid");
|
|
30
|
+
})
|
|
31
|
+
.then((json) => {
|
|
32
|
+
setSession({ token: existing, user: json.user });
|
|
33
|
+
})
|
|
34
|
+
.catch(() => {
|
|
35
|
+
js_cookie_1.default.remove(tokenKey);
|
|
36
|
+
setSession({ token: null });
|
|
37
|
+
})
|
|
38
|
+
.finally(() => setIsLoading(false));
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
setIsLoading(false);
|
|
42
|
+
}
|
|
43
|
+
}, [apiBase, tokenKey]);
|
|
44
|
+
const value = (0, react_1.useMemo)(() => ({
|
|
45
|
+
config,
|
|
46
|
+
session,
|
|
47
|
+
user: session.user || null,
|
|
48
|
+
token: session.token,
|
|
49
|
+
isLoading,
|
|
50
|
+
logout: () => {
|
|
51
|
+
js_cookie_1.default.remove(tokenKey);
|
|
52
|
+
setSession({ token: null });
|
|
53
|
+
},
|
|
54
|
+
login: async (email, password) => {
|
|
55
|
+
const body = { email, password, appId: config.appId, environment };
|
|
56
|
+
const res = await (0, cross_fetch_1.default)(`${apiBase}/v1/auth/login`, {
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: { "Content-Type": "application/json" },
|
|
59
|
+
body: JSON.stringify(body),
|
|
60
|
+
});
|
|
61
|
+
if (!res.ok) {
|
|
62
|
+
const errJson = await res.json().catch(() => ({ message: "Login failed" }));
|
|
63
|
+
throw new Error(errJson.message || "Login failed");
|
|
64
|
+
}
|
|
65
|
+
const json = (await res.json());
|
|
66
|
+
js_cookie_1.default.set(tokenKey, json.token, { expires: 7 });
|
|
67
|
+
setSession({ token: json.token, user: json.user });
|
|
68
|
+
},
|
|
69
|
+
register: async (email, password, organizationId) => {
|
|
70
|
+
const body = { email, password, appId: config.appId, organizationId, environment };
|
|
71
|
+
const res = await (0, cross_fetch_1.default)(`${apiBase}/v1/auth/register`, {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: { "Content-Type": "application/json" },
|
|
74
|
+
body: JSON.stringify(body),
|
|
75
|
+
});
|
|
76
|
+
if (!res.ok) {
|
|
77
|
+
const errJson = await res.json().catch(() => ({ message: "Registration failed" }));
|
|
78
|
+
throw new Error(errJson.message || "Registration failed");
|
|
79
|
+
}
|
|
80
|
+
const json = (await res.json());
|
|
81
|
+
js_cookie_1.default.set(tokenKey, json.token, { expires: 7 });
|
|
82
|
+
setSession({ token: json.token, user: json.user });
|
|
83
|
+
},
|
|
84
|
+
refresh: async () => {
|
|
85
|
+
const token = js_cookie_1.default.get(tokenKey);
|
|
86
|
+
if (!token)
|
|
87
|
+
return;
|
|
88
|
+
const res = await (0, cross_fetch_1.default)(`${apiBase}/v1/auth/refresh`, {
|
|
89
|
+
method: "POST",
|
|
90
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
91
|
+
});
|
|
92
|
+
if (!res.ok)
|
|
93
|
+
return;
|
|
94
|
+
const json = await res.json();
|
|
95
|
+
if (json.token) {
|
|
96
|
+
js_cookie_1.default.set(tokenKey, json.token, { expires: 7 });
|
|
97
|
+
setSession({ token: json.token, user: json.user });
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
}), [apiBase, config, session, isLoading, tokenKey, environment]);
|
|
101
|
+
return (0, jsx_runtime_1.jsx)(exports.OneIDContext.Provider, { value: value, children: children });
|
|
102
|
+
};
|
|
103
|
+
exports.OneIDProvider = OneIDProvider;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export declare function useOneID(): {
|
|
2
|
+
config: {
|
|
3
|
+
appId: string;
|
|
4
|
+
apiBase?: string;
|
|
5
|
+
namespaceId?: string;
|
|
6
|
+
organizationId?: string;
|
|
7
|
+
environment?: string;
|
|
8
|
+
};
|
|
9
|
+
session: {
|
|
10
|
+
token: string | null;
|
|
11
|
+
user?: {
|
|
12
|
+
id: string;
|
|
13
|
+
email: string;
|
|
14
|
+
namespaceId?: string;
|
|
15
|
+
environmentId?: string;
|
|
16
|
+
roles?: string[];
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
user: {
|
|
20
|
+
id: string;
|
|
21
|
+
email: string;
|
|
22
|
+
namespaceId?: string;
|
|
23
|
+
environmentId?: string;
|
|
24
|
+
roles?: string[];
|
|
25
|
+
} | null;
|
|
26
|
+
token: string | null;
|
|
27
|
+
isLoading: boolean;
|
|
28
|
+
login: (email: string, password: string) => Promise<void>;
|
|
29
|
+
register: (email: string, password: string, organizationId?: string) => Promise<void>;
|
|
30
|
+
logout: () => void;
|
|
31
|
+
refresh: () => Promise<void>;
|
|
32
|
+
};
|
package/dist/useOneID.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOneID = useOneID;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const provider_1 = require("./provider");
|
|
6
|
+
function useOneID() {
|
|
7
|
+
const ctx = (0, react_1.useContext)(provider_1.OneIDContext);
|
|
8
|
+
if (!ctx) {
|
|
9
|
+
throw new Error("useOneID must be used within OneIDProvider");
|
|
10
|
+
}
|
|
11
|
+
return ctx;
|
|
12
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oneid-sdk/react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React SDK for oneID",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc -p tsconfig.json",
|
|
9
|
+
"dev": "tsc -w -p tsconfig.json",
|
|
10
|
+
"lint": "eslint src --ext .ts,.tsx"
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"react": ">=18"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"cross-fetch": "^3.1.8",
|
|
17
|
+
"js-cookie": "^3.0.5"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/react": "^18.2.45",
|
|
21
|
+
"@types/js-cookie": "^3.0.6",
|
|
22
|
+
"eslint": "^8.56.0",
|
|
23
|
+
"typescript": "^5.3.3"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
ADDED
package/src/provider.tsx
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import React, { createContext, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import fetch from "cross-fetch";
|
|
3
|
+
import Cookies from "js-cookie";
|
|
4
|
+
|
|
5
|
+
type OneIDConfig = {
|
|
6
|
+
appId: string;
|
|
7
|
+
apiBase?: string;
|
|
8
|
+
namespaceId?: string;
|
|
9
|
+
organizationId?: string;
|
|
10
|
+
environment?: string; // e.g., dev/prod
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type UserData = {
|
|
14
|
+
id: string;
|
|
15
|
+
email: string;
|
|
16
|
+
namespaceId?: string;
|
|
17
|
+
environmentId?: string;
|
|
18
|
+
roles?: string[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type Session = {
|
|
22
|
+
token: string | null;
|
|
23
|
+
user?: UserData;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type OneIDContextValue = {
|
|
27
|
+
config: OneIDConfig;
|
|
28
|
+
session: Session;
|
|
29
|
+
user: UserData | null;
|
|
30
|
+
token: string | null;
|
|
31
|
+
isLoading: boolean;
|
|
32
|
+
login: (email: string, password: string) => Promise<void>;
|
|
33
|
+
register: (email: string, password: string, organizationId?: string) => Promise<void>;
|
|
34
|
+
logout: () => void;
|
|
35
|
+
refresh: () => Promise<void>;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const OneIDContext = createContext<OneIDContextValue | undefined>(undefined);
|
|
39
|
+
|
|
40
|
+
export const OneIDProvider = ({
|
|
41
|
+
children,
|
|
42
|
+
config,
|
|
43
|
+
}: {
|
|
44
|
+
children: React.ReactNode;
|
|
45
|
+
config: OneIDConfig;
|
|
46
|
+
}) => {
|
|
47
|
+
const apiBase = config.apiBase || "http://localhost:4100";
|
|
48
|
+
const environment = config.environment || "dev";
|
|
49
|
+
const tokenKey = `oneid_token_${environment}`;
|
|
50
|
+
const [session, setSession] = useState<Session>({ token: null });
|
|
51
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
const existing = Cookies.get(tokenKey);
|
|
55
|
+
if (existing) {
|
|
56
|
+
setSession({ token: existing });
|
|
57
|
+
// Verify token/get user info
|
|
58
|
+
fetch(`${apiBase}/v1/auth/me`, {
|
|
59
|
+
headers: { Authorization: `Bearer ${existing}` },
|
|
60
|
+
})
|
|
61
|
+
.then((res) => {
|
|
62
|
+
if (res.ok) return res.json();
|
|
63
|
+
throw new Error("Session invalid");
|
|
64
|
+
})
|
|
65
|
+
.then((json) => {
|
|
66
|
+
setSession({ token: existing, user: json.user });
|
|
67
|
+
})
|
|
68
|
+
.catch(() => {
|
|
69
|
+
Cookies.remove(tokenKey);
|
|
70
|
+
setSession({ token: null });
|
|
71
|
+
})
|
|
72
|
+
.finally(() => setIsLoading(false));
|
|
73
|
+
} else {
|
|
74
|
+
setIsLoading(false);
|
|
75
|
+
}
|
|
76
|
+
}, [apiBase, tokenKey]);
|
|
77
|
+
|
|
78
|
+
const value = useMemo<OneIDContextValue>(
|
|
79
|
+
() => ({
|
|
80
|
+
config,
|
|
81
|
+
session,
|
|
82
|
+
user: session.user || null,
|
|
83
|
+
token: session.token,
|
|
84
|
+
isLoading,
|
|
85
|
+
logout: () => {
|
|
86
|
+
Cookies.remove(tokenKey);
|
|
87
|
+
setSession({ token: null });
|
|
88
|
+
},
|
|
89
|
+
login: async (email: string, password: string) => {
|
|
90
|
+
const body = { email, password, appId: config.appId, environment };
|
|
91
|
+
const res = await fetch(`${apiBase}/v1/auth/login`, {
|
|
92
|
+
method: "POST",
|
|
93
|
+
headers: { "Content-Type": "application/json" },
|
|
94
|
+
body: JSON.stringify(body),
|
|
95
|
+
});
|
|
96
|
+
if (!res.ok) {
|
|
97
|
+
const errJson = await res.json().catch(() => ({ message: "Login failed" }));
|
|
98
|
+
throw new Error(errJson.message || "Login failed");
|
|
99
|
+
}
|
|
100
|
+
const json = (await res.json()) as { token: string, user: UserData };
|
|
101
|
+
Cookies.set(tokenKey, json.token, { expires: 7 });
|
|
102
|
+
setSession({ token: json.token, user: json.user });
|
|
103
|
+
},
|
|
104
|
+
register: async (email: string, password: string, organizationId?: string) => {
|
|
105
|
+
const body = { email, password, appId: config.appId, organizationId, environment };
|
|
106
|
+
const res = await fetch(`${apiBase}/v1/auth/register`, {
|
|
107
|
+
method: "POST",
|
|
108
|
+
headers: { "Content-Type": "application/json" },
|
|
109
|
+
body: JSON.stringify(body),
|
|
110
|
+
});
|
|
111
|
+
if (!res.ok) {
|
|
112
|
+
const errJson = await res.json().catch(() => ({ message: "Registration failed" }));
|
|
113
|
+
throw new Error(errJson.message || "Registration failed");
|
|
114
|
+
}
|
|
115
|
+
const json = (await res.json()) as { token: string, user: UserData };
|
|
116
|
+
Cookies.set(tokenKey, json.token, { expires: 7 });
|
|
117
|
+
setSession({ token: json.token, user: json.user });
|
|
118
|
+
},
|
|
119
|
+
refresh: async () => {
|
|
120
|
+
const token = Cookies.get(tokenKey);
|
|
121
|
+
if (!token) return;
|
|
122
|
+
|
|
123
|
+
const res = await fetch(`${apiBase}/v1/auth/refresh`, {
|
|
124
|
+
method: "POST",
|
|
125
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
126
|
+
});
|
|
127
|
+
if (!res.ok) return;
|
|
128
|
+
|
|
129
|
+
const json = await res.json();
|
|
130
|
+
if (json.token) {
|
|
131
|
+
Cookies.set(tokenKey, json.token, { expires: 7 });
|
|
132
|
+
setSession({ token: json.token, user: json.user });
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
}),
|
|
136
|
+
[apiBase, config, session, isLoading, tokenKey, environment]
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
return <OneIDContext.Provider value={value}>{children}</OneIDContext.Provider>;
|
|
140
|
+
};
|
package/src/useOneID.ts
ADDED
package/tsconfig.json
ADDED