@frequencyads/auth 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.
Files changed (49) hide show
  1. package/dist/chunk-JIENWB3K.mjs +89 -0
  2. package/dist/chunk-NS2JRZGO.mjs +38 -0
  3. package/dist/chunk-OKP757OV.mjs +131 -0
  4. package/dist/chunk-OUUVIDG6.mjs +63 -0
  5. package/dist/chunk-P4OKC4U7.mjs +41 -0
  6. package/dist/chunk-VBLFQXCD.mjs +406 -0
  7. package/dist/chunk-VQCPN62H.mjs +134 -0
  8. package/dist/chunk-XJFMS3SM.mjs +21 -0
  9. package/dist/components/auth-provider.d.mts +24 -0
  10. package/dist/components/auth-provider.d.ts +24 -0
  11. package/dist/components/auth-provider.js +107 -0
  12. package/dist/components/auth-provider.mjs +11 -0
  13. package/dist/components/login.d.mts +24 -0
  14. package/dist/components/login.d.ts +24 -0
  15. package/dist/components/login.js +430 -0
  16. package/dist/components/login.mjs +9 -0
  17. package/dist/components/reset-password.d.mts +11 -0
  18. package/dist/components/reset-password.d.ts +11 -0
  19. package/dist/components/reset-password.js +283 -0
  20. package/dist/components/reset-password.mjs +10 -0
  21. package/dist/components/sign-out-button.d.mts +11 -0
  22. package/dist/components/sign-out-button.d.ts +11 -0
  23. package/dist/components/sign-out-button.js +90 -0
  24. package/dist/components/sign-out-button.mjs +11 -0
  25. package/dist/components/user-menu.d.mts +5 -0
  26. package/dist/components/user-menu.d.ts +5 -0
  27. package/dist/components/user-menu.js +107 -0
  28. package/dist/components/user-menu.mjs +11 -0
  29. package/dist/components/verify-code.d.mts +20 -0
  30. package/dist/components/verify-code.d.ts +20 -0
  31. package/dist/components/verify-code.js +155 -0
  32. package/dist/components/verify-code.mjs +9 -0
  33. package/dist/index.d.mts +44 -0
  34. package/dist/index.d.ts +44 -0
  35. package/dist/index.js +1006 -0
  36. package/dist/index.mjs +159 -0
  37. package/dist/middleware.d.mts +29 -0
  38. package/dist/middleware.d.ts +29 -0
  39. package/dist/middleware.js +114 -0
  40. package/dist/middleware.mjs +83 -0
  41. package/dist/proxy.d.mts +9 -0
  42. package/dist/proxy.d.ts +9 -0
  43. package/dist/proxy.js +71 -0
  44. package/dist/proxy.mjs +41 -0
  45. package/dist/request.d.mts +14 -0
  46. package/dist/request.d.ts +14 -0
  47. package/dist/request.js +46 -0
  48. package/dist/request.mjs +8 -0
  49. package/package.json +101 -0
