@ssplib/react-components 0.0.289 → 0.0.292
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.
|
@@ -1,16 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import { User } from '../../types/auth';
|
|
3
|
-
interface AuthContextData {
|
|
4
|
-
user: User | null | undefined;
|
|
5
|
-
isAuth: boolean;
|
|
6
|
-
userLoaded: boolean;
|
|
7
|
-
login: () => void;
|
|
8
|
-
logout: () => void;
|
|
9
|
-
saveUserData: () => void;
|
|
10
|
-
refreshTokens: () => Promise<boolean>;
|
|
11
|
-
type: 'govbr' | 'ad';
|
|
12
|
-
}
|
|
13
|
-
export declare const AuthContext: React.Context<AuthContextData>;
|
|
1
|
+
/// <reference types="react" />
|
|
14
2
|
export declare const cookieName = "nextauth.token";
|
|
15
3
|
export declare function KeycloakAuthProvider({ url, realm, clientId, children, type, resource_name, redirectUri, }: {
|
|
16
4
|
url: string;
|
|
@@ -21,4 +9,3 @@ export declare function KeycloakAuthProvider({ url, realm, clientId, children, t
|
|
|
21
9
|
resource_name?: string;
|
|
22
10
|
redirectUri?: string;
|
|
23
11
|
}): JSX.Element;
|
|
24
|
-
export {};
|
|
@@ -22,24 +22,15 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
27
|
};
|
|
37
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
exports.KeycloakAuthProvider = exports.cookieName =
|
|
39
|
-
const keycloak_js_1 = __importDefault(require("keycloak-js"));
|
|
29
|
+
exports.KeycloakAuthProvider = exports.cookieName = void 0;
|
|
40
30
|
const router_1 = require("next/router");
|
|
41
31
|
const react_1 = __importStar(require("react"));
|
|
42
|
-
|
|
32
|
+
const auth_1 = require("../../context/auth");
|
|
33
|
+
const keycloak_js_1 = __importDefault(require("keycloak-js"));
|
|
43
34
|
exports.cookieName = 'nextauth.token';
|
|
44
35
|
const userImgName = 'user-data.img';
|
|
45
36
|
function KeycloakAuthProvider({ url, realm, clientId, children, type = 'ad', resource_name = 'eventos-front', redirectUri = '', }) {
|
|
@@ -48,33 +39,12 @@ function KeycloakAuthProvider({ url, realm, clientId, children, type = 'ad', res
|
|
|
48
39
|
const [kc, setKc] = (0, react_1.useState)(null);
|
|
49
40
|
const router = (0, router_1.useRouter)();
|
|
50
41
|
const isAuth = !!user;
|
|
51
|
-
const updateUserData = (token) => {
|
|
52
|
-
var _a, _b;
|
|
53
|
-
if (!kc)
|
|
54
|
-
return;
|
|
55
|
-
const tokenParsed = kc.tokenParsed;
|
|
56
|
-
const roles = ((_b = ((_a = tokenParsed === null || tokenParsed === void 0 ? void 0 : tokenParsed.resource_access) !== null && _a !== void 0 ? _a : {})[resource_name]) === null || _b === void 0 ? void 0 : _b.roles) || [];
|
|
57
|
-
setUser(Object.assign(Object.assign({}, tokenParsed), { token,
|
|
58
|
-
roles }));
|
|
59
|
-
};
|
|
60
|
-
const refreshTokens = () => __awaiter(this, void 0, void 0, function* () {
|
|
61
|
-
if (!kc)
|
|
62
|
-
return false;
|
|
63
|
-
try {
|
|
64
|
-
const refreshed = yield kc.updateToken(5);
|
|
65
|
-
if (refreshed) {
|
|
66
|
-
updateUserData(kc.token);
|
|
67
|
-
}
|
|
68
|
-
return refreshed;
|
|
69
|
-
}
|
|
70
|
-
catch (err) {
|
|
71
|
-
console.error('Falha ao atualizar token:', err);
|
|
72
|
-
logout();
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
42
|
(0, react_1.useEffect)(() => {
|
|
77
|
-
const keycloak = new keycloak_js_1.default({
|
|
43
|
+
const keycloak = new keycloak_js_1.default({
|
|
44
|
+
url: url,
|
|
45
|
+
realm: realm,
|
|
46
|
+
clientId: clientId,
|
|
47
|
+
});
|
|
78
48
|
setKc(keycloak);
|
|
79
49
|
keycloak
|
|
80
50
|
.init({
|
|
@@ -82,23 +52,29 @@ function KeycloakAuthProvider({ url, realm, clientId, children, type = 'ad', res
|
|
|
82
52
|
pkceMethod: 'S256',
|
|
83
53
|
})
|
|
84
54
|
.then((auth) => {
|
|
55
|
+
var _a, _b;
|
|
85
56
|
setUserLoaded(true);
|
|
86
57
|
if (!auth) {
|
|
87
58
|
console.log('NAO AUTENTICADO');
|
|
59
|
+
//window.location.reload();
|
|
88
60
|
}
|
|
89
61
|
else {
|
|
90
|
-
|
|
62
|
+
const tokenParsed = keycloak.tokenParsed;
|
|
63
|
+
console.log('TOKEN-> ', tokenParsed);
|
|
64
|
+
const userData = Object.assign(Object.assign({}, tokenParsed), { token: keycloak.token, roles: ((_b = ((_a = tokenParsed === null || tokenParsed === void 0 ? void 0 : tokenParsed.resource_access) !== null && _a !== void 0 ? _a : {})[resource_name]) !== null && _b !== void 0 ? _b : []).roles });
|
|
65
|
+
setUser(userData);
|
|
91
66
|
console.info('Authenticated!!!');
|
|
92
67
|
keycloak.onTokenExpired = () => {
|
|
93
|
-
console.log('token expired
|
|
94
|
-
refreshTokens();
|
|
68
|
+
console.log('token expired');
|
|
95
69
|
};
|
|
96
70
|
}
|
|
71
|
+
}, () => {
|
|
72
|
+
console.error('Authenticated Failed');
|
|
97
73
|
})
|
|
98
|
-
.catch((err) =>
|
|
99
|
-
console.error('Authenticated Failed', err);
|
|
100
|
-
});
|
|
74
|
+
.catch((err) => console.log(err));
|
|
101
75
|
const s = router.query['status'];
|
|
76
|
+
if (!s)
|
|
77
|
+
return;
|
|
102
78
|
if (s === 'success') {
|
|
103
79
|
window.history.replaceState(null, '', '/');
|
|
104
80
|
}
|
|
@@ -118,6 +94,6 @@ function KeycloakAuthProvider({ url, realm, clientId, children, type = 'ad', res
|
|
|
118
94
|
setUser(null);
|
|
119
95
|
localStorage.removeItem(userImgName);
|
|
120
96
|
}
|
|
121
|
-
return react_1.default.createElement(
|
|
97
|
+
return react_1.default.createElement(auth_1.AuthContext.Provider, { value: { user, isAuth, userLoaded, login, logout, saveUserData: () => { }, type } }, children);
|
|
122
98
|
}
|
|
123
99
|
exports.KeycloakAuthProvider = KeycloakAuthProvider;
|