UNPKG

@krashnakant/next-keycloak

Version:

An authentication library based on Keycloak for NextJs applications

73 lines (72 loc) 2.55 kB
!function(e, t) { "object" == typeof exports && "undefined" != typeof module ? t(exports, require("react")) : "function" == typeof define && define.amd ? define([ "exports", "react" ], t) : t((e = "undefined" != typeof globalThis ? globalThis : e || self).KrashnakantNextKeycloak = {}, e.React); }(this, (function(e, t) { "use strict"; const isServer = () => "undefined" == typeof window, getUserFromToken = e => { const t = parseToken(e); return { sub: t.sub, email: t.email, name: t.given_name, family: t.family_name }; }, parseToken = e => { const t = e.split(".")[1].replace("-", "+").replace("_", "/"), n = decodeURIComponent(atob(t).split("").map((function(e) { return "%" + ("00" + e.charCodeAt(0).toString(16)).slice(-2); })).join("")); return JSON.parse(n); }, n = isServer() ? null : require("keycloak-js"); let o = n; const a = t.createContext({ loading: !0, authenticated: !1, login: e => {}, logout: e => {}, register: e => {}, hasRealmRole: e => !1, accountManagement: () => {} }); e.NextKeycloakAuthProvider = ({children: e, config: i, initOption: r}) => { const l = ((e, n = {}) => { const [o, a] = t.useState(); t.useEffect((() => { e && initialize(); }), []); const initialize = async () => { try { await e.init(n), a(e); } catch (e) { console.error("failed to initialize keycloak"); } }; return { loading: !(null != o && o.authenticated && void 0 !== o.token), token: o ? o.token : "", authenticated: !!o && Boolean(o.authenticated), userInfo: null != o && o.authenticated ? getUserFromToken(String(o.token)) : void 0, login: e => { if (o) return o.login(e); }, logout: e => { if (void 0 !== o) return o.logout(e); }, register: e => { if (o) return o.register(e); }, hasRealmRole: e => !!o && o.hasRealmRole(e), accountManagement: () => { if (o) return o.accountManagement(); } }; })(t.useMemo((() => (e => { if (!isServer()) return o = "function" == typeof n.default ? new n.default(e) : new n(e), o; })(i)), [ i ]), r); return t.createElement(a.Provider, { value: l }, e); }, e.useNextKeycloakAuth = () => t.useContext(a), Object.defineProperty(e, "__esModule", { value: !0 }); })); //# sourceMappingURL=next-keycloak.umd.production.min.cjs.map