@iad-os/react-ghost-auth
Advanced tools
Comparing version 0.1.22 to 0.1.23
import AuthenticationProvider from './Authentication'; | ||
import { useAuthentication, AuthenticationConfig } from './Authentication'; | ||
import Autologin from './components/AutoLogin'; | ||
import LoggedIn from './components/LoggedIn'; | ||
import LogginIn from './components/LogginIn'; | ||
import Public from './components/Public'; | ||
import RequireAuth from './components/RequireAuth'; | ||
import { TokenResponse } from './models/TokenResponse'; | ||
export default AuthenticationProvider; | ||
export { useAuthentication }; | ||
export type { AuthenticationConfig }; | ||
export type { AuthenticationConfig, TokenResponse }; | ||
export { Autologin, LoggedIn, LogginIn, Public, RequireAuth }; |
@@ -1,2 +0,2 @@ | ||
import { jsx } from 'react/jsx-runtime'; | ||
import { jsx, Fragment, jsxs } from 'react/jsx-runtime'; | ||
import React, { useState, useMemo, useEffect, useCallback, useContext } from 'react'; | ||
@@ -902,3 +902,57 @@ import { randomBytes, createHash } from 'crypto'; | ||
export { AuthenticationProvider as default, useAuthentication }; | ||
function AutoLogin(props) { | ||
const { children } = props; | ||
const { login, isAuthenticated, status, providerInfo } = useAuthentication(); | ||
const { list: providers } = providerInfo(); | ||
useEffect(() => { | ||
if (!isAuthenticated() && status === 'LOGIN' && providers) { | ||
providers.length === 1 && login(providers[0]); | ||
} | ||
}, [status]); | ||
return (jsx(Fragment, { children: providers && | ||
providers.length > 1 && | ||
!isAuthenticated() && | ||
status === 'LOGIN' && | ||
children }, void 0)); | ||
} | ||
function LoggedIn(props) { | ||
const { children } = props; | ||
const { isAuthenticated, status } = useAuthentication(); | ||
return jsx("div", { children: isAuthenticated() && status === 'LOGGED' && children }, void 0); | ||
} | ||
function LogginIn(props) { | ||
const { children } = props; | ||
const { isAuthenticated, status } = useAuthentication(); | ||
return jsx("div", { children: !isAuthenticated() && status === 'LOGGING' && children }, void 0); | ||
} | ||
function Public(props) { | ||
const { children } = props; | ||
const { status } = useAuthentication(); | ||
return jsx(Fragment, { children: status !== 'LOGGING' && children }, void 0); | ||
} | ||
function RequireAuth(props) { | ||
const { authRequired: _authRequired = true, children } = props; | ||
const { status, changeStatus, isAuthenticated } = useAuthentication(); | ||
const [authRequired, setAuthRequired] = useState(false); | ||
useEffect(() => { | ||
if (typeof _authRequired === 'function') { | ||
_authRequired().then(res => setAuthRequired(res)); | ||
} | ||
else { | ||
setAuthRequired(_authRequired); | ||
} | ||
}, []); | ||
useEffect(() => { | ||
if (status === 'INIT' && authRequired) { | ||
changeStatus('LOGIN'); | ||
} | ||
}, [authRequired]); | ||
return (jsxs(Fragment, { children: [authRequired && jsx(LoggedIn, { children: children }, void 0), !authRequired && jsx(Fragment, { children: children }, void 0)] }, void 0)); | ||
} | ||
export { AutoLogin as Autologin, LoggedIn, LogginIn, Public, RequireAuth, AuthenticationProvider as default, useAuthentication }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -910,4 +910,63 @@ 'use strict'; | ||
function AutoLogin(props) { | ||
const { children } = props; | ||
const { login, isAuthenticated, status, providerInfo } = useAuthentication(); | ||
const { list: providers } = providerInfo(); | ||
React.useEffect(() => { | ||
if (!isAuthenticated() && status === 'LOGIN' && providers) { | ||
providers.length === 1 && login(providers[0]); | ||
} | ||
}, [status]); | ||
return (jsxRuntime.jsx(jsxRuntime.Fragment, { children: providers && | ||
providers.length > 1 && | ||
!isAuthenticated() && | ||
status === 'LOGIN' && | ||
children }, void 0)); | ||
} | ||
function LoggedIn(props) { | ||
const { children } = props; | ||
const { isAuthenticated, status } = useAuthentication(); | ||
return jsxRuntime.jsx("div", { children: isAuthenticated() && status === 'LOGGED' && children }, void 0); | ||
} | ||
function LogginIn(props) { | ||
const { children } = props; | ||
const { isAuthenticated, status } = useAuthentication(); | ||
return jsxRuntime.jsx("div", { children: !isAuthenticated() && status === 'LOGGING' && children }, void 0); | ||
} | ||
function Public(props) { | ||
const { children } = props; | ||
const { status } = useAuthentication(); | ||
return jsxRuntime.jsx(jsxRuntime.Fragment, { children: status !== 'LOGGING' && children }, void 0); | ||
} | ||
function RequireAuth(props) { | ||
const { authRequired: _authRequired = true, children } = props; | ||
const { status, changeStatus, isAuthenticated } = useAuthentication(); | ||
const [authRequired, setAuthRequired] = React.useState(false); | ||
React.useEffect(() => { | ||
if (typeof _authRequired === 'function') { | ||
_authRequired().then(res => setAuthRequired(res)); | ||
} | ||
else { | ||
setAuthRequired(_authRequired); | ||
} | ||
}, []); | ||
React.useEffect(() => { | ||
if (status === 'INIT' && authRequired) { | ||
changeStatus('LOGIN'); | ||
} | ||
}, [authRequired]); | ||
return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [authRequired && jsxRuntime.jsx(LoggedIn, { children: children }, void 0), !authRequired && jsxRuntime.jsx(jsxRuntime.Fragment, { children: children }, void 0)] }, void 0)); | ||
} | ||
exports.Autologin = AutoLogin; | ||
exports.LoggedIn = LoggedIn; | ||
exports.LogginIn = LogginIn; | ||
exports.Public = Public; | ||
exports.RequireAuth = RequireAuth; | ||
exports["default"] = AuthenticationProvider; | ||
exports.useAuthentication = useAuthentication; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@iad-os/react-ghost-auth", | ||
"version": "0.1.22", | ||
"version": "0.1.23", | ||
"maintainers": [ | ||
@@ -5,0 +5,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
159964
1773