【问题标题】:Logout of all devices, NodeJS and Angular 6注销所有设备、NodeJS 和 Angular 6
【发布时间】:2019-04-13 09:47:27
【问题描述】:

我正在开发一个应用程序,NodeJs(连同 Express)将是服务器端,它将提供 RESTful 服务,而 Angular 6 将利用 RESTful API。

现在的问题是当用户更改密码时,如何从所有设备上注销用户。

我正在使用 JWT 进行身份验证。

【问题讨论】:

  • 所有设备是什么意思?您是在移动设备、桌面设备...还是多个设备上登录用户?
  • 如何存储登录令牌?如果它在数据库中,您应该在用户更改密码后删除该用户的所有令牌
  • 目前NodeJS返回一个JWT Token,我保存在localStorage中,不知道最佳实践是什么
  • 根据我对您现在已删除的问题的评论,读者通常会鼓励新社区成员尽可能详细地编写问题。这个问题似乎也遇到了同样的问题——它太简短了,没有显示导致问题的代码,因此没有得到很好的接受。
  • 请注意,这里确实有可能获得好评——您只需要先让读者相信您已经做出了扎实的个人努力。我很欣赏这与您之前可能在网络上获得帮助的其他地方不同,但人们想要来这里的原因是社区强制执行的高质量标准。

标签: node.js angular jwt


【解决方案1】:

经过长时间的搜索,我找到了一个解决方案,

您应该在用户表中有一个 updated_at 字段,该字段存储用户上次更新他/她的详细信息的时间, 您必须将此字段用作 JWT 的加密字符串,因此当用户更新信息时,之前发布的令牌被视为无效,因此用户将无法执行任何操作,并且他/她必须更新令牌

【讨论】:

    【解决方案2】:

    除非用户自愿选择从所有设备上注销,否则在更改密码时注销用户是不好的做法:-

    一旦用户注销,然后清除本地存储并重定向到登录为:-

    只需在用户更新密码时更改授权令牌并检查令牌对于每个请求是否仍然有效。如果无效,则给出 401 错误,然后重定向到登录页面。

    logout(){
    /* clear your localStorage token /*
    // redirect to login page 
    }
    

    interceptor.ts

      import { Injectable } from '@angular/core';
        import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
        import { Observable } from 'rxjs';
    
        @Injectable()
        export class JwtInterceptor implements HttpInterceptor {
            intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
                // add authorization header with jwt token if available
                let currentUser = JSON.parse(localStorage.getItem('currentUser'));
                if (currentUser && currentUser.token) {
                    request = request.clone({
                        setHeaders: { 
                            Authorization: `Bearer ${currentUser.token}`
                        }
                    });
                }
    
                return next.handle(request).do((event: HttpEvent<any>) => {
          if (event instanceof HttpResponse) {
            // do stuff with response if you want
          }
        }, (err: any) => {
          if (err instanceof HttpErrorResponse) {
            if (err.status === 401) {
              // redirect to the login route
              // or show a modal showing, we are redirecting to you login page.
            }
          }
        });
      }
    }
    

    参考链接:-https://medium.com/@ryanchenkie_40935/angular-authentication-using-the-http-client-and-http-interceptors-2f9d1540eb8

    http://jasonwatmore.com/post/2018/05/23/angular-6-jwt-authentication-example-tutorial

    【讨论】:

    • 感谢您的回复,但让我讨论一个“用例” - 用户在多个地方(chrome、safari、mozilla)登录,然后他更改了 chrome 中的密码,现在是 safari 中的令牌并且 Mozilla 应该不再被接受,如何处理这个
    • 什么是“用例”?
    • 那么,拦截器在这里做什么,它对每个请求都使用授权令牌。因此,在后端,如果处理了一些东西来检查所请求的授权,即如果授权令牌不匹配,那么它将发送 401 错误。即使刷新浏览器,这也有效。因此,在其他设备上,如果刷新浏览器,则会出现 401 错误。
    • 你甚至可以看看socket.io。看看stackoverflow.com/questions/30814330/…
    猜你喜欢
    • 2022-10-09
    • 2022-07-20
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 2022-11-10
    • 2015-09-01
    • 2021-08-12
    • 2021-04-17
    相关资源
    最近更新 更多