@xouteiro/auth_npm 1.0.0 → 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.
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@xouteiro/auth_npm",
3
- "version": "1.0.0",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "1.0.2",
4
+ "description": "Authentication package for Supabase",
5
+ "main": "src/index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "build": "tsup"
8
8
  },
9
- "keywords": [],
9
+ "keywords": [
10
+ "supabase",
11
+ "auth",
12
+ "authentication"
13
+ ],
10
14
  "author": "",
11
15
  "license": "ISC",
12
16
  "dependencies": {
@@ -15,6 +19,10 @@
15
19
  "@supabase/supabase-js": "^2.52.1"
16
20
  },
17
21
  "devDependencies": {
18
- "tsup": "^8.5.0"
22
+ "@types/react": "^19.1.9",
23
+ "@types/react-dom": "^19.1.7",
24
+ "esbuild": "^0.25.8",
25
+ "tsup": "^8.5.0",
26
+ "typescript": "^5.9.2"
19
27
  }
20
28
  }
@@ -1,35 +1,39 @@
1
- import { useEffect, useState } from 'react'
1
+ import { useState, useEffect } from 'react'
2
2
  import { Auth } from '@supabase/auth-ui-react'
3
3
  import { ThemeSupa } from '@supabase/auth-ui-shared'
4
4
  import { supabase } from './supabaseClient'
5
5
 
6
- export default function AuthWrapper({ onAuth }) {
6
+ export default function AuthWrapper({ onAuth, providers = ['google', 'github'], theme = 'dark' }) {
7
+ if (!supabase) {
8
+ throw new Error('Supabase client is not initialized. Call initializeSupabase() before using AuthWrapper.')
9
+ }
10
+
7
11
  const [session, setSession] = useState(null)
8
12
 
9
13
  useEffect(() => {
10
14
  supabase.auth.getSession().then(({ data: { session } }) => {
11
15
  setSession(session)
12
- if (session) onAuth(session)
16
+ if (session) onAuth?.(session)
13
17
  })
14
18
 
15
19
  const { data: subscription } = supabase.auth.onAuthStateChange((_event, session) => {
16
20
  setSession(session)
17
- if (session) onAuth(session)
21
+ if (session) onAuth?.(session)
18
22
  })
19
23
 
20
- return () => subscription.unsubscribe()
21
- }, [])
24
+ return () => subscription?.unsubscribe()
25
+ }, [onAuth])
22
26
 
23
27
  if (!session) {
24
28
  return (
25
29
  <Auth
26
30
  supabaseClient={supabase}
27
31
  appearance={{ theme: ThemeSupa }}
28
- providers={['google', 'github']}
29
- theme="dark"
32
+ providers={providers}
33
+ theme={theme}
30
34
  />
31
35
  )
32
36
  }
33
37
 
34
38
  return <div>✅ Logged in as: {session.user.email}</div>
35
- }
39
+ }
@@ -0,0 +1,12 @@
1
+ import { useAuth } from './useAuth'
2
+
3
+ export default function LogoutButton({ supabaseClient, onLogout }) {
4
+ const { signOut } = useAuth(supabaseClient)
5
+
6
+ const handleLogout = async () => {
7
+ await signOut()
8
+ onLogout?.()
9
+ }
10
+
11
+ return <button onClick={handleLogout}>🚪 Log out</button>
12
+ }
package/src/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export { default as AuthWrapper } from './AuthWrapper';
2
+ export { default as LogoutButton } from './LogoutButton';
3
+ export { useAuth } from './useAuth';
4
+ export { isAdmin } from './utils';
5
+ export { supabase } from './supabaseClient';
6
+ export { signInWithEmail } from './login'; // Add this line
package/src/login.js ADDED
@@ -0,0 +1,21 @@
1
+ import { supabase } from './supabaseClient';
2
+
3
+ /**
4
+ * Logs in a user using email and password.
5
+ * @param {string} email - The user's email address.
6
+ * @param {string} password - The user's password.
7
+ * @returns {Promise<{ session: any, error: any }>} - The session and error (if any).
8
+ */
9
+ export async function signInWithEmail(email, password) {
10
+ try {
11
+ const { data: session, error } = await supabase.auth.signInWithPassword({
12
+ email,
13
+ password,
14
+ });
15
+
16
+ return { session, error };
17
+ } catch (err) {
18
+ console.error('Unexpected error during login:', err);
19
+ return { session: null, error: err };
20
+ }
21
+ }
@@ -1,6 +1,12 @@
1
1
  import { createClient } from '@supabase/supabase-js'
2
2
 
3
- const supabaseUrl = import.meta.env.VITE_SUPABASE_URL
4
- const supabaseKey = import.meta.env.VITE_SUPABASE_ANON_KEY
3
+ let supabase = null
5
4
 
6
- export const supabase = createClient(supabaseUrl, supabaseKey)
5
+ export function initializeSupabase(url, anonKey) {
6
+ if (!url || !anonKey) {
7
+ throw new Error('Supabase URL and Anon Key are required to initialize the client.')
8
+ }
9
+ supabase = createClient(url, anonKey)
10
+ }
11
+
12
+ export { supabase }
package/src/useAuth.js ADDED
@@ -0,0 +1,24 @@
1
+ import { useState, useEffect } from 'react'
2
+
3
+ export function useAuth(supabaseClient) {
4
+ const [session, setSession] = useState(null)
5
+
6
+ useEffect(() => {
7
+ supabaseClient.auth.getSession().then(({ data: { session } }) => {
8
+ setSession(session)
9
+ })
10
+
11
+ const { data: subscription } = supabaseClient.auth.onAuthStateChange((_event, session) => {
12
+ setSession(session)
13
+ })
14
+
15
+ return () => subscription?.unsubscribe()
16
+ }, [supabaseClient])
17
+
18
+ const signOut = async () => {
19
+ await supabaseClient.auth.signOut()
20
+ setSession(null)
21
+ }
22
+
23
+ return { session, signOut }
24
+ }
package/src/utils.js ADDED
@@ -0,0 +1,4 @@
1
+ export function isAdmin(user) {
2
+ return user?.role === 'admin' // You can customize this later!
3
+ }
4
+
package/tsup.config.js ADDED
@@ -0,0 +1,13 @@
1
+ import { defineConfig } from 'tsup'
2
+
3
+ export default defineConfig({
4
+ entry: ['src/index.js'], // Entry point for your package
5
+ format: ['esm', 'cjs'], // Output formats
6
+ clean: true, // Clean the output directory before building
7
+ esbuildOptions(options) {
8
+ options.loader = {
9
+ '.js': 'jsx', // Treat .js files as JSX
10
+ '.jsx': 'jsx', // Treat .jsx files as JSX
11
+ }
12
+ },
13
+ })