@niledatabase/server 4.0.0-alpha.11 → 4.0.0-alpha.12

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.mts CHANGED
@@ -145,6 +145,13 @@ type Tenant = {
145
145
  name: string;
146
146
  };
147
147
 
148
+ type Provider = {
149
+ id: string;
150
+ name: string;
151
+ type: string;
152
+ signinUrl: string;
153
+ callbackUr: string;
154
+ };
148
155
  type JWT = {
149
156
  email: string;
150
157
  sub: string;
@@ -212,6 +219,16 @@ declare class Auth extends Config {
212
219
  handleHeaders(init?: RequestInit): RequestInit | undefined;
213
220
  get sessionUrl(): string;
214
221
  getSession: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<JWT | ActiveSession | Response | undefined>;
222
+ get getCsrfUrl(): string;
223
+ getCsrf(req: NileRequest<void> | Headers, init?: RequestInit): Promise<JSON | Response>;
224
+ get listProvidersUrl(): string;
225
+ listProviders: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<Response | {
226
+ [key: string]: Provider;
227
+ }>;
228
+ get signOutUrl(): string;
229
+ signOut: (req: NileRequest<void | {
230
+ callbackUrl?: string;
231
+ }> | Headers, init?: RequestInit) => Promise<Response | JSON>;
215
232
  }
216
233
 
217
234
  declare class Tenants extends Config {
package/dist/index.d.ts CHANGED
@@ -145,6 +145,13 @@ type Tenant = {
145
145
  name: string;
146
146
  };
147
147
 
148
+ type Provider = {
149
+ id: string;
150
+ name: string;
151
+ type: string;
152
+ signinUrl: string;
153
+ callbackUr: string;
154
+ };
148
155
  type JWT = {
149
156
  email: string;
150
157
  sub: string;
@@ -212,6 +219,16 @@ declare class Auth extends Config {
212
219
  handleHeaders(init?: RequestInit): RequestInit | undefined;
213
220
  get sessionUrl(): string;
214
221
  getSession: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<JWT | ActiveSession | Response | undefined>;
222
+ get getCsrfUrl(): string;
223
+ getCsrf(req: NileRequest<void> | Headers, init?: RequestInit): Promise<JSON | Response>;
224
+ get listProvidersUrl(): string;
225
+ listProviders: (req: NileRequest<void> | Headers, init?: RequestInit) => Promise<Response | {
226
+ [key: string]: Provider;
227
+ }>;
228
+ get signOutUrl(): string;
229
+ signOut: (req: NileRequest<void | {
230
+ callbackUrl?: string;
231
+ }> | Headers, init?: RequestInit) => Promise<Response | JSON>;
215
232
  }
216
233
 
217
234
  declare class Tenants extends Config {
package/dist/index.js CHANGED
@@ -1077,7 +1077,8 @@ async function _fetch(config, path, opts) {
1077
1077
  const errorHandler = typeof response?.clone === "function" ? response.clone() : null;
1078
1078
  let msg = "";
1079
1079
  try {
1080
- res = await response?.json();
1080
+ res = await response?.json().catch(() => {
1081
+ });
1081
1082
  } catch (e) {
1082
1083
  if (errorHandler) {
1083
1084
  msg = await errorHandler.text();
@@ -1110,7 +1111,8 @@ async function _fetch(config, path, opts) {
1110
1111
  error(
1111
1112
  `[fetch][response][status: ${errorHandler?.status}] UNHANDLED ERROR`,
1112
1113
  {
1113
- res
1114
+ response,
1115
+ message: await response.text()
1114
1116
  }
1115
1117
  );
1116
1118
  return new ResponseError(null, {
@@ -1773,7 +1775,7 @@ var appRoutes = (prefix = "/api") => ({
1773
1775
  TENANT_USER: `${prefix}/tenants/{tenantId}/users/{userId}`,
1774
1776
  TENANT_USERS: `${prefix}/tenants/{tenantId}/users`,
1775
1777
  SIGNUP: `${prefix}/signup`,
1776
- LOG: `${prefix}/auth/_log`
1778
+ LOG: `${prefix}/_log`
1777
1779
  });
1778
1780
 
1779
1781
  // src/utils/Requester/index.ts
@@ -1783,9 +1785,9 @@ var Requester = class extends Config {
1783
1785
  }
1784
1786
  async rawRequest(method, url, init, body) {
1785
1787
  const _init = {
1786
- ...init,
1787
1788
  body,
1788
- method
1789
+ method,
1790
+ ...init
1789
1791
  };
1790
1792
  const res = await _fetch(this, url, _init);
1791
1793
  if (res instanceof ResponseError) {
@@ -1986,6 +1988,8 @@ var Auth = class extends Config {
1986
1988
  }
1987
1989
  handleHeaders(init) {
1988
1990
  if (this.headers) {
1991
+ const cburl = getCallbackUrl2(this.headers);
1992
+ this.headers.set(X_NILE_ORIGIN, new URL(cburl).origin);
1989
1993
  if (init) {
1990
1994
  init.headers = new Headers({ ...this.headers, ...init?.headers });
1991
1995
  return init;
@@ -2010,7 +2014,57 @@ var Auth = class extends Config {
2010
2014
  }
2011
2015
  return session;
2012
2016
  };
2017
+ get getCsrfUrl() {
2018
+ return "/auth/csrf";
2019
+ }
2020
+ async getCsrf(req, init) {
2021
+ const _requester = new Requester(this);
2022
+ const _init = this.handleHeaders(init);
2023
+ return await _requester.get(req, this.getCsrfUrl, _init);
2024
+ }
2025
+ get listProvidersUrl() {
2026
+ return "/auth/providers";
2027
+ }
2028
+ listProviders = async (req, init) => {
2029
+ const _requester = new Requester(this);
2030
+ const _init = this.handleHeaders(init);
2031
+ return await _requester.get(req, this.listProvidersUrl, _init);
2032
+ };
2033
+ get signOutUrl() {
2034
+ return "/auth/signout";
2035
+ }
2036
+ signOut = async (req, init) => {
2037
+ const _requester = new Requester(this);
2038
+ const _init = this.handleHeaders(init);
2039
+ const csrf = await this.getCsrf(req);
2040
+ const callbackUrl = req && "callbackUrl" in req ? String(req.callbackUrl) : "/";
2041
+ if (csrf instanceof Request) {
2042
+ return csrf;
2043
+ }
2044
+ const csrfToken = "csrfToken" in csrf ? String(csrf.csrfToken) : "";
2045
+ if (!csrfToken) {
2046
+ return new Response("Request blocked", { status: 400 });
2047
+ }
2048
+ return await _requester.post(req, this.signOutUrl, {
2049
+ method: "post",
2050
+ body: JSON.stringify({
2051
+ csrfToken,
2052
+ callbackUrl,
2053
+ json: String(true)
2054
+ }),
2055
+ ..._init
2056
+ });
2057
+ };
2013
2058
  };
2059
+ function getCallbackUrl2(headers) {
2060
+ if (headers) {
2061
+ const cookieHeader = headers.get("cookie") || "";
2062
+ const cookies = Object.fromEntries(
2063
+ cookieHeader.split("; ").map((cookie) => cookie.split("=").map(decodeURIComponent))
2064
+ );
2065
+ return cookies["__Secure-nile.callback-url"] || cookies["nile.callback-url"];
2066
+ }
2067
+ }
2014
2068
 
2015
2069
  // src/tenants/index.ts
2016
2070
  var Tenants = class extends Config {