AngularJS 有用户登录的网站

AngularJS website with user logins

我想制作一个人们可以注册和登录的网站,但如果我是对的。每次用户刷新页面他都会被注销?我该如何预防?

例如:

User logged in ➞ does his stuff on the website ➞ refreshes the page ➞ is logged out

这将是对广泛主题的简短回答,但要回答您的问题:

首先,您需要在 back-end 上使用某种身份验证服务来验证用户。成功登录后,它将 return HTTP 200 和用户信息:

{
  "user":{
    "firstname": "MyFirstname"
    "lastname": "MyLastname"
  } 
}

用户信息(可能还有某种令牌或 session-id)将被存储 client-side。这将保持数据持久化(即使浏览器 window 已关闭)并且您始终可以从应用程序的其余部分访问它。

angular.module('app').factory('AuthenticationService', function() {
  // call the back-end login service
  // handle login, logout, authentication and authorization 
  // storing the userinfo in localstorage, cookie etc.
});