【发布时间】:2018-04-22 10:30:42
【问题描述】:
我正在尝试通过 PassportJS 限制对客户端 Angular 应用程序的访问。
基本上,我正在尝试将尝试访问该应用程序的任何人路由到重定向到身份验证提供程序以登录。但似乎角度路线覆盖了 NodeJS Express 路线。
因此,如果我转到“/”,它只会加载 Angular 应用程序而不是重定向它们。对于使用与角度路线相同的路线的每条路线,情况都是相同的。
server.js sn-p:
以下代码应将所有人重定向到登录 facebook。但它只是加载角度索引页面。
function checkAuthentication(req,res,next){
if(req.isAuthenticated()){
//if user is looged in, req.isAuthenticated() will return true
console.log("/ " + true);
res.sendFile(path.join(__dirname, 'dist/index.html'));
} else{
console.log("/ " + false);
res.redirect("/api/passport/login/facebook");
}
}
app.get('/*', checkAuthentication ,(req, res) => {
});
Angular index.html sn-p:
index.html 页面使用与 express 路由器中显示的相同路由。
<base href="/">
假设我将 index.html 基本 href 更改为使用 '/app/',如下所示:
<base href="/app/">
并在 express 中设置路由以将登录用户重定向到“/app/”,如下所示:
angular.route 示例:
app.use("/app", express.static(path.join(__dirname, 'dist')));
function checkAuthentication(req,res,next){
if(req.isAuthenticated()){
//if user is looged in, req.isAuthenticated() will return true
console.log("/app" + true);
res.sendFile(path.join(__dirname, '../../dist/index.html'));
} else{
console.log("/app" + false);
res.redirect("/api/passport/login/facebook");
}
}
router.get('/app', checkAuthentication, (req, res) => {
console.log("approuter hit");
});
module.exports = router;
然后直接转到“/app”。它仍然会加载 angular index.html 页面,而不会将您重定向到登录。但是如果您转到“/”,它会将您重定向到登录。
如何停止 Angular 覆盖我的 NodeJS express 路线?
更新:
app.route.js sn-p:
import { Routes } from '@angular/router';
import { HomeComponent } from './home';
export const ROUTES: Routes = [
{ path: '', component: HomeComponent },
];
【问题讨论】:
-
能不能先贴出角度路线的详细信息?
-
@mohituprim 我已经包含了角度路线的相关部分。如果您需要完整的,请询问。
标签: node.js angular express mean-stack