gpt4 book ai didi

javascript - 使用 Aurelia 进行 Firebase Angular 色授权

转载 作者:行者123 更新时间:2023-12-03 02:04:11 24 4
gpt4 key购买 nike

目前为了避免未登录的用户未经许可进入任何路线,我设置了此类:

export class AuthorizeStep {
run(navigationInstruction, next) {
return new Promise((resolve, reject) => {
firebase.auth().onAuthStateChanged(user => {
let currentRoute = navigationInstruction.config;
let loginRequired = currentRoute.auth && currentRoute.auth === true;

if (!user && loginRequired) {
return resolve(next.cancel(new Redirect('')));
}

return resolve(next());
});
});
}

这里调用的是:

configureRouter(config, router) {
this.router = router;
config.title = 'title';

config.map([
{ route: ['', 'inicio'], name: 'inicio', moduleId: './modules/contacts/components/inicio', title: 'au-samples.contacts.mainPage' },
{ route: 'conta', name: 'conta', moduleId: './modules/admin/conta', title: 'accountMessages.account', auth: true},
{ route: 'contacts', name: 'contacts', moduleId: './modules/contacts/components/list', title: 'au-samples.contacts.contacts', auth: true},
{ route: ':id', name: 'contact-details', moduleId: './modules/contacts/components/details', auth: true },
{ route: ':id/edit', name: 'contact-edition', moduleId: './modules/contacts/components/edition', auth: true },
{ route: ':id/photo', name: 'contact-photo', moduleId: './modules/contacts/components/photo', auth: true }
]);
config.mapUnknownRoutes('not-found');
config.addPipelineStep('authorize', AuthorizeStep);
}

所有这些都运行良好,但我希望有一种方法可以让我提取用户的数据,我有办法做到这一点,获取其 Angular 色(我也可以这样做)并检查如果用户在访问特定路由之前具有该 Angular 色,我想知道是否需要执行另一个类并使用 addPipelineStep 在我的configureRouter中调用它,或者通过检查来启用基于 Angular 色的授权的另一种方法是什么变量(在本例中,如果数组包含单词)。

提前致谢。

最佳答案

假设我正确阅读了您的问题并且您能够正确检索用户 Angular 色,那么您正在寻找一种方法来根据用户可以拥有的特定 Angular 色来授权对某些路由的访问。

使用管道步骤,您可以非常自由地找出您喜欢的任何实现。您可以在路由上使用 settings 参数,如下所示:

config.map([
{
route: 'conta',
name: 'conta',
moduleId: './modules/admin/conta',
title: 'accountMessages.account',
auth: true,
settings: {
role: 'admin' /* or any naming of choice */
}
}
]);

然后在管道步骤中,您可以检查此属性并根据用户是否拥有该属性来限制访问:

run(navigationInstruction, next) {
return new Promise((resolve, reject) => {
firebase.auth().onAuthStateChanged(user => {
let currentRoute = navigationInstruction.config;
let loginRequired = currentRoute.auth && currentRoute.auth === true;

if (!user && loginRequired) {
return resolve(next.cancel(new Redirect('')));
}

// hasRole checks the user roles for the role set on the route
if (currentRoute.settings && currentRoute.settings.role) {
if (!user.hasRole(currentRoute.settings.role) {
return resolve(next.cancel(new Redirect('')));
}
}

return resolve(next());
});
});
}

显然,您可以自由地以您喜欢的任何方式进行编码,这更多的是一个总体想法。

关于javascript - 使用 Aurelia 进行 Firebase Angular 色授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49878725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com