delta-auth-next 0.1.28 → 0.1.30
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/edge/middleware-helper.d.ts +11 -0
- package/dist/edge/middleware-helper.d.ts.map +1 -0
- package/dist/edge/middleware-helper.js +10 -0
- package/dist/edge/middleware-helper.js.map +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/package.json +39 -8
- package/src/DeltaProvider.tsx +0 -129
- package/src/alert.tsx +0 -30
- package/src/client/authClient.ts +0 -13
- package/src/context.ts +0 -21
- package/src/hooks.ts +0 -7
- package/src/index.ts +0 -7
- package/src/middleware-helper.ts +0 -22
- package/src/types.ts +0 -25
- package/src/urls.ts +0 -12
- package/tsconfig.json +0 -20
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface DeltaAuthConfig {
|
|
2
|
+
publishableKey?: string;
|
|
3
|
+
autoRefresh?: boolean;
|
|
4
|
+
refreshInterval?: number;
|
|
5
|
+
showAlerts?: boolean;
|
|
6
|
+
backendUrl?: string;
|
|
7
|
+
authDomain?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function GetsigninUrl(pathname: string, config: DeltaAuthConfig, origin: string): string;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=middleware-helper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware-helper.d.ts","sourceRoot":"","sources":["../../src/edge/middleware-helper.ts"],"names":[],"mappings":"AACA,UAAU,eAAe;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAGD,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAC,MAAM,EAAC,MAAM,UAUnF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function GetsigninUrl(pathname, config, origin) {
|
|
2
|
+
if (pathname === '/api/signin') {
|
|
3
|
+
const signinUrl = `${config.authDomain}/signin?client_id=${config.publishableKey}&redirect_uri=${encodeURIComponent(origin)}`;
|
|
4
|
+
return signinUrl;
|
|
5
|
+
}
|
|
6
|
+
else {
|
|
7
|
+
return pathname;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=middleware-helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware-helper.js","sourceRoot":"","sources":["../../src/edge/middleware-helper.ts"],"names":[],"mappings":"AAWA,MAAM,UAAU,YAAY,CAAC,QAAgB,EAAE,MAAuB,EAAC,MAAa;IAClF,IAAI,QAAQ,KAAK,aAAa,EAAE,CAAC;QAC/B,MAAM,SAAS,GAAW,GAAG,MAAM,CAAC,UAAU,qBAC5C,MAAM,CAAC,cACT,iBAAiB,kBAAkB,CAAC,MAAM,CAAC,EAAE,CAAC;QAC9C,OAAO,SAAS,CAAA;IAClB,CAAC;SACG,CAAC;QACH,OAAO,QAAQ,CAAA;IACjB,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,31 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "delta-auth-next",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.30",
|
|
4
|
+
"description": "A lightweight authentication helper for Next.js that provides React context providers and Edge-safe middleware utilities for handling sign-in redirects and session management.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Samuel Onwodi",
|
|
4
7
|
"main": "dist/index.js",
|
|
5
8
|
"types": "dist/index.d.ts",
|
|
6
|
-
"
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"./react": {
|
|
15
|
+
"import": "./dist/react/index.js",
|
|
16
|
+
"types": "./dist/react/index.d.ts"
|
|
17
|
+
},
|
|
18
|
+
"./middleware": {
|
|
19
|
+
"import": "./dist/edge/middleware-helper.js",
|
|
20
|
+
"types": "./dist/edge/middleware-helper.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"typesVersions": {
|
|
24
|
+
"*": {
|
|
25
|
+
"middleware": ["dist/edge/middleware-helper.d.ts"],
|
|
26
|
+
"react": ["dist/react/index.d.ts"],
|
|
27
|
+
"*": ["dist/index.d.ts"]
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
7
33
|
"scripts": {
|
|
8
|
-
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
34
|
"build": "tsc",
|
|
10
|
-
"watch": "tsc --watch"
|
|
35
|
+
"watch": "tsc --watch",
|
|
36
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
37
|
},
|
|
12
|
-
"author": "Samuel Onwodi",
|
|
13
38
|
"keywords": [
|
|
14
39
|
"react",
|
|
15
40
|
"auth",
|
|
16
|
-
"
|
|
41
|
+
"nextjs",
|
|
42
|
+
"middleware",
|
|
43
|
+
"edge"
|
|
17
44
|
],
|
|
18
45
|
"packageManager": "pnpm@10.28.2",
|
|
19
46
|
"dependencies": {
|
|
20
47
|
"axios": "^1.13.4"
|
|
21
48
|
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"react": "^18",
|
|
51
|
+
"react-dom": "^18"
|
|
52
|
+
},
|
|
22
53
|
"devDependencies": {
|
|
23
54
|
"@types/node": "^25.2.0",
|
|
24
55
|
"@types/react": "^18.3.27",
|
|
25
56
|
"@types/react-dom": "^18.3.7",
|
|
26
57
|
"react": "^18.3.1",
|
|
27
58
|
"react-dom": "^18.3.1",
|
|
28
|
-
"
|
|
29
|
-
"
|
|
59
|
+
"typescript": "^5.9.3",
|
|
60
|
+
"tsup": "^8.5.1"
|
|
30
61
|
}
|
|
31
62
|
}
|
package/src/DeltaProvider.tsx
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
import React, { useState, useEffect, useCallback } from 'react';
|
|
3
|
-
import { AuthContext } from './context';
|
|
4
|
-
import { createAuthClient } from './client/authClient';
|
|
5
|
-
import { DeltaAuthConfig, Session, User } from './types';
|
|
6
|
-
import { useSimpleToast } from './alert';
|
|
7
|
-
|
|
8
|
-
interface Props {
|
|
9
|
-
config: DeltaAuthConfig;
|
|
10
|
-
children: React.ReactNode;
|
|
11
|
-
}
|
|
12
|
-
// export Appconfigs = ()=> {
|
|
13
|
-
// return
|
|
14
|
-
// }
|
|
15
|
-
export function DeltaAuthProvider({ config, children }: Props) {
|
|
16
|
-
const [session, setSession] = useState<Session | null>(null);
|
|
17
|
-
const [loading, setLoading] = useState(true);
|
|
18
|
-
const [enabled, setEnabled] = useState(true); // disables provider if misconfigured
|
|
19
|
-
const { show, Toast } = useSimpleToast();
|
|
20
|
-
|
|
21
|
-
// 🔹 Disable if no publishable key
|
|
22
|
-
useEffect(() => {
|
|
23
|
-
if (!config?.publishableKey) {
|
|
24
|
-
show('DeltaAuth: publishableKey is required');
|
|
25
|
-
setEnabled(false);
|
|
26
|
-
}
|
|
27
|
-
if (!config?.authDomain) {
|
|
28
|
-
show('DeltaAuth: authDomain is required');
|
|
29
|
-
setEnabled(false);
|
|
30
|
-
}
|
|
31
|
-
}, [config?.publishableKey]);
|
|
32
|
-
|
|
33
|
-
// 🔹 Create client only if enabled
|
|
34
|
-
const client = createAuthClient(
|
|
35
|
-
config.backendUrl ? { backendUrl: config.backendUrl } : {}
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
// 🔹 Fetch current session
|
|
39
|
-
const fetchSession = useCallback(async () => {
|
|
40
|
-
if (!enabled || !client) return;
|
|
41
|
-
|
|
42
|
-
try {
|
|
43
|
-
const res = await client.get('/auth/me'); // cookie-based
|
|
44
|
-
if (res.data?.data?.user) {
|
|
45
|
-
setSession(res.data.data);
|
|
46
|
-
} else {
|
|
47
|
-
setSession(null);
|
|
48
|
-
}
|
|
49
|
-
} catch (err: any) {
|
|
50
|
-
setSession(null);
|
|
51
|
-
if (config.showAlerts ?? true) {
|
|
52
|
-
show(err.response?.data?.error ?? 'Auth failed');
|
|
53
|
-
}
|
|
54
|
-
} finally {
|
|
55
|
-
setLoading(false);
|
|
56
|
-
}
|
|
57
|
-
}, [client, enabled, config.showAlerts, show]);
|
|
58
|
-
|
|
59
|
-
// 🔹 Fetch detailed user info
|
|
60
|
-
const fetchUserDetails = useCallback(async () => {
|
|
61
|
-
if (!session?.user || !client) return null;
|
|
62
|
-
|
|
63
|
-
try {
|
|
64
|
-
const res = await client.get(`/users/${session.user.id}`);
|
|
65
|
-
setSession((prev) => (prev ? { ...prev, user: res.data } : null));
|
|
66
|
-
return res.data as User;
|
|
67
|
-
} catch {
|
|
68
|
-
return session.user; // fallback
|
|
69
|
-
}
|
|
70
|
-
}, [client, session]);
|
|
71
|
-
|
|
72
|
-
// 🔹 Environment mismatch warnings
|
|
73
|
-
useEffect(() => {
|
|
74
|
-
if (!enabled) return;
|
|
75
|
-
|
|
76
|
-
const hostname =
|
|
77
|
-
typeof window !== 'undefined' ? window.location.hostname : '';
|
|
78
|
-
const protocol =
|
|
79
|
-
typeof window !== 'undefined' ? window.location.protocol : '';
|
|
80
|
-
|
|
81
|
-
const isLocalhost =
|
|
82
|
-
hostname.includes('localhost') || hostname.includes('127.0.0.1');
|
|
83
|
-
const isProd = protocol === 'https:';
|
|
84
|
-
|
|
85
|
-
if (isLocalhost && config.publishableKey?.startsWith('pk_live')) {
|
|
86
|
-
show(
|
|
87
|
-
'You are using a live key on localhost. Use a test key for development.'
|
|
88
|
-
);
|
|
89
|
-
setEnabled(false);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (isProd && config.publishableKey?.startsWith('pk_test')) {
|
|
93
|
-
show('You are using a test key in production. Use a live key.');
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (enabled) fetchSession();
|
|
97
|
-
}, [config.publishableKey, fetchSession, enabled, show]);
|
|
98
|
-
|
|
99
|
-
// 🔹 Auto refresh session
|
|
100
|
-
useEffect(() => {
|
|
101
|
-
if (!enabled || !config.autoRefresh) return;
|
|
102
|
-
|
|
103
|
-
const interval = setInterval(
|
|
104
|
-
fetchSession,
|
|
105
|
-
config.refreshInterval ?? 5 * 60 * 1000
|
|
106
|
-
);
|
|
107
|
-
return () => clearInterval(interval);
|
|
108
|
-
}, [fetchSession, config.autoRefresh, config.refreshInterval, enabled]);
|
|
109
|
-
|
|
110
|
-
if (!enabled) return <>{children}</>; // render children without auth if disabled
|
|
111
|
-
|
|
112
|
-
return (
|
|
113
|
-
<>
|
|
114
|
-
<AuthContext.Provider
|
|
115
|
-
value={{
|
|
116
|
-
session,
|
|
117
|
-
user: session?.user ?? null,
|
|
118
|
-
loading,
|
|
119
|
-
isAuthenticated: !!session,
|
|
120
|
-
client,
|
|
121
|
-
fetchUser: fetchUserDetails,
|
|
122
|
-
}}
|
|
123
|
-
>
|
|
124
|
-
{children}
|
|
125
|
-
</AuthContext.Provider>
|
|
126
|
-
<Toast /> {/* 🔹 render the toast container */}
|
|
127
|
-
</>
|
|
128
|
-
);
|
|
129
|
-
}
|
package/src/alert.tsx
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { useState, useEffect } from 'react';
|
|
2
|
-
|
|
3
|
-
export function useSimpleToast() {
|
|
4
|
-
const [message, setMessage] = useState<string | null>(null);
|
|
5
|
-
|
|
6
|
-
const show = (msg: string, duration = 3000) => {
|
|
7
|
-
setMessage(msg);
|
|
8
|
-
setTimeout(() => setMessage(null), duration);
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const Toast = () =>
|
|
12
|
-
message ? (
|
|
13
|
-
<div
|
|
14
|
-
style={{
|
|
15
|
-
position: 'fixed',
|
|
16
|
-
top: 20,
|
|
17
|
-
right: 20,
|
|
18
|
-
background: 'rgba(0,0,0,0.85)',
|
|
19
|
-
color: '#fff',
|
|
20
|
-
padding: '10px 20px',
|
|
21
|
-
borderRadius: 6,
|
|
22
|
-
zIndex: 9999,
|
|
23
|
-
}}
|
|
24
|
-
>
|
|
25
|
-
{message}
|
|
26
|
-
</div>
|
|
27
|
-
) : null;
|
|
28
|
-
|
|
29
|
-
return { show, Toast };
|
|
30
|
-
}
|
package/src/client/authClient.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// src/client/authClient.ts
|
|
2
|
-
import axios from 'axios';
|
|
3
|
-
|
|
4
|
-
export function createAuthClient(config: { backendUrl?: string }) {
|
|
5
|
-
const baseURL = config.backendUrl ?? '/api';
|
|
6
|
-
|
|
7
|
-
const client = axios.create({
|
|
8
|
-
baseURL,
|
|
9
|
-
withCredentials: true, // send cookies
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
return client;
|
|
13
|
-
}
|
package/src/context.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// src/context.ts
|
|
2
|
-
import { createContext } from 'react';
|
|
3
|
-
import { Session, User } from './types';
|
|
4
|
-
|
|
5
|
-
export interface AuthContextValue {
|
|
6
|
-
session: Session | null;
|
|
7
|
-
user: User | null;
|
|
8
|
-
loading: boolean;
|
|
9
|
-
isAuthenticated: boolean;
|
|
10
|
-
client: ReturnType<typeof import('./client/authClient').createAuthClient>;
|
|
11
|
-
fetchUser: () => Promise<User | null>;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const AuthContext = createContext<AuthContextValue>({
|
|
15
|
-
session: null,
|
|
16
|
-
user: null,
|
|
17
|
-
loading: true,
|
|
18
|
-
isAuthenticated: false,
|
|
19
|
-
client: {} as any,
|
|
20
|
-
fetchUser: async () => null,
|
|
21
|
-
});
|
package/src/hooks.ts
DELETED
package/src/index.ts
DELETED
package/src/middleware-helper.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
// src/middleware-helper.ts
|
|
2
|
-
interface DeltaAuthConfig {
|
|
3
|
-
publishableKey?: string;
|
|
4
|
-
autoRefresh?: boolean;
|
|
5
|
-
refreshInterval?: number; // ms
|
|
6
|
-
showAlerts?: boolean; // toggle alerts
|
|
7
|
-
backendUrl?: string;
|
|
8
|
-
authDomain?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export function GetsigninUrl(pathname: string, config: DeltaAuthConfig,origin:string) {
|
|
13
|
-
if (pathname === '/api/signin') {
|
|
14
|
-
const signinUrl :string = `${config.authDomain}/signin?client_id=${
|
|
15
|
-
config.publishableKey
|
|
16
|
-
}&redirect_uri=${encodeURIComponent(origin)}`;
|
|
17
|
-
return signinUrl
|
|
18
|
-
}
|
|
19
|
-
else{
|
|
20
|
-
return pathname
|
|
21
|
-
}
|
|
22
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// src/types.ts
|
|
2
|
-
export interface DeltaAuthConfig {
|
|
3
|
-
publishableKey: string;
|
|
4
|
-
autoRefresh?: boolean;
|
|
5
|
-
refreshInterval?: number; // ms
|
|
6
|
-
showAlerts?: boolean; // toggle alerts
|
|
7
|
-
backendUrl?: string;
|
|
8
|
-
authDomain?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface User {
|
|
12
|
-
id: string;
|
|
13
|
-
email: string;
|
|
14
|
-
first_name: string;
|
|
15
|
-
last_name: string;
|
|
16
|
-
role: string;
|
|
17
|
-
public_metadata?: Record<string, any>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface Session {
|
|
21
|
-
access_token: string;
|
|
22
|
-
refresh_token: string;
|
|
23
|
-
expires_at: string;
|
|
24
|
-
user: User;
|
|
25
|
-
}
|
package/src/urls.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// src/urls.ts
|
|
2
|
-
|
|
3
|
-
interface DeltaAuthConfig {
|
|
4
|
-
publishableKey: string;
|
|
5
|
-
authDomain: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export default function getSigninUrl(config: DeltaAuthConfig, redirectUri: string) {
|
|
9
|
-
return `${config.authDomain}/signin?client_id=${
|
|
10
|
-
config.publishableKey
|
|
11
|
-
}&redirect_uri=${encodeURIComponent(redirectUri)}`;
|
|
12
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"rootDir": "src",
|
|
4
|
-
"outDir": "dist",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"declarationMap": true,
|
|
7
|
-
"sourceMap": true,
|
|
8
|
-
"module": "ESNext",
|
|
9
|
-
"target": "ES2022",
|
|
10
|
-
"moduleResolution": "node",
|
|
11
|
-
"jsx": "react-jsx",
|
|
12
|
-
"strict": true,
|
|
13
|
-
"noImplicitAny": true,
|
|
14
|
-
"skipLibCheck": true,
|
|
15
|
-
"esModuleInterop": true,
|
|
16
|
-
"forceConsistentCasingInFileNames": true
|
|
17
|
-
},
|
|
18
|
-
"include": ["src"],
|
|
19
|
-
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
|
|
20
|
-
}
|