@niledatabase/server 5.0.0-alpha.2 → 5.0.0-alpha.3

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.mjs CHANGED
@@ -904,9 +904,11 @@ async function route13(req, config) {
904
904
  function matches13(configRoutes, request2) {
905
905
  return urlMatches(request2.url, configRoutes.PASSWORD_RESET);
906
906
  }
907
- async function fetchResetPassword(config, method, body, params) {
907
+ async function fetchResetPassword(config, method, body, params, useJson = true) {
908
908
  const authParams = new URLSearchParams(params ?? {});
909
- authParams?.set("json", "true");
909
+ if (useJson) {
910
+ authParams?.set("json", "true");
911
+ }
910
912
  const clientUrl = `${config.serverOrigin}${config.routePrefix}${"/auth/reset-password" /* PASSWORD_RESET */}?${authParams?.toString()}`;
911
913
  const init = {
912
914
  method,
@@ -1936,11 +1938,42 @@ var Auth = class {
1936
1938
  return res;
1937
1939
  }
1938
1940
  }
1941
+ async forgotPassword(req) {
1942
+ let email = "";
1943
+ const defaults = defaultCallbackUrl({
1944
+ config: this.#config
1945
+ });
1946
+ let callbackUrl = defaults.callbackUrl;
1947
+ let redirectUrl = defaults.redirectUrl;
1948
+ if ("email" in req) {
1949
+ email = req.email;
1950
+ }
1951
+ if ("callbackUrl" in req) {
1952
+ callbackUrl = req.callbackUrl ? req.callbackUrl : null;
1953
+ }
1954
+ if ("redirectUrl" in req) {
1955
+ redirectUrl = req.redirectUrl ? req.redirectUrl : null;
1956
+ }
1957
+ const body = JSON.stringify({
1958
+ email,
1959
+ redirectUrl,
1960
+ callbackUrl
1961
+ });
1962
+ const data = await fetchResetPassword(
1963
+ this.#config,
1964
+ "POST",
1965
+ body,
1966
+ new URLSearchParams(),
1967
+ false
1968
+ );
1969
+ return data;
1970
+ }
1939
1971
  async resetPassword(req) {
1940
1972
  let email = "";
1941
1973
  let password = "";
1942
- let callbackUrl = null;
1943
- let redirectUrl = null;
1974
+ const defaults = defaultCallbackUrl({ config: this.#config });
1975
+ let callbackUrl = defaults.callbackUrl;
1976
+ let redirectUrl = defaults.redirectUrl;
1944
1977
  if (req instanceof Request) {
1945
1978
  const body2 = await req.json();
1946
1979
  email = body2.email;
@@ -1969,19 +2002,6 @@ var Auth = class {
1969
2002
  redirectUrl = req.redirectUrl ? req.redirectUrl : null;
1970
2003
  }
1971
2004
  }
1972
- const fallbackCb = parseCallback(this.#config.headers);
1973
- if (fallbackCb) {
1974
- const [, value] = fallbackCb.split("=");
1975
- if (value) {
1976
- const parsedUrl = decodeURIComponent(value);
1977
- if (!redirectUrl) {
1978
- redirectUrl = `${new URL(parsedUrl).origin}${"/auth/reset-password" /* PASSWORD_RESET */}`;
1979
- }
1980
- }
1981
- if (!callbackUrl) {
1982
- callbackUrl = value;
1983
- }
1984
- }
1985
2005
  await this.getCsrf();
1986
2006
  const body = JSON.stringify({
1987
2007
  email,
@@ -2200,6 +2220,19 @@ function parseResetToken(headers) {
2200
2220
  const [, token] = /((__Secure-)?nile\.reset=[^;]+)/.exec(authCookie) ?? [];
2201
2221
  return token;
2202
2222
  }
2223
+ function defaultCallbackUrl({ config }) {
2224
+ let cb = null;
2225
+ let redirect = null;
2226
+ const fallbackCb = parseCallback(config.headers);
2227
+ if (fallbackCb) {
2228
+ const [, value] = fallbackCb.split("=");
2229
+ cb = decodeURIComponent(value);
2230
+ if (value) {
2231
+ redirect = `${new URL(cb).origin}${"/auth/reset-password" /* PASSWORD_RESET */}`;
2232
+ }
2233
+ }
2234
+ return { callbackUrl: cb, redirectUrl: redirect };
2235
+ }
2203
2236
 
2204
2237
  // src/auth/getCsrf.ts
2205
2238
  async function getCsrf(config, rawResponse = false) {