@baseworks/auth 0.2.5 → 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.
@@ -1,66 +0,0 @@
1
- export function normalizeUrlLike(value: string) {
2
- if (value === "/") return "";
3
- return value.replace(/\/+$/, "");
4
- }
5
-
6
- function joinPublicUrl(base: string, path = "") {
7
- const normalizedBase = normalizeUrlLike(base);
8
- const normalizedPath = path.startsWith("/") ? path : `/${path}`;
9
- if (!normalizedBase) return normalizedPath;
10
- if (/^https?:\/\//.test(normalizedBase)) {
11
- return new URL(normalizedPath, `${normalizedBase}/`).toString();
12
- }
13
- return `${normalizedBase}${normalizedPath}`;
14
- }
15
-
16
- export function getAuthPublicUrl() {
17
- return normalizeUrlLike(process.env.NEXT_PUBLIC_AUTH_URL ?? "/auth");
18
- }
19
-
20
- export function getAccountPublicUrl() {
21
- return normalizeUrlLike(process.env.NEXT_PUBLIC_ACCOUNT_URL ?? "/account");
22
- }
23
-
24
- export function getAppBasePath(defaultPath = "") {
25
- return normalizeUrlLike(
26
- process.env.NEXT_PUBLIC_APP_BASE_PATH ?? process.env.APP_BASE_PATH ?? defaultPath,
27
- );
28
- }
29
-
30
- export function getPublicSiteUrl() {
31
- return normalizeUrlLike(process.env.APP_PUBLIC_URL ?? process.env.NEXT_PUBLIC_SITE_URL ?? "");
32
- }
33
-
34
- export function buildPublicUrl(pathOrUrl: string, fallbackOrigin?: string) {
35
- if (/^https?:\/\//.test(pathOrUrl)) return pathOrUrl;
36
- const origin = getPublicSiteUrl() || fallbackOrigin;
37
- if (!origin) return pathOrUrl;
38
- return new URL(pathOrUrl, `${normalizeUrlLike(origin)}/`).toString();
39
- }
40
-
41
- function buildAuthUrl(path: string, redirectTo: string, origin?: string) {
42
- const target = joinPublicUrl(getAuthPublicUrl(), path);
43
- const targetIsAbsolute = /^https?:\/\//.test(target);
44
- const fallbackOrigin = "http://localhost";
45
- const url = new URL(target, origin ?? fallbackOrigin);
46
- url.searchParams.set("redirectTo", redirectTo);
47
- const href = url.toString();
48
- if (origin || targetIsAbsolute) return href;
49
- return href.replace(fallbackOrigin, "");
50
- }
51
-
52
- export function buildAuthLoginUrl(redirectTo: string, origin?: string) {
53
- return buildAuthUrl("/login", redirectTo, origin);
54
- }
55
-
56
- export function buildAuthLogoutUrl(redirectTo: string, origin?: string) {
57
- return buildAuthUrl(
58
- "/logout",
59
- process.env.NEXT_PUBLIC_SIGN_OUT_REDIRECT_URL ?? redirectTo,
60
- origin,
61
- );
62
- }
63
-
64
- export function buildAccountProfileUrl() {
65
- return joinPublicUrl(getAccountPublicUrl(), "/profile");
66
- }