@vatts/auth 1.1.4-alpha.9 → 1.2.0-alpha.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/dist/react/index.d.ts +4 -5
- package/dist/react/index.js +5 -7
- package/dist/{react.d.ts → react/react.d.ts} +1 -1
- package/dist/vue/component.vue +25 -0
- package/dist/vue/guards.d.ts +72 -0
- package/dist/vue/guards.js +138 -0
- package/dist/vue/index.d.ts +6 -0
- package/dist/vue/index.js +48 -0
- package/dist/vue/session.d.ts +36 -0
- package/dist/vue/session.js +212 -0
- package/package.json +12 -4
- /package/dist/{components.d.ts → react/components.d.ts} +0 -0
- /package/dist/{components.js → react/components.js} +0 -0
- /package/dist/{react.js → react/react.js} +0 -0
package/dist/react/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
export * from '
|
|
1
|
+
export * from './react';
|
|
2
2
|
export * from '../client';
|
|
3
|
-
export * from '
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export { AuthGuard, GuestOnly } from '../components';
|
|
3
|
+
export * from './components';
|
|
4
|
+
export { useSession, useAuth, SessionProvider } from './react';
|
|
5
|
+
export { AuthGuard, GuestOnly } from './components';
|
package/dist/react/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.GuestOnly = exports.AuthGuard = exports.SessionProvider = exports.useAuth = exports.useSession =
|
|
17
|
+
exports.GuestOnly = exports.AuthGuard = exports.SessionProvider = exports.useAuth = exports.useSession = void 0;
|
|
18
18
|
/*
|
|
19
19
|
* This file is part of the Vatts.js Project.
|
|
20
20
|
* Copyright (c) 2026 itsmuzin
|
|
@@ -32,16 +32,14 @@ exports.GuestOnly = exports.AuthGuard = exports.SessionProvider = exports.useAut
|
|
|
32
32
|
* limitations under the License.
|
|
33
33
|
*/
|
|
34
34
|
// Exportações do frontend
|
|
35
|
-
__exportStar(require("
|
|
35
|
+
__exportStar(require("./react"), exports);
|
|
36
36
|
__exportStar(require("../client"), exports);
|
|
37
|
-
__exportStar(require("
|
|
37
|
+
__exportStar(require("./components"), exports);
|
|
38
38
|
// Re-exports das funções mais usadas para conveniência
|
|
39
|
-
var
|
|
40
|
-
Object.defineProperty(exports, "getSession", { enumerable: true, get: function () { return client_1.getSession; } });
|
|
41
|
-
var react_1 = require("../react");
|
|
39
|
+
var react_1 = require("./react");
|
|
42
40
|
Object.defineProperty(exports, "useSession", { enumerable: true, get: function () { return react_1.useSession; } });
|
|
43
41
|
Object.defineProperty(exports, "useAuth", { enumerable: true, get: function () { return react_1.useAuth; } });
|
|
44
42
|
Object.defineProperty(exports, "SessionProvider", { enumerable: true, get: function () { return react_1.SessionProvider; } });
|
|
45
|
-
var components_1 = require("
|
|
43
|
+
var components_1 = require("./components");
|
|
46
44
|
Object.defineProperty(exports, "AuthGuard", { enumerable: true, get: function () { return components_1.AuthGuard; } });
|
|
47
45
|
Object.defineProperty(exports, "GuestOnly", { enumerable: true, get: function () { return components_1.GuestOnly; } });
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* This file is part of the Vatts.js Project.
|
|
4
|
+
* Copyright (c) 2026 itsmuzin
|
|
5
|
+
*/
|
|
6
|
+
import { useSessionProviderLogic } from './session';
|
|
7
|
+
|
|
8
|
+
// Definição das props com valores padrão
|
|
9
|
+
const props = withDefaults(defineProps<{
|
|
10
|
+
basePath?: string;
|
|
11
|
+
refetchInterval?: number;
|
|
12
|
+
refetchOnWindowFocus?: boolean;
|
|
13
|
+
}>(), {
|
|
14
|
+
basePath: '/api/auth',
|
|
15
|
+
refetchInterval: 0,
|
|
16
|
+
refetchOnWindowFocus: true
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// Inicializa a lógica do provider passando as props reativas
|
|
20
|
+
useSessionProviderLogic(props);
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<template>
|
|
24
|
+
<slot />
|
|
25
|
+
</template>
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { type PropType, type VNode } from 'vue';
|
|
2
|
+
/**
|
|
3
|
+
* Guard simples que só renderiza o slot default se estiver autenticado.
|
|
4
|
+
* * Uso:
|
|
5
|
+
* <AuthGuard redirectTo="/login">
|
|
6
|
+
* <ConteudoProtegido />
|
|
7
|
+
* </AuthGuard>
|
|
8
|
+
*/
|
|
9
|
+
export declare const AuthGuard: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
10
|
+
fallback: {
|
|
11
|
+
type: PropType<VNode | string | object>;
|
|
12
|
+
default: null;
|
|
13
|
+
};
|
|
14
|
+
redirectTo: {
|
|
15
|
+
type: StringConstructor;
|
|
16
|
+
default: undefined;
|
|
17
|
+
};
|
|
18
|
+
}>, () => VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
19
|
+
[key: string]: any;
|
|
20
|
+
}> | VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
21
|
+
[key: string]: any;
|
|
22
|
+
}>[] | null, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
23
|
+
fallback: {
|
|
24
|
+
type: PropType<VNode | string | object>;
|
|
25
|
+
default: null;
|
|
26
|
+
};
|
|
27
|
+
redirectTo: {
|
|
28
|
+
type: StringConstructor;
|
|
29
|
+
default: undefined;
|
|
30
|
+
};
|
|
31
|
+
}>> & Readonly<{}>, {
|
|
32
|
+
fallback: string | object | VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
33
|
+
[key: string]: any;
|
|
34
|
+
}>;
|
|
35
|
+
redirectTo: string;
|
|
36
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
37
|
+
/**
|
|
38
|
+
* Componente para mostrar conteúdo apenas para usuários NÃO autenticados (ex: página de Login)
|
|
39
|
+
*/
|
|
40
|
+
export declare const GuestOnly: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
41
|
+
fallback: {
|
|
42
|
+
type: PropType<VNode | string | object>;
|
|
43
|
+
default: null;
|
|
44
|
+
};
|
|
45
|
+
redirectTo: {
|
|
46
|
+
type: StringConstructor;
|
|
47
|
+
default: undefined;
|
|
48
|
+
};
|
|
49
|
+
}>, () => VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
50
|
+
[key: string]: any;
|
|
51
|
+
}> | VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
52
|
+
[key: string]: any;
|
|
53
|
+
}>[] | null, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
54
|
+
fallback: {
|
|
55
|
+
type: PropType<VNode | string | object>;
|
|
56
|
+
default: null;
|
|
57
|
+
};
|
|
58
|
+
redirectTo: {
|
|
59
|
+
type: StringConstructor;
|
|
60
|
+
default: undefined;
|
|
61
|
+
};
|
|
62
|
+
}>> & Readonly<{}>, {
|
|
63
|
+
fallback: string | object | VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
64
|
+
[key: string]: any;
|
|
65
|
+
}>;
|
|
66
|
+
redirectTo: string;
|
|
67
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
68
|
+
/**
|
|
69
|
+
* Composable para redirecionar baseado no status de autenticação.
|
|
70
|
+
* Equivalente ao hook useAuthRedirect do React.
|
|
71
|
+
*/
|
|
72
|
+
export declare function useAuthRedirect(authenticatedRedirect?: string, unauthenticatedRedirect?: string): void;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GuestOnly = exports.AuthGuard = void 0;
|
|
4
|
+
exports.useAuthRedirect = useAuthRedirect;
|
|
5
|
+
/*
|
|
6
|
+
* This file is part of the Vatts.js Project.
|
|
7
|
+
* Copyright (c) 2026 itsmuzin
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
const vue_1 = require("vue");
|
|
22
|
+
const session_1 = require("./session");
|
|
23
|
+
const vue_2 = require("vatts/vue");
|
|
24
|
+
// Helper reativo interno para garantir acesso às Refs
|
|
25
|
+
// O useAuth do session.ts pode retornar valores estáticos dependendo da implementação,
|
|
26
|
+
// então usamos useSession diretamente aqui para garantir reatividade via computed.
|
|
27
|
+
function useAuthReactive() {
|
|
28
|
+
const { data, status } = (0, session_1.useSession)();
|
|
29
|
+
// Casting para Ref pois o inject traz as refs do provide
|
|
30
|
+
const isAuthenticated = (0, vue_1.computed)(() => status.value === 'authenticated');
|
|
31
|
+
const isLoading = (0, vue_1.computed)(() => status.value === 'loading');
|
|
32
|
+
return { isAuthenticated, isLoading };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Guard simples que só renderiza o slot default se estiver autenticado.
|
|
36
|
+
* * Uso:
|
|
37
|
+
* <AuthGuard redirectTo="/login">
|
|
38
|
+
* <ConteudoProtegido />
|
|
39
|
+
* </AuthGuard>
|
|
40
|
+
*/
|
|
41
|
+
exports.AuthGuard = (0, vue_1.defineComponent)({
|
|
42
|
+
name: 'AuthGuard',
|
|
43
|
+
props: {
|
|
44
|
+
fallback: {
|
|
45
|
+
type: [Object, String],
|
|
46
|
+
default: null
|
|
47
|
+
},
|
|
48
|
+
redirectTo: {
|
|
49
|
+
type: String,
|
|
50
|
+
default: undefined
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
setup(props, { slots }) {
|
|
54
|
+
const { isAuthenticated, isLoading } = useAuthReactive();
|
|
55
|
+
// Lógica de redirecionamento
|
|
56
|
+
(0, vue_1.watchEffect)(() => {
|
|
57
|
+
if (props.redirectTo && !isLoading.value && !isAuthenticated.value) {
|
|
58
|
+
if (vue_2.router && typeof vue_2.router.push === 'function') {
|
|
59
|
+
vue_2.router.push(props.redirectTo);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
window.location.href = props.redirectTo;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
// Render Function
|
|
67
|
+
return () => {
|
|
68
|
+
if (isLoading.value) {
|
|
69
|
+
// Se houver fallback, renderiza ele (se for string cria text node, se for comp renderiza)
|
|
70
|
+
// Caso contrário renderiza div vazia para manter paridade com React
|
|
71
|
+
return props.fallback
|
|
72
|
+
? (typeof props.fallback === 'string' ? (0, vue_1.h)('div', props.fallback) : (0, vue_1.h)(props.fallback))
|
|
73
|
+
: (0, vue_1.h)('div');
|
|
74
|
+
}
|
|
75
|
+
if (!isAuthenticated.value) {
|
|
76
|
+
return props.fallback
|
|
77
|
+
? (typeof props.fallback === 'string' ? (0, vue_1.h)('div', props.fallback) : (0, vue_1.h)(props.fallback))
|
|
78
|
+
: null;
|
|
79
|
+
}
|
|
80
|
+
// Renderiza conteúdo principal
|
|
81
|
+
return slots.default ? slots.default() : null;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
/**
|
|
86
|
+
* Componente para mostrar conteúdo apenas para usuários NÃO autenticados (ex: página de Login)
|
|
87
|
+
*/
|
|
88
|
+
exports.GuestOnly = (0, vue_1.defineComponent)({
|
|
89
|
+
name: 'GuestOnly',
|
|
90
|
+
props: {
|
|
91
|
+
fallback: {
|
|
92
|
+
type: [Object, String],
|
|
93
|
+
default: null
|
|
94
|
+
},
|
|
95
|
+
redirectTo: {
|
|
96
|
+
type: String,
|
|
97
|
+
default: undefined
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
setup(props, { slots }) {
|
|
101
|
+
const { isAuthenticated, isLoading } = useAuthReactive();
|
|
102
|
+
(0, vue_1.watchEffect)(() => {
|
|
103
|
+
if (props.redirectTo && !isLoading.value && isAuthenticated.value) {
|
|
104
|
+
if (vue_2.router && typeof vue_2.router.push === 'function') {
|
|
105
|
+
vue_2.router.push(props.redirectTo);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
window.location.href = props.redirectTo;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
return () => {
|
|
113
|
+
if (isLoading.value || isAuthenticated.value) {
|
|
114
|
+
return props.fallback
|
|
115
|
+
? (typeof props.fallback === 'string' ? (0, vue_1.h)('div', props.fallback) : (0, vue_1.h)(props.fallback))
|
|
116
|
+
: (0, vue_1.h)('div');
|
|
117
|
+
}
|
|
118
|
+
return slots.default ? slots.default() : null;
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
/**
|
|
123
|
+
* Composable para redirecionar baseado no status de autenticação.
|
|
124
|
+
* Equivalente ao hook useAuthRedirect do React.
|
|
125
|
+
*/
|
|
126
|
+
function useAuthRedirect(authenticatedRedirect, unauthenticatedRedirect) {
|
|
127
|
+
const { isAuthenticated, isLoading } = useAuthReactive();
|
|
128
|
+
(0, vue_1.watchEffect)(() => {
|
|
129
|
+
if (isLoading.value)
|
|
130
|
+
return;
|
|
131
|
+
if (isAuthenticated.value && authenticatedRedirect) {
|
|
132
|
+
window.location.href = authenticatedRedirect;
|
|
133
|
+
}
|
|
134
|
+
else if (!isAuthenticated.value && unauthenticatedRedirect) {
|
|
135
|
+
window.location.href = unauthenticatedRedirect;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.SessionProvider = exports.GuestOnly = exports.AuthGuard = exports.useAuth = exports.useSession = void 0;
|
|
21
|
+
/*
|
|
22
|
+
* This file is part of the Vatts.js Project.
|
|
23
|
+
* Copyright (c) 2026 itsmuzin
|
|
24
|
+
*
|
|
25
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
26
|
+
* you may not use this file except in compliance with the License.
|
|
27
|
+
* You may obtain a copy of the License at
|
|
28
|
+
*
|
|
29
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
30
|
+
*
|
|
31
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
32
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
33
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
34
|
+
* See the License for the specific language governing permissions and
|
|
35
|
+
* limitations under the License.
|
|
36
|
+
*/
|
|
37
|
+
// Exportações do frontend
|
|
38
|
+
__exportStar(require("./session"), exports);
|
|
39
|
+
__exportStar(require("./guards"), exports);
|
|
40
|
+
// Re-exports das funções mais usadas para conveniência
|
|
41
|
+
var session_1 = require("./session");
|
|
42
|
+
Object.defineProperty(exports, "useSession", { enumerable: true, get: function () { return session_1.useSession; } });
|
|
43
|
+
Object.defineProperty(exports, "useAuth", { enumerable: true, get: function () { return session_1.useAuth; } });
|
|
44
|
+
var guards_1 = require("./guards");
|
|
45
|
+
Object.defineProperty(exports, "AuthGuard", { enumerable: true, get: function () { return guards_1.AuthGuard; } });
|
|
46
|
+
Object.defineProperty(exports, "GuestOnly", { enumerable: true, get: function () { return guards_1.GuestOnly; } });
|
|
47
|
+
const component_vue_1 = __importDefault(require("./component.vue"));
|
|
48
|
+
exports.SessionProvider = component_vue_1.default;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type InjectionKey, type Ref } from 'vue';
|
|
2
|
+
import type { Session, SessionContextType, User } from '../types';
|
|
3
|
+
export declare const SessionKey: InjectionKey<SessionContextType>;
|
|
4
|
+
export interface SessionProviderProps {
|
|
5
|
+
basePath: string;
|
|
6
|
+
refetchInterval: number;
|
|
7
|
+
refetchOnWindowFocus: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Composable que contém toda a lógica do SessionProvider.
|
|
11
|
+
* Deve ser chamado dentro do setup do componente.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useSessionProviderLogic(props: SessionProviderProps): {
|
|
14
|
+
session: Ref<{
|
|
15
|
+
user: User;
|
|
16
|
+
expires: string;
|
|
17
|
+
accessToken?: string | undefined;
|
|
18
|
+
} | null, Session | {
|
|
19
|
+
user: User;
|
|
20
|
+
expires: string;
|
|
21
|
+
accessToken?: string | undefined;
|
|
22
|
+
} | null>;
|
|
23
|
+
status: Ref<"loading" | "authenticated" | "unauthenticated", "loading" | "authenticated" | "unauthenticated">;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Hook para acessar a sessão atual
|
|
27
|
+
*/
|
|
28
|
+
export declare function useSession(): SessionContextType;
|
|
29
|
+
/**
|
|
30
|
+
* Hook para verificar autenticação
|
|
31
|
+
*/
|
|
32
|
+
export declare function useAuth(): {
|
|
33
|
+
user: User | null;
|
|
34
|
+
isAuthenticated: boolean;
|
|
35
|
+
isLoading: boolean;
|
|
36
|
+
};
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SessionKey = void 0;
|
|
4
|
+
exports.useSessionProviderLogic = useSessionProviderLogic;
|
|
5
|
+
exports.useSession = useSession;
|
|
6
|
+
exports.useAuth = useAuth;
|
|
7
|
+
/*
|
|
8
|
+
* This file is part of the Vatts.js Project.
|
|
9
|
+
* Copyright (c) 2026 itsmuzin
|
|
10
|
+
*
|
|
11
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
* you may not use this file except in compliance with the License.
|
|
13
|
+
* You may obtain a copy of the License at
|
|
14
|
+
*
|
|
15
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
18
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
19
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
+
* See the License for the specific language governing permissions and
|
|
21
|
+
* limitations under the License.
|
|
22
|
+
*/
|
|
23
|
+
const vue_1 = require("vue");
|
|
24
|
+
const vue_2 = require("vatts/vue");
|
|
25
|
+
// Chave de injeção para o TypeScript
|
|
26
|
+
exports.SessionKey = Symbol('SessionKey');
|
|
27
|
+
/**
|
|
28
|
+
* Composable que contém toda a lógica do SessionProvider.
|
|
29
|
+
* Deve ser chamado dentro do setup do componente.
|
|
30
|
+
*/
|
|
31
|
+
function useSessionProviderLogic(props) {
|
|
32
|
+
// Estado reativo
|
|
33
|
+
const session = (0, vue_1.ref)(null);
|
|
34
|
+
const status = (0, vue_1.ref)('loading');
|
|
35
|
+
// Fetch da sessão atual
|
|
36
|
+
const fetchSession = async () => {
|
|
37
|
+
try {
|
|
38
|
+
const response = await fetch(`${props.basePath}/session`, {
|
|
39
|
+
credentials: 'include'
|
|
40
|
+
});
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
status.value = 'unauthenticated';
|
|
43
|
+
session.value = null;
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const data = await response.json();
|
|
47
|
+
const sessionData = data.session;
|
|
48
|
+
if (sessionData) {
|
|
49
|
+
session.value = sessionData;
|
|
50
|
+
status.value = 'authenticated';
|
|
51
|
+
return sessionData;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
session.value = null;
|
|
55
|
+
status.value = 'unauthenticated';
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
console.error('[vatts-auth] Error fetching session:', error);
|
|
61
|
+
session.value = null;
|
|
62
|
+
status.value = 'unauthenticated';
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
// SignIn function
|
|
67
|
+
const signIn = async (provider = 'credentials', options = {}) => {
|
|
68
|
+
try {
|
|
69
|
+
const { redirect = true, callbackUrl, ...credentials } = options;
|
|
70
|
+
const response = await fetch(`${props.basePath}/signin`, {
|
|
71
|
+
method: 'POST',
|
|
72
|
+
headers: {
|
|
73
|
+
'Content-Type': 'application/json',
|
|
74
|
+
},
|
|
75
|
+
credentials: 'include',
|
|
76
|
+
body: JSON.stringify({
|
|
77
|
+
provider,
|
|
78
|
+
...credentials
|
|
79
|
+
})
|
|
80
|
+
});
|
|
81
|
+
const data = await response.json();
|
|
82
|
+
if (response.ok && data.success) {
|
|
83
|
+
await fetchSession();
|
|
84
|
+
if (data.type === 'oauth' && data.redirectUrl) {
|
|
85
|
+
if (redirect && typeof window !== 'undefined') {
|
|
86
|
+
window.location.href = data.redirectUrl;
|
|
87
|
+
}
|
|
88
|
+
return { ok: true, status: 200, url: data.redirectUrl };
|
|
89
|
+
}
|
|
90
|
+
if (data.type === 'session') {
|
|
91
|
+
const finalUrl = callbackUrl || '/';
|
|
92
|
+
if (redirect && typeof window !== 'undefined') {
|
|
93
|
+
try {
|
|
94
|
+
if (vue_2.router && typeof vue_2.router.push === 'function') {
|
|
95
|
+
vue_2.router.push(finalUrl);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
window.location.href = finalUrl;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
window.location.href = finalUrl;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return { ok: true, status: 200, url: finalUrl };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
return {
|
|
110
|
+
error: data.error || 'Authentication failed',
|
|
111
|
+
status: response.status,
|
|
112
|
+
ok: false
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
console.error('[vatts-auth] Error on signIn:', error);
|
|
118
|
+
return { error: 'Network error', status: 500, ok: false };
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
// SignOut function
|
|
122
|
+
const signOut = async (options = {}) => {
|
|
123
|
+
try {
|
|
124
|
+
await fetch(`${props.basePath}/signout`, {
|
|
125
|
+
method: 'POST',
|
|
126
|
+
credentials: 'include'
|
|
127
|
+
});
|
|
128
|
+
session.value = null;
|
|
129
|
+
status.value = 'unauthenticated';
|
|
130
|
+
if (typeof window !== 'undefined') {
|
|
131
|
+
const url = options.callbackUrl || '/';
|
|
132
|
+
try {
|
|
133
|
+
if (vue_2.router && typeof vue_2.router.push === 'function') {
|
|
134
|
+
vue_2.router.push(url);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
window.location.href = url;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch (e) {
|
|
141
|
+
window.location.href = url;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
console.error('[vatts-auth] Error on signOut:', error);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const update = async () => {
|
|
150
|
+
return await fetchSession();
|
|
151
|
+
};
|
|
152
|
+
// Ciclo de vida e Listeners
|
|
153
|
+
(0, vue_1.onMounted)(() => {
|
|
154
|
+
fetchSession();
|
|
155
|
+
// Refetch Interval
|
|
156
|
+
let intervalId = null;
|
|
157
|
+
if (props.refetchInterval > 0) {
|
|
158
|
+
intervalId = setInterval(() => {
|
|
159
|
+
if (status.value === 'authenticated') {
|
|
160
|
+
fetchSession();
|
|
161
|
+
}
|
|
162
|
+
}, props.refetchInterval * 1000);
|
|
163
|
+
}
|
|
164
|
+
// Refetch on Focus
|
|
165
|
+
const handleFocus = () => {
|
|
166
|
+
if (props.refetchOnWindowFocus && status.value === 'authenticated') {
|
|
167
|
+
fetchSession();
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
if (props.refetchOnWindowFocus) {
|
|
171
|
+
window.addEventListener('focus', handleFocus);
|
|
172
|
+
}
|
|
173
|
+
(0, vue_1.onUnmounted)(() => {
|
|
174
|
+
if (intervalId)
|
|
175
|
+
clearInterval(intervalId);
|
|
176
|
+
window.removeEventListener('focus', handleFocus);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
// Fornece o contexto para os filhos
|
|
180
|
+
(0, vue_1.provide)(exports.SessionKey, {
|
|
181
|
+
data: session,
|
|
182
|
+
status: status,
|
|
183
|
+
signIn,
|
|
184
|
+
signOut,
|
|
185
|
+
update
|
|
186
|
+
});
|
|
187
|
+
return { session, status };
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Hook para acessar a sessão atual
|
|
191
|
+
*/
|
|
192
|
+
function useSession() {
|
|
193
|
+
const context = (0, vue_1.inject)(exports.SessionKey);
|
|
194
|
+
if (!context) {
|
|
195
|
+
throw new Error('useSession must be used inside a SessionProvider');
|
|
196
|
+
}
|
|
197
|
+
return context;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Hook para verificar autenticação
|
|
201
|
+
*/
|
|
202
|
+
function useAuth() {
|
|
203
|
+
const context = useSession();
|
|
204
|
+
// Tratando Refs injetadas
|
|
205
|
+
const sessionData = context.data.value;
|
|
206
|
+
const statusVal = context.status.value;
|
|
207
|
+
return {
|
|
208
|
+
user: sessionData?.user || null,
|
|
209
|
+
isAuthenticated: statusVal === 'authenticated',
|
|
210
|
+
isLoading: statusVal === 'loading'
|
|
211
|
+
};
|
|
212
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vatts/auth",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0-alpha.2",
|
|
4
4
|
"description": "Authentication package for Vatts.js framework",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
"types": "./dist/react/index.d.ts",
|
|
18
18
|
"import": "./dist/react/index.js",
|
|
19
19
|
"require": "./dist/react/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./vue": {
|
|
22
|
+
"types": "./dist/vue/index.d.ts",
|
|
23
|
+
"import": "./dist/vue/index.js",
|
|
24
|
+
"require": "./dist/vue/index.js"
|
|
20
25
|
}
|
|
21
26
|
},
|
|
22
27
|
"keywords": [
|
|
@@ -36,13 +41,16 @@
|
|
|
36
41
|
"devDependencies": {
|
|
37
42
|
"@types/node": "^20.11.24",
|
|
38
43
|
"@types/react": "^19.2.2",
|
|
44
|
+
"copyfiles": "^2.4.1",
|
|
39
45
|
"rimraf": "^5.0.0",
|
|
40
|
-
"typescript": "^5.9.3"
|
|
46
|
+
"typescript": "^5.9.3",
|
|
47
|
+
"vue": "^3.5.27"
|
|
41
48
|
},
|
|
42
49
|
"peerDependencies": {
|
|
43
50
|
"react": "^19.2.0",
|
|
44
51
|
"react-dom": "^19.2.0",
|
|
45
|
-
"
|
|
52
|
+
"vue": "^3.5.27",
|
|
53
|
+
"vatts": "^1.2.0-alpha.2"
|
|
46
54
|
},
|
|
47
55
|
"peerDependenciesMeta": {
|
|
48
56
|
"react-dom": {
|
|
@@ -50,7 +58,7 @@
|
|
|
50
58
|
}
|
|
51
59
|
},
|
|
52
60
|
"scripts": {
|
|
53
|
-
"build": "tsc",
|
|
61
|
+
"build": "tsc && copyfiles -u 1 \"src/**/*.vue\" dist",
|
|
54
62
|
"build:watch": "tsc --watch",
|
|
55
63
|
"clean": "rimraf dist",
|
|
56
64
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|