本篇文章给大家谈谈在受保护的路由中刷新时反应身份验证“失败”,以及路由器处在被保护的状态的知识点,同时本文还将给你拓展c#–受保护的内部保护和内部受保护之间有区别吗?、css–Less–使用在受保护的
本篇文章给大家谈谈在受保护的路由中刷新时反应身份验证“失败”,以及路由器处在被保护的状态的知识点,同时本文还将给你拓展c# – 受保护的内部保护和内部受保护之间有区别吗?、css – Less – 使用在受保护的mixins中创建的类、Firebase身份验证在刷新时延迟、Flutter 使用受基本身份验证保护的网络图像 问题等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- 在受保护的路由中刷新时反应身份验证“失败”(路由器处在被保护的状态)
- c# – 受保护的内部保护和内部受保护之间有区别吗?
- css – Less – 使用在受保护的mixins中创建的类
- Firebase身份验证在刷新时延迟
- Flutter 使用受基本身份验证保护的网络图像 问题
在受保护的路由中刷新时反应身份验证“失败”(路由器处在被保护的状态)
如何解决在受保护的路由中刷新时反应身份验证“失败”?
我遇到了受保护路由和受保护路由身份验证的问题。
我在整个 web 应用程序中使用 AuthContext 进行整个用户身份验证。我将所有用户数据保存在一个状态中。在 useEffect 和 sessionStorage 的帮助下,我存储了用户对象,以便它可以在页面重新加载后使用。
[...]
export const AuthProvider = ({ children }) => {
const [user,setUser] = useState({})
useEffect(() => {
const sessionUser = sessionStorage.getItem("user")
if(!sessionUser) return
changeUser(JSON.parse(sessionUser))
},[])
const hasRank = (ranks) => {
if(!Object.keys(user).length > 0) return false
const matchingPerms = ranks.filter(rank => user.rank.includes(rank))
return matchingPerms.length > 0
}
const changeUser = (data) => {
setUser(data)
if(Object.keys(data).length > 0) {
return sessionStorage.setItem("user",JSON.stringify(data))
}
return sessionStorage.removeItem("user")
}
}
[...]
为了保护某些页面,我使用了一个受保护的路由组件来检查用户是否登录。
[...]
const auth = useContext(AuthContext)
const isAuthorized = auth.hasRank(rest.rank)
<Route
{...rest}
render={props => {
return isAuthorized ? (
<Component {...props} />
) : (
<Redirect to="/auth/login" />
)
}}
/>
在 sessionStorage 中的保存和提取工作正常,直到我想呈现受保护路由内的内容 - 我总是被重定向到登录页面,因为用户对象因重新加载而为空而状态不是更新够早。因此,受保护的路由使用空对象检查身份验证,这会导致重定向到登录页面。
如何等待用户状态更新后再检查受保护路由内的身份验证?
编辑:
应用组件:
return (
<Router>
<Switch>
<Route path="/auth/register" component={Register} />
<Route path="/auth/login" component={LogIn} />
<Route path="/err/404" component={Page404} />
<Route path="/" component={PanelRoutes}/>
</Switch>
</Router>
)
PanelRoutes 组件:
return (
<div className="skin-default fixed-layout">
<div id="main-wrapper">
<Topbar />
<Sidebar />
<div className="page-wrapper">
<div className="container-fluid">
<Switch>
<Route exact path="/" component={Homepage} />
<Route exact path="/legal/imprint" component={Imprint} />
<Route exact path="/legal/privacy" component={Privacy} />
<ProtectedRoute exact path="/mc/sounds" component={Mcsounds} rank={["ADMIN","MC"]} />
<ProtectedRoute exact path="/admin/users" component={AdminUsers} rank={["ADMIN"]} />
</Switch>
</div>
</div>
</div>
</div>
)
亲切的问候
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
c# – 受保护的内部保护和内部受保护之间有区别吗?
public class TestClass { protected internal int FieldA; internal protected int FieldB; }
受保护的内部成员和内部成员之间是否存在差异?
解决方法
Is there a difference between
protected internal
andinternal protected
members?
它们之间没有区别.
受保护的内部意味着保护或内部
内部保护意味着内部或受保护.
The type or member can be accessed by any code in the assembly in
which it is declared,or from within a derived class in another
assembly. Access from another assembly must take place within a class
declaration that derives from the class in which the protected
internal element is declared,and it must take place through an
instance of the derived class type.
在这两者中,通常使用受保护的内部. MSDN page about Access Modifiers上没有对内部保护的引用.
另请参阅Phil Haack的博客文章What Does Protected Internal Mean?
css – Less – 使用在受保护的mixins中创建的类
.loop (@index) when (@index >= 10) { (~".font@{index}") { font-size: ~"@{index}px"; } .loop(@index - 1); } .loop (0) {} .loop (10);
哪个输出:
.font15 { font-size: 15px; } .font14 { font-size: 14px; } .font13 { font-size: 13px; } .font12 { font-size: 12px; } .font11 { font-size: 11px; } .font10 { font-size: 10px; }
在Less文档的最后,我有这个类:
.title{ font-size:14px; margin-bottom:0.5em; .font13; }
我正在使用WinLess来编译它,我得到一个错误,说“.font13”是未定义的
有没有办法在同一个文档中使用“动态”定义的类?
谢谢!
解决方法
所以做这样的事情的最好方法是设计一个getter mixin(你可以从其他规则调用),也许是一个生成器mixin(编写.font10,.font11,… .fontNN类)…如果你只想在循环中生成类(并且你可以将它与循环合并),则不需要后者.
像这样的东西:
.getFont(@size) { font-size: ~"@{size}px"} .genFontClass (@size) { (~".font@{size}") { .getFont(@size); } }
然后你可以使用你的循环来生成.fontNN类:
.loop (@index) when (@index >= 10) { .genFontClass (@index); .loop(@index - 1); } .loop (@index) when (@index < 10) {}
使用例如索引13:
.loop (13);
输出CSS:
.font13 { font-size: 13px; } .font12 { font-size: 12px; } .font11 { font-size: 11px; } .font10 { font-size: 10px; }
并且独立于直接打印到输出CSS的这些生成的类(并且无法从其他Less规则中访问),您可以调用getter mixin,为规则添加所需的属性,在我们的示例中为所需索引13的字体:
.title{ margin-bottom:0.5em; .getFont(13); }
现在将字体大小属性添加到.title规则.
CSS:
.title { margin-bottom: 0.5em; font-size: 13px; }
希望这可以帮助.
Firebase身份验证在刷新时延迟
我的SPA React / Firebase应用程序上的硬刷新在立即执行功能时不保持身份验证状态。我有一个解决方法,但是它很粗略。
我的反应路线利用该onEnter
功能来确定用户是否已通过身份验证。例如
<Route path="/secure" component={Dashboard} onEnter={requireAuth}/>
此外,我的requireAuth
函数如下所示:
function (nextState, replace) { console.log(''requireAuth'', firebase.auth().currentUser); if (!firebase.auth().currentUser) { console.log(''attempting to access a secure route. please login first.''); replace({ pathname: ''/login'', state: { nextPathname: nextState.location.pathname } }); }};
但是,在进行强制刷新时,会稍有延迟firebase.auth().currentUser
。首先为null,然后POST
对firebase服务器执行,以确定身份验证状态。返回时将currentUser
填充对象。但是,这种延迟会导致问题。
我的hacky解决方案如下: 更新: 这实际上不起作用…
function (nextState, replace) { setTimeout(function () { console.log(''requireAuth'', firebase.auth().currentUser); if (!firebase.auth().currentUser) { console.log(''attempting to access a secure route. please login first.''); replace({ pathname: ''/login'', state: { nextPathname: nextState.location.pathname } }); } }, 50);};
只需将其包装在超时中即可。但是,我真的不喜欢这个……有什么想法吗?
更新:
我还尝试将其包装在一个onAuthStateChanged
侦听器中,这应该比setTimeout
具有确定的时间延迟的a 更准确。代码如下:
function (nextState, replace) { var unsubscribe = firebase.auth().onAuthStateChanged(function (user) { if (!user) { console.log(''attempting to access a secure route''); replace({ pathname: ''/login'', state: { nextPathname: nextState.location.pathname } }) console.log(''should have called replace''); } unsubscribe(); }); // setTimeout(function () { // console.log(''requireAuth'', firebase.auth().currentUser); // if (!firebase.auth().currentUser) { // console.log(''attempting to access a secure route. please login first.''); // replace({ // pathname: ''/login'', // state: { nextPathname: nextState.location.pathname } // }); // } // }, 50);};
这两个日志语句已执行,但是react-router replace
似乎未正确执行。对于反应路由器专家来说,也许这是一个不同的问题。
更新2:
我正在做这件事的时候已经很晚了。显然setTimeout
实际上也不起作用。
答案1
小编典典好的。因此,我能够利用localStorage
firebase提供的变量来存储用户信息来解决此问题。
function (nextState, replace) { if (!firebase.auth().currentUser) { let hasLocalStorageUser = false; for (let key in localStorage) { if (key.startsWith("firebase:authUser:")) { hasLocalStorageUser = true; } } if (!hasLocalStorageUser) { console.log(''Attempting to access a secure route. Please authenticate first.''); replace({ pathname: ''/login'', state: { nextPathname: nextState.location.pathname } }); } }};
Flutter 使用受基本身份验证保护的网络图像 问题
如何解决Flutter 使用受基本身份验证保护的网络图像 问题?
我们有一个主站点,其中包含所有用户上传的内容,通常只是个人资料图片。
所有上传的文件夹都受到 Nginx 和 basic auth 的保护。所以你需要输入用户名和密码才能访问。
问题
如何使用 Flutter cached_network_image 做到这一点?或者类似的东西。我们不想将图像物理存储在设备上。
解决方法
您可以在 httpHeaders
小部件上使用 CachedNetworkImage
属性来传递自定义身份验证标头。
CachedNetworkImage(
imageUrl: ...,imageBuilder: ...,placeholder: (context,url) => CircularProgressIndicator(),errorWidget: (context,url,error) => Icon(Icons.error),httpHeaders: {
"Authorization": "Basic $token",}
),
关于在受保护的路由中刷新时反应身份验证“失败”和路由器处在被保护的状态的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于c# – 受保护的内部保护和内部受保护之间有区别吗?、css – Less – 使用在受保护的mixins中创建的类、Firebase身份验证在刷新时延迟、Flutter 使用受基本身份验证保护的网络图像 问题的相关知识,请在本站寻找。
本文标签: