简体   繁体   English

从后端加载 Angular 应用程序

[英]Loading an Angular App from the Backend

I want to check if a user is authenticated from the backend BEFORE anything else has loaded in my angular app, so that no source code has loaded.我想在我的 angular 应用程序中加载任何其他内容之前检查用户是否从后端进行了身份验证,以便没有加载源代码。 The initial request to be sent to the back end will be to see whether the user is authenticated.发送到后端的初始请求将是查看用户是否通过身份验证。 If so, the app will be loaded.如果是这样,应用程序将被加载。

Alternatively, I would like to know how I could have my backend check authentication when the page is requested, and send different content depending on whether the user is logged in or not.或者,我想知道如何在请求页面时让我的后端检查身份验证,并根据用户是否登录发送不同的内容。

How can I best accomplish this?我怎样才能最好地做到这一点?

app.use(function(req, res, next) {
  console.log(req);
 next();
});

app.all('*', function(req, res, next) {
  console.log(req);
  res.send('hey');
})

Why don't these work in my node app?为什么这些在我的节点应用程序中不起作用?

If you're using a render engine like jade.如果您使用的是像 jade 这样的渲染引擎。 In your jade template that loads you can embed angular and embed the ng-view on said jade template.在加载的玉石模板中,您可以嵌入角度并在所述玉石模板上嵌入 ng-view。

So you have the server handle auth using a jade template tk render your open/public page and then authenticate.因此,您让服务器使用 jade 模板 tk 来处理身份验证,从而呈现您的开放/公共页面,然后进行身份验证。 Once they pass your Auth test redirect the page to the jade template which has the angular on it.一旦他们通过您的 Auth 测试,将页面重定向到具有角度的 jade 模板。 jade will then render the page, once the page loads angular gets called and your angular app will take over the page.然后 jade 将呈现页面,一旦页面加载 angular 被调用并且您的 angular 应用程序将接管该页面。

Just be careful if you use URIs that overlap on your server routes and your angular template URIs as they will trigger any middleware on those routes during the ajax call.如果您使用与服务器路由和角度模板 URI 重叠的 URI,请小心,因为它们会在 ajax 调用期间触发这些路由上的任何中间件。

You could have your backend check authentication when the page is requested, and send different content depending on whether the user is logged in or not.您可以在请求页面时让后端检查身份验证,并根据用户是否登录发送不同的内容。

The other way is to resolve a service in your route config which checks authentication before the route is resolved.另一种方法是解析路由配置中的服务,该服务在解析路由之前检查身份验证。 https://docs.angularjs.org/api/ngRoute/provider/ $routeProvider https://docs.angularjs.org/api/ngRoute/provider/ $routeProvider

I give a very helpful tutorial about authentication with token ;我给出了一个关于使用令牌进行身份验证的非常有用的教程; in general the idea is use a interceptor that check if the user was authenticated, in case that the user was not authenticate redirect to default or login page一般来说,想法是使用拦截器检查用户是否已通过身份验证,以防用户未通过身份验证重定向到默认或登录页面

You need to authenticate your user and check his authorization somehow.您需要对您的用户进行身份验证并以某种方式检查他的授权。 For example, if you use session -based authentication (quite similar to the way PHP does sessions), you might have a /login site that renders the user's login page.例如,如果您使用基于会话的身份验证(非常类似于 PHP 进行会话的方式),您可能有一个/login站点来呈现用户的登录页面。 With a custom expressjs middleware you can then redirect unauthorized users or reply with a 401 Unauthorized / 403 Forbidden .使用自定义 expressjs 中间件,您可以重定向未经授权的用户或回复401 Unauthorized / 403 Forbidden

app.use(require('cookie-parser')());
app.use(require('express-session')());
// ...

app.post('/login', function (req, res, next) {
  if (credentialsAreOK(req) {
    req.session.authorized = true;
    res.redirect('/pageAfterLogin');
  } else {
    res.send(401);
  }
};

var checkAuthorization = function (req, res, next) {
  if (req.session && req.session.authorized) {
    return next();
  } else {
    res.redirect('/login');
    // or return res.send(401);
  }
};

app.get('/protectedSite', checkAuthorization);
// or
app.get('/protectedSite', checkAuthorization, protectedSiteHandler);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM