@lalternative/auth 0.20.0 → 1.0.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/README.md +65 -0
- package/dist/chunk-MJI5PR73.js +71 -0
- package/dist/chunk-MJI5PR73.js.map +1 -0
- package/dist/server.js +4 -66
- package/dist/server.js.map +1 -1
- package/dist/urbangate-client.d.ts +39 -0
- package/dist/urbangate-client.js +70 -0
- package/dist/urbangate-client.js.map +1 -0
- package/dist/urbangate.d.ts +57 -0
- package/dist/urbangate.js +625 -0
- package/dist/urbangate.js.map +1 -0
- package/package.json +9 -1
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// src/urbangate/client.ts
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
function createUrbangateAuthClient(config = {}) {
|
|
4
|
+
const base = (config.baseURL ?? (typeof window !== "undefined" ? window.location.origin : "")).replace(/\/$/, "");
|
|
5
|
+
async function call(method, path, body) {
|
|
6
|
+
try {
|
|
7
|
+
const res = await fetch(`${base}/api/auth/${path}`, {
|
|
8
|
+
method,
|
|
9
|
+
credentials: "include",
|
|
10
|
+
headers: body === void 0 ? {} : { "content-type": "application/json" },
|
|
11
|
+
body: body === void 0 ? void 0 : JSON.stringify(body)
|
|
12
|
+
});
|
|
13
|
+
const parsed = await res.json().catch(() => null);
|
|
14
|
+
if (!res.ok) {
|
|
15
|
+
const error = parsed?.error;
|
|
16
|
+
return {
|
|
17
|
+
data: null,
|
|
18
|
+
error: error ?? { status: res.status, message: res.statusText }
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return { data: parsed, error: null };
|
|
22
|
+
} catch (err) {
|
|
23
|
+
return {
|
|
24
|
+
data: null,
|
|
25
|
+
error: {
|
|
26
|
+
message: err instanceof Error ? err.message : "network",
|
|
27
|
+
status: 0
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const client = {
|
|
33
|
+
signIn: {
|
|
34
|
+
email: (input) => call("POST", "sign-in/email", input),
|
|
35
|
+
social: async () => ({ error: { code: "not_supported", status: 501 } })
|
|
36
|
+
},
|
|
37
|
+
signUp: {
|
|
38
|
+
email: (input) => call("POST", "sign-up/email", input)
|
|
39
|
+
},
|
|
40
|
+
emailOtp: {
|
|
41
|
+
sendVerificationOtp: (input) => call("POST", "email-otp/send-verification-otp", input),
|
|
42
|
+
verifyEmail: (input) => call("POST", "email-otp/verify-email", input),
|
|
43
|
+
resetPassword: (input) => call("POST", "email-otp/reset-password", input)
|
|
44
|
+
},
|
|
45
|
+
signOut: () => call("POST", "sign-out"),
|
|
46
|
+
getSession: () => call("GET", "get-session"),
|
|
47
|
+
useSession() {
|
|
48
|
+
const [state, setState] = useState({
|
|
49
|
+
data: null,
|
|
50
|
+
isPending: true,
|
|
51
|
+
error: null
|
|
52
|
+
});
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
let live = true;
|
|
55
|
+
void client.getSession().then(({ data, error }) => {
|
|
56
|
+
if (live) setState({ data, isPending: false, error: error ?? null });
|
|
57
|
+
});
|
|
58
|
+
return () => {
|
|
59
|
+
live = false;
|
|
60
|
+
};
|
|
61
|
+
}, []);
|
|
62
|
+
return state;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
return client;
|
|
66
|
+
}
|
|
67
|
+
export {
|
|
68
|
+
createUrbangateAuthClient
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=urbangate-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/urbangate/client.ts"],"sourcesContent":["import { useEffect, useState } from \"react\";\nimport type { AuthClientResult, AuthClientSurface } from \"../types\";\n\nexport interface UrbangateAuthClientConfig {\n baseURL?: string;\n}\n\nexport interface UrbangateClientUser {\n id: string;\n identityId: string;\n email: string;\n emailVerified: boolean;\n name: string;\n role: \"admin\" | \"user\";\n}\n\nexport interface UrbangateClientSession {\n user: UrbangateClientUser;\n session: { expiresAt: string };\n}\n\nexport interface SessionState {\n data: UrbangateClientSession | null;\n isPending: boolean;\n error: { message?: string; status?: number } | null;\n}\n\nexport type UrbangateAuthClient = Omit<AuthClientSurface, \"admin\"> & {\n signOut(): Promise<AuthClientResult>;\n getSession(): Promise<{\n data: UrbangateClientSession | null;\n error: AuthClientResult[\"error\"];\n }>;\n useSession(): SessionState;\n};\n\ntype Result<T = unknown> = { data: T | null; error: AuthClientResult[\"error\"] };\n\nexport function createUrbangateAuthClient(\n config: UrbangateAuthClientConfig = {},\n): UrbangateAuthClient {\n const base = (\n config.baseURL ??\n (typeof window !== \"undefined\" ? window.location.origin : \"\")\n ).replace(/\\/$/, \"\");\n\n async function call<T = unknown>(\n method: \"GET\" | \"POST\",\n path: string,\n body?: unknown,\n ): Promise<Result<T>> {\n try {\n const res = await fetch(`${base}/api/auth/${path}`, {\n method,\n credentials: \"include\",\n headers:\n body === undefined ? {} : { \"content-type\": \"application/json\" },\n body: body === undefined ? undefined : JSON.stringify(body),\n });\n const parsed = (await res.json().catch(() => null)) as\n { error?: AuthClientResult[\"error\"] } | T | null;\n if (!res.ok) {\n const error = (parsed as { error?: AuthClientResult[\"error\"] } | null)\n ?.error;\n return {\n data: null,\n error: error ?? { status: res.status, message: res.statusText },\n };\n }\n return { data: parsed as T, error: null };\n } catch (err) {\n return {\n data: null,\n error: {\n message: err instanceof Error ? err.message : \"network\",\n status: 0,\n },\n };\n }\n }\n\n const client: UrbangateAuthClient = {\n signIn: {\n email: (input) => call(\"POST\", \"sign-in/email\", input),\n social: async () => ({ error: { code: \"not_supported\", status: 501 } }),\n },\n signUp: {\n email: (input) => call(\"POST\", \"sign-up/email\", input),\n },\n emailOtp: {\n sendVerificationOtp: (input) =>\n call(\"POST\", \"email-otp/send-verification-otp\", input),\n verifyEmail: (input) => call(\"POST\", \"email-otp/verify-email\", input),\n resetPassword: (input) => call(\"POST\", \"email-otp/reset-password\", input),\n },\n signOut: () => call(\"POST\", \"sign-out\"),\n getSession: () => call<UrbangateClientSession | null>(\"GET\", \"get-session\"),\n useSession() {\n const [state, setState] = useState<SessionState>({\n data: null,\n isPending: true,\n error: null,\n });\n useEffect(() => {\n let live = true;\n void client.getSession().then(({ data, error }) => {\n if (live) setState({ data, isPending: false, error: error ?? null });\n });\n return () => {\n live = false;\n };\n }, []);\n return state;\n },\n };\n return client;\n}\n"],"mappings":";AAAA,SAAS,WAAW,gBAAgB;AAsC7B,SAAS,0BACd,SAAoC,CAAC,GAChB;AACrB,QAAM,QACJ,OAAO,YACN,OAAO,WAAW,cAAc,OAAO,SAAS,SAAS,KAC1D,QAAQ,OAAO,EAAE;AAEnB,iBAAe,KACb,QACA,MACA,MACoB;AACpB,QAAI;AACF,YAAM,MAAM,MAAM,MAAM,GAAG,IAAI,aAAa,IAAI,IAAI;AAAA,QAClD;AAAA,QACA,aAAa;AAAA,QACb,SACE,SAAS,SAAY,CAAC,IAAI,EAAE,gBAAgB,mBAAmB;AAAA,QACjE,MAAM,SAAS,SAAY,SAAY,KAAK,UAAU,IAAI;AAAA,MAC5D,CAAC;AACD,YAAM,SAAU,MAAM,IAAI,KAAK,EAAE,MAAM,MAAM,IAAI;AAEjD,UAAI,CAAC,IAAI,IAAI;AACX,cAAM,QAAS,QACX;AACJ,eAAO;AAAA,UACL,MAAM;AAAA,UACN,OAAO,SAAS,EAAE,QAAQ,IAAI,QAAQ,SAAS,IAAI,WAAW;AAAA,QAChE;AAAA,MACF;AACA,aAAO,EAAE,MAAM,QAAa,OAAO,KAAK;AAAA,IAC1C,SAAS,KAAK;AACZ,aAAO;AAAA,QACL,MAAM;AAAA,QACN,OAAO;AAAA,UACL,SAAS,eAAe,QAAQ,IAAI,UAAU;AAAA,UAC9C,QAAQ;AAAA,QACV;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,QAAM,SAA8B;AAAA,IAClC,QAAQ;AAAA,MACN,OAAO,CAAC,UAAU,KAAK,QAAQ,iBAAiB,KAAK;AAAA,MACrD,QAAQ,aAAa,EAAE,OAAO,EAAE,MAAM,iBAAiB,QAAQ,IAAI,EAAE;AAAA,IACvE;AAAA,IACA,QAAQ;AAAA,MACN,OAAO,CAAC,UAAU,KAAK,QAAQ,iBAAiB,KAAK;AAAA,IACvD;AAAA,IACA,UAAU;AAAA,MACR,qBAAqB,CAAC,UACpB,KAAK,QAAQ,mCAAmC,KAAK;AAAA,MACvD,aAAa,CAAC,UAAU,KAAK,QAAQ,0BAA0B,KAAK;AAAA,MACpE,eAAe,CAAC,UAAU,KAAK,QAAQ,4BAA4B,KAAK;AAAA,IAC1E;AAAA,IACA,SAAS,MAAM,KAAK,QAAQ,UAAU;AAAA,IACtC,YAAY,MAAM,KAAoC,OAAO,aAAa;AAAA,IAC1E,aAAa;AACX,YAAM,CAAC,OAAO,QAAQ,IAAI,SAAuB;AAAA,QAC/C,MAAM;AAAA,QACN,WAAW;AAAA,QACX,OAAO;AAAA,MACT,CAAC;AACD,gBAAU,MAAM;AACd,YAAI,OAAO;AACX,aAAK,OAAO,WAAW,EAAE,KAAK,CAAC,EAAE,MAAM,MAAM,MAAM;AACjD,cAAI,KAAM,UAAS,EAAE,MAAM,WAAW,OAAO,OAAO,SAAS,KAAK,CAAC;AAAA,QACrE,CAAC;AACD,eAAO,MAAM;AACX,iBAAO;AAAA,QACT;AAAA,MACF,GAAG,CAAC,CAAC;AACL,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
type Fetch = typeof fetch;
|
|
2
|
+
|
|
3
|
+
interface ExchangeConfig {
|
|
4
|
+
product: string;
|
|
5
|
+
issuerUrl: string;
|
|
6
|
+
provisioner: {
|
|
7
|
+
clientId: string;
|
|
8
|
+
clientSecret: string;
|
|
9
|
+
};
|
|
10
|
+
admin: {
|
|
11
|
+
clientId: string;
|
|
12
|
+
clientSecret: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface UrbangateAuthConfig {
|
|
17
|
+
product: string;
|
|
18
|
+
/** Kratos' public API as the server reaches it, e.g. http://kratos:4433 or https://id.urbangate.dev. */
|
|
19
|
+
kratosUrl: string;
|
|
20
|
+
urbangate: Omit<ExchangeConfig, "product">;
|
|
21
|
+
cookie?: {
|
|
22
|
+
name?: string;
|
|
23
|
+
secure?: boolean;
|
|
24
|
+
};
|
|
25
|
+
fetch?: Fetch;
|
|
26
|
+
}
|
|
27
|
+
interface UrbangateUser {
|
|
28
|
+
id: string;
|
|
29
|
+
identityId: string;
|
|
30
|
+
email: string;
|
|
31
|
+
emailVerified: boolean;
|
|
32
|
+
name: string;
|
|
33
|
+
role: "admin" | "user";
|
|
34
|
+
}
|
|
35
|
+
interface UrbangateSession {
|
|
36
|
+
user: UrbangateUser;
|
|
37
|
+
session: {
|
|
38
|
+
expiresAt: string;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
interface AccessToken {
|
|
42
|
+
token: string;
|
|
43
|
+
setCookie?: string;
|
|
44
|
+
}
|
|
45
|
+
interface UrbangateCoreProxyOptions {
|
|
46
|
+
coreUrl: string;
|
|
47
|
+
adminOnly?: boolean;
|
|
48
|
+
}
|
|
49
|
+
interface UrbangateAuth {
|
|
50
|
+
handler(request: Request): Promise<Response>;
|
|
51
|
+
getSession(headers: Headers): Promise<UrbangateSession | null>;
|
|
52
|
+
accessToken(headers: Headers): Promise<AccessToken | null>;
|
|
53
|
+
coreProxy(options: UrbangateCoreProxyOptions): (request: Request) => Promise<Response>;
|
|
54
|
+
}
|
|
55
|
+
declare function createUrbangateAuth(config: UrbangateAuthConfig): UrbangateAuth;
|
|
56
|
+
|
|
57
|
+
export { type AccessToken, type UrbangateAuth, type UrbangateAuthConfig, type UrbangateCoreProxyOptions, type UrbangateSession, type UrbangateUser, createUrbangateAuth };
|