@digilogiclabs/saas-factory-auth 0.1.0 → 0.1.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.
- package/README.md +148 -0
- package/dist/index.js +19 -56
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +54 -8
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# @digilogiclabs/saas-factory-auth
|
|
2
|
+
|
|
3
|
+
A reusable authentication package built with Next.js, React, and Supabase. This package provides a complete authentication solution for your Next.js applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔐 Complete authentication flows
|
|
8
|
+
- Sign in / Sign up
|
|
9
|
+
- Password reset
|
|
10
|
+
- Email verification
|
|
11
|
+
- Magic link authentication
|
|
12
|
+
- 🛡️ Protected routes
|
|
13
|
+
- 📱 Two-factor authentication
|
|
14
|
+
- 👥 Role-based access control
|
|
15
|
+
- 🔑 OAuth provider support
|
|
16
|
+
- 💫 Zero-configuration Supabase integration
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @digilogiclabs/saas-factory-auth
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Requirements
|
|
25
|
+
|
|
26
|
+
- Next.js 13 or higher
|
|
27
|
+
- React 18 or higher
|
|
28
|
+
- Supabase project and credentials
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
1. Set up your Supabase environment variables:
|
|
33
|
+
|
|
34
|
+
```env
|
|
35
|
+
NEXT_PUBLIC_SUPABASE_URL=your-supabase-url
|
|
36
|
+
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-supabase-anon-key
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
2. Initialize the authentication provider:
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
// app/providers.tsx
|
|
43
|
+
import { AuthProvider } from '@digilogiclabs/saas-factory-auth';
|
|
44
|
+
|
|
45
|
+
export function Providers({ children }: { children: React.ReactNode }) {
|
|
46
|
+
return <AuthProvider>{children}</AuthProvider>;
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
3. Use authentication hooks in your components:
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import { useAuth } from '@digilogiclabs/saas-factory-auth';
|
|
54
|
+
|
|
55
|
+
export default function Profile() {
|
|
56
|
+
const { user, signOut } = useAuth();
|
|
57
|
+
|
|
58
|
+
if (!user) return <div>Please sign in</div>;
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div>
|
|
62
|
+
<h1>Welcome, {user.email}</h1>
|
|
63
|
+
<button onClick={signOut}>Sign Out</button>
|
|
64
|
+
</div>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Protected Routes
|
|
70
|
+
|
|
71
|
+
Protect your routes using the middleware:
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
// middleware.ts
|
|
75
|
+
export { authMiddleware as middleware } from '@digilogiclabs/saas-factory-auth';
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Role-Based Access Control
|
|
79
|
+
|
|
80
|
+
Define and check user roles:
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
import { useAuth, withRole } from '@digilogiclabs/saas-factory-auth';
|
|
84
|
+
|
|
85
|
+
// Protect component with role
|
|
86
|
+
const AdminDashboard = withRole(['admin'])(() => {
|
|
87
|
+
return <div>Admin Only Content</div>;
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Use role checks in components
|
|
91
|
+
function Dashboard() {
|
|
92
|
+
const { hasRole } = useAuth();
|
|
93
|
+
|
|
94
|
+
if (hasRole('admin')) {
|
|
95
|
+
return <div>Admin Content</div>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return <div>User Content</div>;
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## OAuth Providers
|
|
103
|
+
|
|
104
|
+
Enable social authentication:
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
import { OAuthButtons } from '@digilogiclabs/saas-factory-auth';
|
|
108
|
+
|
|
109
|
+
function SignIn() {
|
|
110
|
+
return (
|
|
111
|
+
<div>
|
|
112
|
+
<OAuthButtons providers={['google', 'github']} />
|
|
113
|
+
</div>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## API Reference
|
|
119
|
+
|
|
120
|
+
### Hooks
|
|
121
|
+
|
|
122
|
+
- `useAuth()` - Core authentication hook
|
|
123
|
+
- `useUser()` - Current user hook
|
|
124
|
+
- `useRole()` - Role management hook
|
|
125
|
+
- `useProtectedRoute()` - Route protection hook
|
|
126
|
+
|
|
127
|
+
### Components
|
|
128
|
+
|
|
129
|
+
- `<AuthProvider>` - Authentication context provider
|
|
130
|
+
- `<ProtectedRoute>` - Route protection component
|
|
131
|
+
- `<SignIn>` - Sign-in form component
|
|
132
|
+
- `<SignUp>` - Sign-up form component
|
|
133
|
+
- `<OAuthButtons>` - Social authentication buttons
|
|
134
|
+
- `<MagicLink>` - Magic link authentication component
|
|
135
|
+
|
|
136
|
+
### Utilities
|
|
137
|
+
|
|
138
|
+
- `withAuth()` - HOC for protected components
|
|
139
|
+
- `withRole()` - HOC for role-based components
|
|
140
|
+
- `authMiddleware` - Next.js middleware
|
|
141
|
+
|
|
142
|
+
## Contributing
|
|
143
|
+
|
|
144
|
+
We welcome contributions! Please see our [contributing guide](CONTRIBUTING.md) for details.
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
MIT © [DigiLogic Labs](https://github.com/DigiLogicLabs)
|
package/dist/index.js
CHANGED
|
@@ -1,52 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
'use strict';
|
|
19
2
|
|
|
20
|
-
|
|
21
|
-
var
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
createClient: () => createClient,
|
|
25
|
-
supabase: () => supabase,
|
|
26
|
-
useAuth: () => useAuth,
|
|
27
|
-
useAuthContext: () => useAuthContext,
|
|
28
|
-
useAuthStore: () => useAuthStore
|
|
29
|
-
});
|
|
30
|
-
module.exports = __toCommonJS(index_exports);
|
|
31
|
-
|
|
32
|
-
// src/hooks/useAuth.ts
|
|
33
|
-
var import_react = require("react");
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var zustand = require('zustand');
|
|
5
|
+
var authHelpersNextjs = require('@supabase/auth-helpers-nextjs');
|
|
6
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
34
7
|
|
|
35
|
-
// src/store/authStore.ts
|
|
36
|
-
var import_zustand = require("zustand");
|
|
37
|
-
|
|
38
|
-
// src/utils/supabase.ts
|
|
39
|
-
var import_auth_helpers_nextjs = require("@supabase/auth-helpers-nextjs");
|
|
40
8
|
var createClient = () => {
|
|
41
9
|
if (!process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY) {
|
|
42
10
|
throw new Error("Missing Supabase environment variables");
|
|
43
11
|
}
|
|
44
|
-
return
|
|
12
|
+
return authHelpersNextjs.createClientComponentClient();
|
|
45
13
|
};
|
|
46
14
|
var supabase = createClient();
|
|
47
15
|
|
|
48
16
|
// src/store/authStore.ts
|
|
49
|
-
var useAuthStore =
|
|
17
|
+
var useAuthStore = zustand.create((set) => ({
|
|
50
18
|
user: null,
|
|
51
19
|
loading: true,
|
|
52
20
|
error: null,
|
|
@@ -141,7 +109,7 @@ var useAuthStore = (0, import_zustand.create)((set) => ({
|
|
|
141
109
|
// src/hooks/useAuth.ts
|
|
142
110
|
var useAuth = () => {
|
|
143
111
|
const store = useAuthStore();
|
|
144
|
-
|
|
112
|
+
react.useEffect(() => {
|
|
145
113
|
supabase.auth.getSession().then(({ data: { session } }) => {
|
|
146
114
|
var _a;
|
|
147
115
|
store.updateUser((_a = session == null ? void 0 : session.user) != null ? _a : null);
|
|
@@ -157,29 +125,24 @@ var useAuth = () => {
|
|
|
157
125
|
}, []);
|
|
158
126
|
return store;
|
|
159
127
|
};
|
|
160
|
-
|
|
161
|
-
// src/components/AuthProvider.tsx
|
|
162
|
-
var import_react2 = require("react");
|
|
163
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
164
|
-
var AuthContext = (0, import_react2.createContext)(null);
|
|
128
|
+
var AuthContext = react.createContext(null);
|
|
165
129
|
var AuthProvider = ({ children }) => {
|
|
166
130
|
const auth = useAuth();
|
|
167
|
-
return /* @__PURE__ */
|
|
131
|
+
return /* @__PURE__ */ jsxRuntime.jsx(AuthContext.Provider, { value: auth, children });
|
|
168
132
|
};
|
|
169
133
|
var useAuthContext = () => {
|
|
170
|
-
const context =
|
|
134
|
+
const context = react.useContext(AuthContext);
|
|
171
135
|
if (!context) {
|
|
172
136
|
throw new Error("useAuthContext must be used within an AuthProvider");
|
|
173
137
|
}
|
|
174
138
|
return context;
|
|
175
139
|
};
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
});
|
|
140
|
+
|
|
141
|
+
exports.AuthProvider = AuthProvider;
|
|
142
|
+
exports.createClient = createClient;
|
|
143
|
+
exports.supabase = supabase;
|
|
144
|
+
exports.useAuth = useAuth;
|
|
145
|
+
exports.useAuthContext = useAuthContext;
|
|
146
|
+
exports.useAuthStore = useAuthStore;
|
|
147
|
+
//# sourceMappingURL=index.js.map
|
|
185
148
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/utils/supabase.ts","../src/store/authStore.ts","../src/hooks/useAuth.ts","../src/components/AuthProvider.tsx"],"names":["createClientComponentClient","create","useEffect","createContext","useContext"],"mappings":";;;;;;;AAIO,IAAM,eAAe,MAAM;AAChC,EAAA,IAAI,CAAC,OAAQ,CAAA,GAAA,CAAI,4BAA4B,CAAC,OAAA,CAAQ,IAAI,6BAA+B,EAAA;AACvF,IAAM,MAAA,IAAI,MAAM,wCAAwC,CAAA;AAAA;AAG1D,EAAA,OAAOA,6CAAsC,EAAA;AAC/C;AAEO,IAAM,WAAW,YAAa;;;ACRxB,IAAA,YAAA,GAAeC,cAAkB,CAAA,CAAC,GAAS,MAAA;AAAA,EACtD,IAAM,EAAA,IAAA;AAAA,EACN,OAAS,EAAA,IAAA;AAAA,EACT,KAAO,EAAA,IAAA;AAAA,EAEP,MAAA,EAAQ,OAAO,KAAA,EAAe,QAAqB,KAAA;AACjD,IAAI,IAAA;AACF,MAAA,GAAA,CAAI,EAAE,OAAA,EAAS,IAAM,EAAA,KAAA,EAAO,MAAM,CAAA;AAClC,MAAA,MAAM,EAAE,KAAM,EAAA,GAAI,MAAM,QAAA,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AAG,EAAE,CAAA;AAEL,EAAO,OAAA,KAAA;AACT;ACrBA,IAAM,WAAA,GAAcC,oBAAgC,IAAI,CAAA;AAEjD,IAAM,YAAe,GAAA,CAAC,EAAE,QAAA,EAAwC,KAAA;AACrE,EAAA,MAAM,OAAO,OAAQ,EAAA;AAErB,EAAA,sCAAQ,WAAY,CAAA,QAAA,EAAZ,EAAqB,KAAA,EAAO,MAAO,QAAS,EAAA,CAAA;AACtD;AAEO,IAAM,iBAAiB,MAAM;AAClC,EAAM,MAAA,OAAA,GAAUC,iBAAW,WAAW,CAAA;AACtC,EAAA,IAAI,CAAC,OAAS,EAAA;AACZ,IAAM,MAAA,IAAI,MAAM,oDAAoD,CAAA;AAAA;AAEtE,EAAO,OAAA,OAAA;AACT","file":"index.js","sourcesContent":["\nimport { createClientComponentClient } from '@supabase/auth-helpers-nextjs'\nimport type { Database } from './database.types'\n\nexport const createClient = () => {\n if (!process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY) {\n throw new Error('Missing Supabase environment variables')\n }\n\n return createClientComponentClient<Database>()\n}\n\nexport const supabase = createClient()","import { create } from 'zustand'\nimport { AuthStore } from '../types'\nimport { supabase } from '../utils/supabase'\n\nexport const useAuthStore = create<AuthStore>((set) => ({\n user: null,\n loading: true,\n error: null,\n\n signIn: async (email: string, password: string) => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.signInWithPassword({\n email,\n password,\n })\n if (error) throw error\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n\n signUp: async (email: string, password: string) => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.signUp({\n email,\n password,\n })\n if (error) throw error\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n\n signOut: async () => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.signOut()\n if (error) throw error\n set({ user: null })\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n\n resetPassword: async (email: string) => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.resetPasswordForEmail(email)\n if (error) throw error\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n signInWithOAuth: async (provider: string, redirectTo?: string) => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.signInWithOAuth({\n provider: provider as any,\n options: {\n redirectTo\n }\n })\n if (error) throw error\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n\n signInWithMagicLink: async (email: string, redirectTo?: string) => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.signInWithOtp({\n email,\n options: {\n emailRedirectTo: redirectTo\n }\n })\n if (error) throw error\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n updateUser: (user) => set({ user }),\n setLoading: (loading) => set({ loading }),\n setError: (error) => set({ error }),\n}))\n\n// Example usage in a Next.js app:\n/*\nimport { AuthForm } from '@digilogiclabs/saas-factory-auth'\n\nexport default function LoginPage() {\n const router = useRouter()\n\n return (\n <AuthForm\n defaultMode=\"signIn\"\n onSuccess={() => router.push('/dashboard')}\n providers={['google', 'github']}\n redirectTo=\"https://your-app.com/auth/callback\"\n />\n )\n}\n*/","'use client'\n\nimport { useEffect } from 'react'\nimport { useAuthStore } from '../store/authStore'\nimport { supabase } from '../utils/supabase'\n\nexport const useAuth = () => {\n const store = useAuthStore()\n\n useEffect(() => {\n // Check active session\n supabase.auth.getSession().then(({ data: { session } }) => {\n store.updateUser(session?.user ?? null)\n store.setLoading(false)\n })\n\n // Listen for auth changes\n const {\n data: { subscription },\n } = supabase.auth.onAuthStateChange((_event, session) => {\n store.updateUser(session?.user ?? null)\n })\n\n return () => subscription.unsubscribe()\n }, [])\n\n return store\n}","'use client'\n\nimport { createContext, useContext, ReactNode } from 'react'\nimport { useAuth } from '../hooks/useAuth'\nimport type { AuthStore } from '../types'\n\nconst AuthContext = createContext<AuthStore | null>(null)\n\nexport const AuthProvider = ({ children }: { children: ReactNode }) => {\n const auth = useAuth()\n\n return <AuthContext.Provider value={auth}>{children}</AuthContext.Provider>\n}\n\nexport const useAuthContext = () => {\n const context = useContext(AuthContext)\n if (!context) {\n throw new Error('useAuthContext must be used within an AuthProvider')\n }\n return context\n}"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import { create } from "zustand";
|
|
1
|
+
import { createContext, useEffect, useContext } from 'react';
|
|
2
|
+
import { create } from 'zustand';
|
|
3
|
+
import { createClientComponentClient } from '@supabase/auth-helpers-nextjs';
|
|
4
|
+
import { jsx } from 'react/jsx-runtime';
|
|
6
5
|
|
|
7
|
-
// src/utils/supabase.ts
|
|
8
|
-
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";
|
|
9
6
|
var createClient = () => {
|
|
10
7
|
if (!process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY) {
|
|
11
8
|
throw new Error("Missing Supabase environment variables");
|
|
@@ -126,10 +123,6 @@ var useAuth = () => {
|
|
|
126
123
|
}, []);
|
|
127
124
|
return store;
|
|
128
125
|
};
|
|
129
|
-
|
|
130
|
-
// src/components/AuthProvider.tsx
|
|
131
|
-
import { createContext, useContext } from "react";
|
|
132
|
-
import { jsx } from "react/jsx-runtime";
|
|
133
126
|
var AuthContext = createContext(null);
|
|
134
127
|
var AuthProvider = ({ children }) => {
|
|
135
128
|
const auth = useAuth();
|
|
@@ -142,12 +135,7 @@ var useAuthContext = () => {
|
|
|
142
135
|
}
|
|
143
136
|
return context;
|
|
144
137
|
};
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
supabase,
|
|
149
|
-
useAuth,
|
|
150
|
-
useAuthContext,
|
|
151
|
-
useAuthStore
|
|
152
|
-
};
|
|
138
|
+
|
|
139
|
+
export { AuthProvider, createClient, supabase, useAuth, useAuthContext, useAuthStore };
|
|
140
|
+
//# sourceMappingURL=index.mjs.map
|
|
153
141
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/utils/supabase.ts","../src/store/authStore.ts","../src/hooks/useAuth.ts","../src/components/AuthProvider.tsx"],"names":[],"mappings":";;;;;AAIO,IAAM,eAAe,MAAM;AAChC,EAAA,IAAI,CAAC,OAAQ,CAAA,GAAA,CAAI,4BAA4B,CAAC,OAAA,CAAQ,IAAI,6BAA+B,EAAA;AACvF,IAAM,MAAA,IAAI,MAAM,wCAAwC,CAAA;AAAA;AAG1D,EAAA,OAAO,2BAAsC,EAAA;AAC/C;AAEO,IAAM,WAAW,YAAa;;;ACRxB,IAAA,YAAA,GAAe,MAAkB,CAAA,CAAC,GAAS,MAAA;AAAA,EACtD,IAAM,EAAA,IAAA;AAAA,EACN,OAAS,EAAA,IAAA;AAAA,EACT,KAAO,EAAA,IAAA;AAAA,EAEP,MAAA,EAAQ,OAAO,KAAA,EAAe,QAAqB,KAAA;AACjD,IAAI,IAAA;AACF,MAAA,GAAA,CAAI,EAAE,OAAA,EAAS,IAAM,EAAA,KAAA,EAAO,MAAM,CAAA;AAClC,MAAA,MAAM,EAAE,KAAM,EAAA,GAAI,MAAM,QAAA,CAAS,KAAK,kBAAmB,CAAA;AAAA,QACvD,KAAA;AAAA,QACA;AAAA,OACD,CAAA;AACD,MAAA,IAAI,OAAa,MAAA,KAAA;AAAA,aACV,KAAO,EAAA;AACd,MAAI,GAAA,CAAA,EAAE,OAAuB,CAAA;AAAA,KAC7B,SAAA;AACA,MAAI,GAAA,CAAA,EAAE,OAAS,EAAA,KAAA,EAAO,CAAA;AAAA;AACxB,GACF;AAAA,EAEA,MAAA,EAAQ,OAAO,KAAA,EAAe,QAAqB,KAAA;AAC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A,GAAU,WAAW,WAAW,CAAA;AACtC,EAAA,IAAI,CAAC,OAAS,EAAA;AACZ,IAAM,MAAA,IAAI,MAAM,oDAAoD,CAAA;AAAA;AAEtE,EAAO,OAAA,OAAA;AACT","file":"index.mjs","sourcesContent":["\nimport { createClientComponentClient } from '@supabase/auth-helpers-nextjs'\nimport type { Database } from './database.types'\n\nexport const createClient = () => {\n if (!process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY) {\n throw new Error('Missing Supabase environment variables')\n }\n\n return createClientComponentClient<Database>()\n}\n\nexport const supabase = createClient()","import { create } from 'zustand'\nimport { AuthStore } from '../types'\nimport { supabase } from '../utils/supabase'\n\nexport const useAuthStore = create<AuthStore>((set) => ({\n user: null,\n loading: true,\n error: null,\n\n signIn: async (email: string, password: string) => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.signInWithPassword({\n email,\n password,\n })\n if (error) throw error\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n\n signUp: async (email: string, password: string) => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.signUp({\n email,\n password,\n })\n if (error) throw error\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n\n signOut: async () => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.signOut()\n if (error) throw error\n set({ user: null })\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n\n resetPassword: async (email: string) => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.resetPasswordForEmail(email)\n if (error) throw error\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n signInWithOAuth: async (provider: string, redirectTo?: string) => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.signInWithOAuth({\n provider: provider as any,\n options: {\n redirectTo\n }\n })\n if (error) throw error\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n\n signInWithMagicLink: async (email: string, redirectTo?: string) => {\n try {\n set({ loading: true, error: null })\n const { error } = await supabase.auth.signInWithOtp({\n email,\n options: {\n emailRedirectTo: redirectTo\n }\n })\n if (error) throw error\n } catch (error) {\n set({ error: error as Error })\n } finally {\n set({ loading: false })\n }\n },\n updateUser: (user) => set({ user }),\n setLoading: (loading) => set({ loading }),\n setError: (error) => set({ error }),\n}))\n\n// Example usage in a Next.js app:\n/*\nimport { AuthForm } from '@digilogiclabs/saas-factory-auth'\n\nexport default function LoginPage() {\n const router = useRouter()\n\n return (\n <AuthForm\n defaultMode=\"signIn\"\n onSuccess={() => router.push('/dashboard')}\n providers={['google', 'github']}\n redirectTo=\"https://your-app.com/auth/callback\"\n />\n )\n}\n*/","'use client'\n\nimport { useEffect } from 'react'\nimport { useAuthStore } from '../store/authStore'\nimport { supabase } from '../utils/supabase'\n\nexport const useAuth = () => {\n const store = useAuthStore()\n\n useEffect(() => {\n // Check active session\n supabase.auth.getSession().then(({ data: { session } }) => {\n store.updateUser(session?.user ?? null)\n store.setLoading(false)\n })\n\n // Listen for auth changes\n const {\n data: { subscription },\n } = supabase.auth.onAuthStateChange((_event, session) => {\n store.updateUser(session?.user ?? null)\n })\n\n return () => subscription.unsubscribe()\n }, [])\n\n return store\n}","'use client'\n\nimport { createContext, useContext, ReactNode } from 'react'\nimport { useAuth } from '../hooks/useAuth'\nimport type { AuthStore } from '../types'\n\nconst AuthContext = createContext<AuthStore | null>(null)\n\nexport const AuthProvider = ({ children }: { children: ReactNode }) => {\n const auth = useAuth()\n\n return <AuthContext.Provider value={auth}>{children}</AuthContext.Provider>\n}\n\nexport const useAuthContext = () => {\n const context = useContext(AuthContext)\n if (!context) {\n throw new Error('useAuthContext must be used within an AuthProvider')\n }\n return context\n}"]}
|
package/package.json
CHANGED
|
@@ -1,26 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@digilogiclabs/saas-factory-auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"private": false,
|
|
5
|
+
"description": "Authentication package for Next.js applications using Supabase",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"authentication",
|
|
8
|
+
"nextjs",
|
|
9
|
+
"react",
|
|
10
|
+
"supabase"
|
|
11
|
+
],
|
|
12
|
+
"author": "DigiLogic Labs",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"homepage": "https://github.com/DigiLogicLabs/saas-factory-auth#readme",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/DigiLogicLabs/saas-factory-auth.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/DigiLogicLabs/saas-factory-auth/issues"
|
|
21
|
+
},
|
|
5
22
|
"publishConfig": {
|
|
6
|
-
"access": "restricted"
|
|
23
|
+
"access": "restricted",
|
|
24
|
+
"registry": "https://registry.npmjs.org/",
|
|
25
|
+
"scope": "@digilogiclabs"
|
|
7
26
|
},
|
|
8
27
|
"main": "./dist/index.js",
|
|
9
28
|
"module": "./dist/index.mjs",
|
|
10
29
|
"types": "./dist/index.d.ts",
|
|
11
30
|
"exports": {
|
|
12
31
|
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
13
33
|
"import": "./dist/index.mjs",
|
|
14
|
-
"require": "./dist/index.js"
|
|
15
|
-
"types": "./dist/index.d.ts"
|
|
34
|
+
"require": "./dist/index.js"
|
|
16
35
|
}
|
|
17
36
|
},
|
|
18
|
-
"files": [
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"sideEffects": false,
|
|
19
43
|
"scripts": {
|
|
20
44
|
"build": "tsup --dts",
|
|
21
45
|
"dev": "tsup --watch --dts",
|
|
22
46
|
"clean": "rm -rf dist",
|
|
23
|
-
"type-check": "tsc --noEmit"
|
|
47
|
+
"type-check": "tsc --noEmit",
|
|
48
|
+
"test": "jest --passWithNoTests",
|
|
49
|
+
"test:watch": "jest --watch",
|
|
50
|
+
"test:coverage": "jest --coverage",
|
|
51
|
+
"lint": "eslint .",
|
|
52
|
+
"format": "prettier --write .",
|
|
53
|
+
"prepare": "husky install",
|
|
54
|
+
"release": "npm run clean && npm run build && changeset publish",
|
|
55
|
+
"version": "changeset version",
|
|
56
|
+
"prepack": "npm run build"
|
|
24
57
|
},
|
|
25
58
|
"dependencies": {
|
|
26
59
|
"@supabase/auth-helpers-nextjs": "^0.8.7",
|
|
@@ -33,11 +66,24 @@
|
|
|
33
66
|
"react-dom": ">=18"
|
|
34
67
|
},
|
|
35
68
|
"devDependencies": {
|
|
69
|
+
"@changesets/cli": "^2.27.11",
|
|
70
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
71
|
+
"@testing-library/react": "^16.1.0",
|
|
36
72
|
"@types/react": "^18.2.48",
|
|
37
73
|
"@typescript-eslint/eslint-plugin": "^6.19.1",
|
|
38
74
|
"@typescript-eslint/parser": "^6.19.1",
|
|
75
|
+
"babel-jest": "^29.7.0",
|
|
39
76
|
"eslint": "^8.56.0",
|
|
77
|
+
"identity-obj-proxy": "^3.0.0",
|
|
78
|
+
"jest": "^29.7.0",
|
|
79
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
40
80
|
"tsup": "^8.0.1",
|
|
41
|
-
"typescript": "^5.3.3"
|
|
81
|
+
"typescript": "^5.3.3",
|
|
82
|
+
"husky": "^8.0.3",
|
|
83
|
+
"lint-staged": "^15.0.2",
|
|
84
|
+
"prettier": "^3.0.3"
|
|
85
|
+
},
|
|
86
|
+
"engines": {
|
|
87
|
+
"node": ">=18.0.0"
|
|
42
88
|
}
|
|
43
|
-
}
|
|
89
|
+
}
|