@@ -0,0 +1,89 @@
1
+ 'use client';
2
+
3
+ // src/components/auth-provider.tsx
4
+ import {
5
+ createContext,
6
+ useCallback,
7
+ useContext,
8
+ useEffect,
9
+ useRef,
10
+ useState
11
+ } from "react";
12
+ import { createBrowserClient } from "@frequencyads/db/client";
13
+ import { jsx } from "react/jsx-runtime";
14
+ var Context = createContext(void 0);
15
+ function AuthProvider({ children, loginUrl }) {
16
+ var _a, _b, _c, _d;
17
+ const [supabase] = useState(() => createBrowserClient());
18
+ const [user, setUser] = useState(null);
19
+ const [loading, setLoading] = useState(true);
20
+ const userIdRef = useRef(null);
21
+ const setUserStable = useCallback((newUser) => {
22
+ var _a2;
23
+ const newId = (_a2 = newUser == null ? void 0 : newUser.id) != null ? _a2 : null;
24
+ if (newId !== userIdRef.current) {
25
+ userIdRef.current = newId;
26
+ setUser(newUser);
27
+ }
28
+ }, []);
29
+ useEffect(() => {
30
+ if (!supabase) {
31
+ setLoading(false);
32
+ return;
33
+ }
34
+ const {
35
+ data: { subscription }
36
+ } = supabase.auth.onAuthStateChange((_event, session) => {
37
+ var _a2;
38
+ setUserStable((_a2 = session == null ? void 0 : session.user) != null ? _a2 : null);
39
+ setLoading(false);
40
+ });
41
+ async function checkSession() {
42
+ const {
43
+ data: { session }
44
+ } = await supabase.auth.getSession();
45
+ if (!session) {
46
+ await supabase.auth.signOut({ scope: "local" });
47
+ }
48
+ }
49
+ const POLL_MS = 3e4;
50
+ const interval = setInterval(checkSession, POLL_MS);
51
+ const handleVisibilityChange = () => {
52
+ if (document.visibilityState === "visible") {
53
+ checkSession();
54
+ }
55
+ };
56
+ document.addEventListener("visibilitychange", handleVisibilityChange);
57
+ return () => {
58
+ subscription.unsubscribe();
59
+ clearInterval(interval);
60
+ document.removeEventListener("visibilitychange", handleVisibilityChange);
61
+ };
62
+ }, [supabase]);
63
+ useEffect(() => {
64
+ if (loading || user) return;
65
+ const url = loginUrl != null ? loginUrl : `${process.env.NEXT_PUBLIC_ACCOUNTS_URL}/login`;
66
+ if (url) {
67
+ window.location.href = `${url}?next=${encodeURIComponent(window.location.href)}`;
68
+ }
69
+ }, [loading, user, loginUrl]);
70
+ const profile = {
71
+ name: (_b = (_a = user == null ? void 0 : user.user_metadata) == null ? void 0 : _a.full_name) != null ? _b : "\u2014",
72
+ email: (_c = user == null ? void 0 : user.email) != null ? _c : "",
73
+ avatarUrl: (_d = user == null ? void 0 : user.user_metadata) == null ? void 0 : _d.avatar_url
74
+ };
75
+ if (loading || !user) return null;
76
+ return /* @__PURE__ */ jsx(Context.Provider, { value: { supabase, user, profile, loading }, children });
77
+ }
78
+ function useAuth() {
79
+ const context = useContext(Context);
80
+ if (context === void 0) {
81
+ throw new Error("useAuth must be used within an AuthProvider");
82
+ }
83
+ return context;
84
+ }
85
+
86
+ export {
87
+ AuthProvider,
88
+ useAuth
89
+ };
@@ -0,0 +1,38 @@
1
+ 'use client';
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
20
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
21
+ var __objRest = (source, exclude) => {
22
+ var target = {};
23
+ for (var prop in source)
24
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
25
+ target[prop] = source[prop];
26
+ if (source != null && __getOwnPropSymbols)
27
+ for (var prop of __getOwnPropSymbols(source)) {
28
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
29
+ target[prop] = source[prop];
30
+ }
31
+ return target;
32
+ };
33
+
34
+ export {
35
+ __spreadValues,
36
+ __spreadProps,
37
+ __objRest
38
+ };
@@ -0,0 +1,131 @@
1
+ 'use client';
2
+
3
+ // src/components/verify-code.tsx
4
+ import { useState, useEffect, useCallback } from "react";
5
+ import { Box, Button, PinInput, Stack, Text, Title, Anchor } from "@mantine/core";
6
+ import { createBrowserClient } from "@frequencyads/db/client";
7
+ import { jsx, jsxs } from "react/jsx-runtime";
8
+ function VerifyCodeForm({
9
+ email,
10
+ type,
11
+ onSuccess,
12
+ onResend,
13
+ onBack,
14
+ backLabel = "Back to sign in",
15
+ header
16
+ }) {
17
+ const [code, setCode] = useState("");
18
+ const [error, setError] = useState(null);
19
+ const [loading, setLoading] = useState(false);
20
+ const [verified, setVerified] = useState(false);
21
+ const [resendCooldown, setResendCooldown] = useState(0);
22
+ useEffect(() => {
23
+ if (resendCooldown <= 0) return;
24
+ const timer = setTimeout(() => setResendCooldown((c) => c - 1), 1e3);
25
+ return () => clearTimeout(timer);
26
+ }, [resendCooldown]);
27
+ const handleVerify = useCallback(
28
+ async (value) => {
29
+ if (value.length !== 6) return;
30
+ setError(null);
31
+ setLoading(true);
32
+ try {
33
+ const supabase = createBrowserClient();
34
+ const { error: verifyError } = await supabase.auth.verifyOtp({
35
+ email,
36
+ token: value,
37
+ type
38
+ });
39
+ if (verifyError) {
40
+ setError("This code is invalid or has expired. Please request a new one.");
41
+ setCode("");
42
+ } else {
43
+ setVerified(true);
44
+ onSuccess();
45
+ }
46
+ } finally {
47
+ setLoading(false);
48
+ }
49
+ },
50
+ [email, type, onSuccess]
51
+ );
52
+ const [resending, setResending] = useState(false);
53
+ const handleResend = async () => {
54
+ setError(null);
55
+ setResending(true);
56
+ try {
57
+ const result = await onResend();
58
+ if (result.error) {
59
+ setError(result.error);
60
+ } else {
61
+ setResendCooldown(60);
62
+ }
63
+ } finally {
64
+ setResending(false);
65
+ }
66
+ };
67
+ return /* @__PURE__ */ jsxs(Box, { w: "100%", maw: 460, children: [
68
+ header && /* @__PURE__ */ jsx(Box, { mb: "lg", style: { textAlign: "center" }, children: header }),
69
+ /* @__PURE__ */ jsx(Title, { order: 3, fw: 600, mb: "xs", c: "gray.7", children: type === "email" ? "Verify your email" : "Enter reset code" }),
70
+ /* @__PURE__ */ jsxs(Text, { size: "sm", c: "gray.6", mb: "lg", children: [
71
+ "We sent a 6-digit code to ",
72
+ /* @__PURE__ */ jsx("strong", { children: email })
73
+ ] }),
74
+ /* @__PURE__ */ jsxs(Stack, { children: [
75
+ /* @__PURE__ */ jsx(
76
+ PinInput,
77
+ {
78
+ length: 6,
79
+ type: "number",
80
+ oneTimeCode: true,
81
+ autoFocus: true,
82
+ size: "lg",
83
+ value: code,
84
+ onChange: (value) => {
85
+ setCode(value);
86
+ if (value.length > 0) setError(null);
87
+ },
88
+ onComplete: handleVerify,
89
+ error: !!error,
90
+ disabled: loading || verified,
91
+ styles: { input: { fontWeight: 600, fontSize: 20 } }
92
+ }
93
+ ),
94
+ error && /* @__PURE__ */ jsx(Text, { size: "sm", c: "red", children: error }),
95
+ /* @__PURE__ */ jsx(
96
+ Button,
97
+ {
98
+ fullWidth: true,
99
+ size: "md",
100
+ loading: loading || verified,
101
+ onClick: () => handleVerify(code),
102
+ children: "Verify"
103
+ }
104
+ ),
105
+ /* @__PURE__ */ jsxs(Text, { size: "sm", c: "gray.6", ta: "center", children: [
106
+ "Didn't receive the code?",
107
+ " ",
108
+ resendCooldown > 0 ? /* @__PURE__ */ jsxs(Text, { span: true, c: "gray.5", children: [
109
+ "Resend in ",
110
+ resendCooldown,
111
+ "s"
112
+ ] }) : /* @__PURE__ */ jsx(
113
+ Anchor,
114
+ {
115
+ component: "button",
116
+ type: "button",
117
+ size: "sm",
118
+ onClick: handleResend,
119
+ disabled: resending,
120
+ children: resending ? "Sending\u2026" : "Resend code"
121
+ }
122
+ )
123
+ ] }),
124
+ onBack && /* @__PURE__ */ jsx(Anchor, { component: "button", type: "button", size: "sm", onClick: onBack, ta: "center", children: backLabel })
125
+ ] })
126
+ ] });
127
+ }
128
+
129
+ export {
130
+ VerifyCodeForm
131
+ };
@@ -0,0 +1,63 @@
1
+ 'use client';
2
+ import {
3
+ useAuth
4
+ } from "./chunk-JIENWB3K.mjs";
5
+ import {
6
+ getSignOutUrl
7
+ } from "./chunk-P4OKC4U7.mjs";
8
+
9
+ // src/components/user-menu.tsx
10
+ import { Menu, UnstyledButton, Group, Avatar, Text, rem } from "@mantine/core";
11
+ import { IconChevronDown, IconLogout } from "@tabler/icons-react";
12
+ import { useState } from "react";
13
+ import { jsx, jsxs } from "react/jsx-runtime";
14
+ function UserMenu() {
15
+ const { user, profile } = useAuth();
16
+ const [opened, setOpened] = useState(false);
17
+ if (!user) return null;
18
+ const handleLogout = () => {
19
+ window.location.href = getSignOutUrl();
20
+ };
21
+ return /* @__PURE__ */ jsxs(
22
+ Menu,
23
+ {
24
+ width: 260,
25
+ position: "bottom-end",
26
+ transitionProps: { transition: "pop-top-right" },
27
+ opened,
28
+ onChange: setOpened,
29
+ children: [
30
+ /* @__PURE__ */ jsx(Menu.Target, { children: /* @__PURE__ */ jsx(
31
+ UnstyledButton,
32
+ {
33
+ style: {
34
+ padding: "var(--mantine-spacing-xs)",
35
+ borderRadius: "var(--mantine-radius-sm)",
36
+ transition: "background-color 100ms ease"
37
+ },
38
+ children: /* @__PURE__ */ jsxs(Group, { gap: 7, children: [
39
+ /* @__PURE__ */ jsx(Avatar, { src: profile.avatarUrl, alt: profile.name, radius: "xl", size: 32 }),
40
+ /* @__PURE__ */ jsx(Text, { fw: 500, size: "sm", lh: 1, mr: 3, children: profile.name }),
41
+ /* @__PURE__ */ jsx(IconChevronDown, { style: { width: rem(12), height: rem(12) }, stroke: 1.5 })
42
+ ] })
43
+ }
44
+ ) }),
45
+ /* @__PURE__ */ jsxs(Menu.Dropdown, { children: [
46
+ /* @__PURE__ */ jsx(Menu.Label, { children: /* @__PURE__ */ jsx(Text, { size: "xs", c: "dimmed", truncate: true, children: profile.email }) }),
47
+ /* @__PURE__ */ jsx(
48
+ Menu.Item,
49
+ {
50
+ leftSection: /* @__PURE__ */ jsx(IconLogout, { style: { width: rem(16), height: rem(16) }, stroke: 1.5 }),
51
+ onClick: handleLogout,
52
+ children: "Sign out"
53
+ }
54
+ )
55
+ ] })
56
+ ]
57
+ }
58
+ );
59
+ }
60
+
61
+ export {
62
+ UserMenu
63
+ };
@@ -0,0 +1,41 @@
1
+ 'use client';
2
+ import {
3
+ __objRest,
4
+ __spreadProps,
5
+ __spreadValues
6
+ } from "./chunk-NS2JRZGO.mjs";
7
+
8
+ // src/components/sign-out-button.tsx
9
+ import { Button } from "@mantine/core";
10
+ import { IconLogout } from "@tabler/icons-react";
11
+ import { jsx } from "react/jsx-runtime";
12
+ function getSignOutUrl(returnTo) {
13
+ const accountsUrl = process.env.NEXT_PUBLIC_ACCOUNTS_URL;
14
+ if (!accountsUrl) return "#";
15
+ const next = returnTo != null ? returnTo : window.location.origin;
16
+ return `${accountsUrl}/logout?next=${encodeURIComponent(next)}`;
17
+ }
18
+ function SignOutButton(_a) {
19
+ var _b = _a, { returnTo } = _b, props = __objRest(_b, ["returnTo"]);
20
+ var _a2;
21
+ const handleClick = () => {
22
+ window.location.href = getSignOutUrl(returnTo);
23
+ };
24
+ return /* @__PURE__ */ jsx(
25
+ Button,
26
+ __spreadProps(__spreadValues({
27
+ onClick: handleClick,
28
+ variant: "light",
29
+ color: "red",
30
+ leftSection: /* @__PURE__ */ jsx(IconLogout, { size: 16 }),
31
+ w: "fit-content"
32
+ }, props), {
33
+ children: (_a2 = props.children) != null ? _a2 : "Sign out"
34
+ })
35
+ );
36
+ }
37
+
38
+ export {
39
+ getSignOutUrl,
40
+ SignOutButton
41
+ };