hightjs 0.4.0 → 0.5.1
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 +42 -126
- package/dist/bin/hightjs.js +51 -23
- package/dist/builder.js +139 -4
- package/dist/client/DefaultNotFound.d.ts +1 -1
- package/dist/client/DefaultNotFound.js +72 -46
- package/dist/client/client.d.ts +3 -0
- package/dist/{client.js → client/client.js} +4 -4
- package/dist/client/entry.client.js +39 -29
- package/dist/global/global.d.ts +117 -0
- package/dist/{auth/types.js → global/global.js} +0 -1
- package/dist/helpers.js +80 -2
- package/dist/hotReload.js +2 -2
- package/dist/index.js +16 -16
- package/dist/loaders.d.ts +1 -0
- package/dist/loaders.js +46 -0
- package/dist/renderer.js +158 -4
- package/dist/types.d.ts +44 -0
- package/package.json +37 -31
- package/src/bin/hightjs.js +59 -29
- package/src/builder.js +163 -4
- package/src/client/DefaultNotFound.tsx +88 -53
- package/src/{client.ts → client/client.ts} +4 -3
- package/src/client/entry.client.tsx +44 -29
- package/src/global/global.ts +171 -0
- package/src/helpers.ts +91 -2
- package/src/hotReload.ts +2 -2
- package/src/index.ts +2 -0
- package/src/loaders.js +53 -0
- package/src/renderer.tsx +162 -4
- package/src/types.ts +51 -0
- package/.idea/HightJS.iml +0 -9
- package/.idea/copilot.data.migration.agent.xml +0 -6
- package/.idea/copilot.data.migration.ask.xml +0 -6
- package/.idea/copilot.data.migration.ask2agent.xml +0 -6
- package/.idea/copilot.data.migration.edit.xml +0 -6
- package/.idea/copilotDiffState.xml +0 -67
- package/.idea/inspectionProfiles/Project_Default.xml +0 -13
- package/.idea/libraries/test_package.xml +0 -9
- package/.idea/libraries/ts_commonjs_default_export.xml +0 -9
- package/.idea/misc.xml +0 -7
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/auth/client.d.ts +0 -24
- package/dist/auth/client.js +0 -146
- package/dist/auth/components.d.ts +0 -29
- package/dist/auth/components.js +0 -100
- package/dist/auth/core.d.ts +0 -55
- package/dist/auth/core.js +0 -189
- package/dist/auth/index.d.ts +0 -7
- package/dist/auth/index.js +0 -45
- package/dist/auth/jwt.d.ts +0 -41
- package/dist/auth/jwt.js +0 -185
- package/dist/auth/providers/credentials.d.ts +0 -60
- package/dist/auth/providers/credentials.js +0 -97
- package/dist/auth/providers/discord.d.ts +0 -63
- package/dist/auth/providers/discord.js +0 -190
- package/dist/auth/providers/google.d.ts +0 -63
- package/dist/auth/providers/google.js +0 -186
- package/dist/auth/providers/index.d.ts +0 -2
- package/dist/auth/providers/index.js +0 -35
- package/dist/auth/providers.d.ts +0 -3
- package/dist/auth/providers.js +0 -26
- package/dist/auth/react/index.d.ts +0 -6
- package/dist/auth/react/index.js +0 -48
- package/dist/auth/react.d.ts +0 -22
- package/dist/auth/react.js +0 -199
- package/dist/auth/routes.d.ts +0 -16
- package/dist/auth/routes.js +0 -152
- package/dist/auth/types.d.ts +0 -76
- package/dist/client.d.ts +0 -3
- package/docs/README.md +0 -58
- package/docs/arquivos-especiais.md +0 -10
- package/docs/autenticacao.md +0 -212
- package/docs/checklist.md +0 -9
- package/docs/cli.md +0 -72
- package/docs/config.md +0 -216
- package/docs/estrutura.md +0 -20
- package/docs/faq.md +0 -10
- package/docs/hot-reload.md +0 -5
- package/docs/integracoes.md +0 -240
- package/docs/middlewares.md +0 -73
- package/docs/rotas-backend.md +0 -45
- package/docs/rotas-frontend.md +0 -66
- package/docs/seguranca.md +0 -8
- package/docs/websocket.md +0 -45
- package/example/certs/cert.pem +0 -20
- package/example/certs/key.pem +0 -27
- package/example/hightjs.config.ts +0 -87
- package/example/package-lock.json +0 -1174
- package/example/package.json +0 -26
- package/example/postcss.config.js +0 -8
- package/example/src/backend/auth.ts +0 -42
- package/example/src/backend/routes/auth.ts +0 -3
- package/example/src/backend/routes/version.ts +0 -13
- package/example/src/web/components/Home.tsx +0 -140
- package/example/src/web/components/LoginPage.tsx +0 -149
- package/example/src/web/globals.css +0 -5
- package/example/src/web/layout.tsx +0 -100
- package/example/src/web/routes/index.tsx +0 -13
- package/example/src/web/routes/login.tsx +0 -30
- package/example/tailwind.config.js +0 -12
- package/example/tsconfig.json +0 -15
- package/src/auth/client.ts +0 -171
- package/src/auth/components.tsx +0 -125
- package/src/auth/core.ts +0 -215
- package/src/auth/index.ts +0 -25
- package/src/auth/jwt.ts +0 -210
- package/src/auth/providers/credentials.ts +0 -139
- package/src/auth/providers/discord.ts +0 -239
- package/src/auth/providers/google.ts +0 -234
- package/src/auth/providers/index.ts +0 -20
- package/src/auth/providers.ts +0 -20
- package/src/auth/react/index.ts +0 -25
- package/src/auth/react.tsx +0 -234
- package/src/auth/routes.ts +0 -183
- package/src/auth/types.ts +0 -108
- package/tsconfig.json +0 -17
package/src/auth/client.ts
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This file is part of the HightJS Project.
|
|
3
|
-
* Copyright (c) 2025 itsmuzin
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
import type { SignInOptions, SignInResult, Session } from './types';
|
|
18
|
-
// Configuração global do client
|
|
19
|
-
let basePath = '/api/auth';
|
|
20
|
-
|
|
21
|
-
export function setBasePath(path: string) {
|
|
22
|
-
basePath = path;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Função para obter a sessão atual (similar ao NextAuth getSession)
|
|
27
|
-
*/
|
|
28
|
-
export async function getSession(): Promise<Session | null> {
|
|
29
|
-
try {
|
|
30
|
-
const response = await fetch(`${basePath}/session`, {
|
|
31
|
-
credentials: 'include'
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
if (!response.ok) {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const data = await response.json();
|
|
39
|
-
return data.session || null;
|
|
40
|
-
} catch (error) {
|
|
41
|
-
console.error('[hweb-auth] Error fetching session:', error);
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Função para obter token CSRF
|
|
48
|
-
*/
|
|
49
|
-
export async function getCsrfToken(): Promise<string | null> {
|
|
50
|
-
try {
|
|
51
|
-
const response = await fetch(`${basePath}/csrf`, {
|
|
52
|
-
credentials: 'include'
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
if (!response.ok) {
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const data = await response.json();
|
|
60
|
-
return data.csrfToken || null;
|
|
61
|
-
} catch (error) {
|
|
62
|
-
console.error('[hweb-auth] Error fetching CSRF token:', error);
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Função para obter providers disponíveis
|
|
69
|
-
*/
|
|
70
|
-
export async function getProviders(): Promise<any[] | null> {
|
|
71
|
-
try {
|
|
72
|
-
const response = await fetch(`${basePath}/providers`, {
|
|
73
|
-
credentials: 'include'
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
if (!response.ok) {
|
|
77
|
-
return null;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const data = await response.json();
|
|
81
|
-
return data.providers || [];
|
|
82
|
-
} catch (error) {
|
|
83
|
-
console.error('[hweb-auth] Error searching for providers:', error);
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Função para fazer login (similar ao NextAuth signIn)
|
|
90
|
-
*/
|
|
91
|
-
export async function signIn(
|
|
92
|
-
provider: string = 'credentials',
|
|
93
|
-
options: SignInOptions = {}
|
|
94
|
-
): Promise<SignInResult | undefined> {
|
|
95
|
-
try {
|
|
96
|
-
const { redirect = true, callbackUrl, ...credentials } = options;
|
|
97
|
-
|
|
98
|
-
const response = await fetch(`${basePath}/signin`, {
|
|
99
|
-
method: 'POST',
|
|
100
|
-
headers: {
|
|
101
|
-
'Content-Type': 'application/json',
|
|
102
|
-
},
|
|
103
|
-
credentials: 'include',
|
|
104
|
-
body: JSON.stringify({
|
|
105
|
-
provider,
|
|
106
|
-
...credentials
|
|
107
|
-
})
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
const data = await response.json();
|
|
111
|
-
|
|
112
|
-
if (response.ok && data.success) {
|
|
113
|
-
// Se é OAuth, redireciona para URL fornecida
|
|
114
|
-
if (data.type === 'oauth' && data.redirectUrl) {
|
|
115
|
-
if (redirect && typeof window !== 'undefined') {
|
|
116
|
-
window.location.href = data.redirectUrl;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return {
|
|
120
|
-
ok: true,
|
|
121
|
-
status: 200,
|
|
122
|
-
url: data.redirectUrl
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Se é sessão (credentials), redireciona para callbackUrl
|
|
127
|
-
if (data.type === 'session') {
|
|
128
|
-
if (redirect && typeof window !== 'undefined') {
|
|
129
|
-
window.location.href = callbackUrl || '/';
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
ok: true,
|
|
134
|
-
status: 200,
|
|
135
|
-
url: callbackUrl || '/'
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
} else {
|
|
139
|
-
return {
|
|
140
|
-
error: data.error || 'Authentication failed',
|
|
141
|
-
status: response.status,
|
|
142
|
-
ok: false
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
} catch (error) {
|
|
146
|
-
console.error('[hweb-auth] Error on signIn:', error);
|
|
147
|
-
return {
|
|
148
|
-
error: 'Network error',
|
|
149
|
-
status: 500,
|
|
150
|
-
ok: false
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Função para fazer logout (similar ao NextAuth signOut)
|
|
157
|
-
*/
|
|
158
|
-
export async function signOut(options: { callbackUrl?: string } = {}): Promise<void> {
|
|
159
|
-
try {
|
|
160
|
-
await fetch(`${basePath}/signout`, {
|
|
161
|
-
method: 'POST',
|
|
162
|
-
credentials: 'include'
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
if (typeof window !== 'undefined') {
|
|
166
|
-
window.location.href = options.callbackUrl || '/';
|
|
167
|
-
}
|
|
168
|
-
} catch (error) {
|
|
169
|
-
console.error('[hweb-auth] Error on signOut:', error);
|
|
170
|
-
}
|
|
171
|
-
}
|
package/src/auth/components.tsx
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This file is part of the HightJS Project.
|
|
3
|
-
* Copyright (c) 2025 itsmuzin
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
import React, { ReactNode } from 'react';
|
|
18
|
-
import { useAuth } from './react';
|
|
19
|
-
import { router } from '../client/clientRouter';
|
|
20
|
-
interface ProtectedRouteProps {
|
|
21
|
-
children: ReactNode;
|
|
22
|
-
fallback?: ReactNode;
|
|
23
|
-
redirectTo?: string;
|
|
24
|
-
requireAuth?: boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Componente para proteger rotas que requerem autenticação
|
|
29
|
-
*/
|
|
30
|
-
export function ProtectedRoute({
|
|
31
|
-
children,
|
|
32
|
-
fallback,
|
|
33
|
-
redirectTo = '/auth/signin',
|
|
34
|
-
requireAuth = true
|
|
35
|
-
}: ProtectedRouteProps) {
|
|
36
|
-
const { isAuthenticated, isLoading } = useAuth();
|
|
37
|
-
|
|
38
|
-
// Ainda carregando
|
|
39
|
-
if (isLoading) {
|
|
40
|
-
return fallback || <div>Loading...</div>;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Requer auth mas não está autenticado
|
|
44
|
-
if (requireAuth && !isAuthenticated) {
|
|
45
|
-
if (typeof window !== 'undefined' && redirectTo) {
|
|
46
|
-
window.location.href = redirectTo;
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
return fallback || <div>Unauthorized</div>;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Não requer auth mas está autenticado (ex: página de login)
|
|
53
|
-
if (!requireAuth && isAuthenticated && redirectTo) {
|
|
54
|
-
if (typeof window !== 'undefined') {
|
|
55
|
-
window.location.href = redirectTo;
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return <>{children}</>;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
interface GuardProps {
|
|
64
|
-
children: ReactNode;
|
|
65
|
-
fallback?: ReactNode;
|
|
66
|
-
redirectTo?: string;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Guard simples que só renderiza children se estiver autenticado
|
|
71
|
-
*/
|
|
72
|
-
export function AuthGuard({ children, fallback, redirectTo }: GuardProps) {
|
|
73
|
-
const { isAuthenticated, isLoading } = useAuth();
|
|
74
|
-
|
|
75
|
-
if(redirectTo && !isLoading && !isAuthenticated) {
|
|
76
|
-
router.push(redirectTo);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (isLoading) {
|
|
80
|
-
return fallback || <div></div>;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (!isAuthenticated) {
|
|
84
|
-
return fallback || null;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return <>{children}</>;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Componente para mostrar conteúdo apenas para usuários não autenticados
|
|
92
|
-
*/
|
|
93
|
-
export function GuestOnly({ children, fallback, redirectTo }: GuardProps) {
|
|
94
|
-
const { isAuthenticated, isLoading } = useAuth();
|
|
95
|
-
|
|
96
|
-
if(redirectTo && !isLoading && isAuthenticated) {
|
|
97
|
-
router.push(redirectTo);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (isLoading || isAuthenticated) {
|
|
101
|
-
return fallback || <div></div>;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return <>{children}</>;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Hook para redirecionar baseado no status de autenticação
|
|
109
|
-
*/
|
|
110
|
-
export function useAuthRedirect(
|
|
111
|
-
authenticatedRedirect?: string,
|
|
112
|
-
unauthenticatedRedirect?: string
|
|
113
|
-
) {
|
|
114
|
-
const { isAuthenticated, isLoading } = useAuth();
|
|
115
|
-
|
|
116
|
-
React.useEffect(() => {
|
|
117
|
-
if (isLoading) return;
|
|
118
|
-
|
|
119
|
-
if (isAuthenticated && authenticatedRedirect) {
|
|
120
|
-
window.location.href = authenticatedRedirect;
|
|
121
|
-
} else if (!isAuthenticated && unauthenticatedRedirect) {
|
|
122
|
-
window.location.href = unauthenticatedRedirect;
|
|
123
|
-
}
|
|
124
|
-
}, [isAuthenticated, isLoading, authenticatedRedirect, unauthenticatedRedirect]);
|
|
125
|
-
}
|
package/src/auth/core.ts
DELETED
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This file is part of the HightJS Project.
|
|
3
|
-
* Copyright (c) 2025 itsmuzin
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
import { HightJSRequest, HightJSResponse } from '../api/http';
|
|
18
|
-
import type { AuthConfig, AuthProviderClass, User, Session } from './types';
|
|
19
|
-
import { SessionManager } from './jwt';
|
|
20
|
-
|
|
21
|
-
export class HWebAuth {
|
|
22
|
-
private config: AuthConfig;
|
|
23
|
-
private sessionManager: SessionManager;
|
|
24
|
-
|
|
25
|
-
constructor(config: AuthConfig) {
|
|
26
|
-
this.config = {
|
|
27
|
-
session: { strategy: 'jwt', maxAge: 86400, ...config.session },
|
|
28
|
-
pages: { signIn: '/auth/signin', signOut: '/auth/signout', ...config.pages },
|
|
29
|
-
...config
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
this.sessionManager = new SessionManager(
|
|
33
|
-
config.secret,
|
|
34
|
-
this.config.session?.maxAge || 86400
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Middleware para adicionar autenticação às rotas
|
|
40
|
-
*/
|
|
41
|
-
private async middleware(req: HightJSRequest): Promise<{ session: Session | null; user: User | null }> {
|
|
42
|
-
const token = this.getTokenFromRequest(req);
|
|
43
|
-
|
|
44
|
-
if (!token) {
|
|
45
|
-
return { session: null, user: null };
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const session = this.sessionManager.verifySession(token);
|
|
49
|
-
return {
|
|
50
|
-
session,
|
|
51
|
-
user: session?.user || null
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Autentica um usuário usando um provider específico
|
|
57
|
-
*/
|
|
58
|
-
async signIn(providerId: string, credentials: Record<string, string>): Promise<{ session: Session; token: string } | { redirectUrl: string } | null> {
|
|
59
|
-
const provider = this.config.providers.find(p => p.id === providerId);
|
|
60
|
-
if (!provider) {
|
|
61
|
-
console.error(`[hweb-auth] Provider not found: ${providerId}`);
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
// Usa o método handleSignIn do provider
|
|
67
|
-
const result = await provider.handleSignIn(credentials);
|
|
68
|
-
|
|
69
|
-
if (!result) return null;
|
|
70
|
-
|
|
71
|
-
// Se resultado é string, é URL de redirecionamento OAuth
|
|
72
|
-
if (typeof result === 'string') {
|
|
73
|
-
return { redirectUrl: result };
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Se resultado é User, cria sessão
|
|
77
|
-
const user = result as User;
|
|
78
|
-
|
|
79
|
-
// Callback de signIn se definido
|
|
80
|
-
if (this.config.callbacks?.signIn) {
|
|
81
|
-
const allowed = await this.config.callbacks.signIn(user, { provider: providerId }, {});
|
|
82
|
-
if (!allowed) return null;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const sessionResult = this.sessionManager.createSession(user);
|
|
86
|
-
|
|
87
|
-
// Callback de sessão se definido
|
|
88
|
-
if (this.config.callbacks?.session) {
|
|
89
|
-
sessionResult.session = await this.config.callbacks.session({session: sessionResult.session, user, provider: providerId});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return sessionResult;
|
|
93
|
-
} catch (error) {
|
|
94
|
-
console.error(`[hweb-auth] Error signing in with provider ${providerId}:`, error);
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Faz logout do usuário
|
|
101
|
-
*/
|
|
102
|
-
async signOut(req: HightJSRequest): Promise<HightJSResponse> {
|
|
103
|
-
// Busca a sessão atual para saber qual provider usar
|
|
104
|
-
const { session } = await this.middleware(req);
|
|
105
|
-
|
|
106
|
-
if (session?.user?.provider) {
|
|
107
|
-
const provider = this.config.providers.find(p => p.id === session.user.provider);
|
|
108
|
-
if (provider && provider.handleSignOut) {
|
|
109
|
-
try {
|
|
110
|
-
await provider.handleSignOut();
|
|
111
|
-
} catch (error) {
|
|
112
|
-
console.error(`[hweb-auth] Signout error on provider ${provider.id}:`, error);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return HightJSResponse
|
|
118
|
-
.json({ success: true })
|
|
119
|
-
.clearCookie('hweb-auth-token', {
|
|
120
|
-
path: '/',
|
|
121
|
-
httpOnly: true,
|
|
122
|
-
secure: this.config.secureCookies || false,
|
|
123
|
-
sameSite: 'strict'
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Obtém a sessão atual
|
|
129
|
-
*/
|
|
130
|
-
async getSession(req: HightJSRequest): Promise<Session | null> {
|
|
131
|
-
const { session } = await this.middleware(req);
|
|
132
|
-
return session;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Verifica se o usuário está autenticado
|
|
137
|
-
*/
|
|
138
|
-
async isAuthenticated(req: HightJSRequest): Promise<boolean> {
|
|
139
|
-
const session = await this.getSession(req);
|
|
140
|
-
return session !== null;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Retorna todos os providers disponíveis (dados públicos)
|
|
145
|
-
*/
|
|
146
|
-
getProviders(): any[] {
|
|
147
|
-
return this.config.providers.map(provider => ({
|
|
148
|
-
id: provider.id,
|
|
149
|
-
name: provider.name,
|
|
150
|
-
type: provider.type,
|
|
151
|
-
config: provider.getConfig ? provider.getConfig() : {}
|
|
152
|
-
}));
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Busca um provider específico
|
|
157
|
-
*/
|
|
158
|
-
getProvider(id: string): AuthProviderClass | null {
|
|
159
|
-
return this.config.providers.find(p => p.id === id) || null;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Retorna todas as rotas adicionais dos providers
|
|
164
|
-
*/
|
|
165
|
-
getAllAdditionalRoutes(): Array<{ provider: string; route: any }> {
|
|
166
|
-
const routes: Array<{ provider: string; route: any }> = [];
|
|
167
|
-
|
|
168
|
-
for (const provider of this.config.providers) {
|
|
169
|
-
if (provider.additionalRoutes) {
|
|
170
|
-
for (const route of provider.additionalRoutes) {
|
|
171
|
-
routes.push({ provider: provider.id, route });
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return routes;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Cria resposta com cookie de autenticação - Secure implementation
|
|
181
|
-
*/
|
|
182
|
-
createAuthResponse(token: string, data: any): HightJSResponse {
|
|
183
|
-
return HightJSResponse
|
|
184
|
-
.json(data)
|
|
185
|
-
.cookie('hweb-auth-token', token, {
|
|
186
|
-
httpOnly: true,
|
|
187
|
-
secure: this.config.secureCookies || false, // Always secure, even in development
|
|
188
|
-
sameSite: 'strict', // Prevent CSRF attacks
|
|
189
|
-
maxAge: (this.config.session?.maxAge || 86400) * 1000,
|
|
190
|
-
path: '/',
|
|
191
|
-
domain: undefined // Let browser set automatically for security
|
|
192
|
-
})
|
|
193
|
-
.header('X-Content-Type-Options', 'nosniff')
|
|
194
|
-
.header('X-Frame-Options', 'DENY')
|
|
195
|
-
.header('X-XSS-Protection', '1; mode=block')
|
|
196
|
-
.header('Referrer-Policy', 'strict-origin-when-cross-origin');
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Extrai token da requisição (cookie ou header)
|
|
201
|
-
*/
|
|
202
|
-
private getTokenFromRequest(req: HightJSRequest): string | null {
|
|
203
|
-
// Primeiro tenta pegar do cookie
|
|
204
|
-
const cookieToken = req.cookie('hweb-auth-token');
|
|
205
|
-
if (cookieToken) return cookieToken;
|
|
206
|
-
|
|
207
|
-
// Depois tenta do header Authorization
|
|
208
|
-
const authHeader = req.header('authorization');
|
|
209
|
-
if (authHeader && typeof authHeader === 'string' && authHeader.startsWith('Bearer ')) {
|
|
210
|
-
return authHeader.substring(7);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return null;
|
|
214
|
-
}
|
|
215
|
-
}
|
package/src/auth/index.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This file is part of the HightJS Project.
|
|
3
|
-
* Copyright (c) 2025 itsmuzin
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
// Exportações principais do sistema de autenticação
|
|
18
|
-
export * from './types';
|
|
19
|
-
export * from './providers';
|
|
20
|
-
export * from './core';
|
|
21
|
-
export * from './routes';
|
|
22
|
-
export * from './jwt';
|
|
23
|
-
|
|
24
|
-
export { CredentialsProvider, DiscordProvider, GoogleProvider } from './providers';
|
|
25
|
-
export { createAuthRoutes } from './routes';
|