Logout of all devices, NodeJS and Angular 6
up vote
-3
down vote
favorite
I'm developing an application, the NodeJs (along with express) will be server side which will provide RESTfull service and Angular 6 will utilize the RESTfull API.
Now the problem is when a user change the password, then how to logout the user from all devices.
I'm using JWT for authentication.
node.js angular jwt
add a comment |
up vote
-3
down vote
favorite
I'm developing an application, the NodeJs (along with express) will be server side which will provide RESTfull service and Angular 6 will utilize the RESTfull API.
Now the problem is when a user change the password, then how to logout the user from all devices.
I'm using JWT for authentication.
node.js angular jwt
What do you mean by all devices? Do you log user in mobile, desktop... or multiple devices?
– Maihan Nijat
Nov 9 at 19:37
How do you store login tokens? If it's in the database, you should remove all tokens from this user after the user has changed his password
– Variable
Nov 9 at 19:45
Currently, the NodeJS return a JWT Token, which I save in the localStorage, I don't know whats the best practice
– AbdulRehman
Nov 9 at 19:54
@Variable If i store the token in database, then should I have to check it (perform database query) at each request, which is a costly job
– AbdulRehman
Nov 9 at 20:00
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I'm developing an application, the NodeJs (along with express) will be server side which will provide RESTfull service and Angular 6 will utilize the RESTfull API.
Now the problem is when a user change the password, then how to logout the user from all devices.
I'm using JWT for authentication.
node.js angular jwt
I'm developing an application, the NodeJs (along with express) will be server side which will provide RESTfull service and Angular 6 will utilize the RESTfull API.
Now the problem is when a user change the password, then how to logout the user from all devices.
I'm using JWT for authentication.
node.js angular jwt
node.js angular jwt
asked Nov 9 at 19:36
AbdulRehman
61
61
What do you mean by all devices? Do you log user in mobile, desktop... or multiple devices?
– Maihan Nijat
Nov 9 at 19:37
How do you store login tokens? If it's in the database, you should remove all tokens from this user after the user has changed his password
– Variable
Nov 9 at 19:45
Currently, the NodeJS return a JWT Token, which I save in the localStorage, I don't know whats the best practice
– AbdulRehman
Nov 9 at 19:54
@Variable If i store the token in database, then should I have to check it (perform database query) at each request, which is a costly job
– AbdulRehman
Nov 9 at 20:00
add a comment |
What do you mean by all devices? Do you log user in mobile, desktop... or multiple devices?
– Maihan Nijat
Nov 9 at 19:37
How do you store login tokens? If it's in the database, you should remove all tokens from this user after the user has changed his password
– Variable
Nov 9 at 19:45
Currently, the NodeJS return a JWT Token, which I save in the localStorage, I don't know whats the best practice
– AbdulRehman
Nov 9 at 19:54
@Variable If i store the token in database, then should I have to check it (perform database query) at each request, which is a costly job
– AbdulRehman
Nov 9 at 20:00
What do you mean by all devices? Do you log user in mobile, desktop... or multiple devices?
– Maihan Nijat
Nov 9 at 19:37
What do you mean by all devices? Do you log user in mobile, desktop... or multiple devices?
– Maihan Nijat
Nov 9 at 19:37
How do you store login tokens? If it's in the database, you should remove all tokens from this user after the user has changed his password
– Variable
Nov 9 at 19:45
How do you store login tokens? If it's in the database, you should remove all tokens from this user after the user has changed his password
– Variable
Nov 9 at 19:45
Currently, the NodeJS return a JWT Token, which I save in the localStorage, I don't know whats the best practice
– AbdulRehman
Nov 9 at 19:54
Currently, the NodeJS return a JWT Token, which I save in the localStorage, I don't know whats the best practice
– AbdulRehman
Nov 9 at 19:54
@Variable If i store the token in database, then should I have to check it (perform database query) at each request, which is a costly job
– AbdulRehman
Nov 9 at 20:00
@Variable If i store the token in database, then should I have to check it (perform database query) at each request, which is a costly job
– AbdulRehman
Nov 9 at 20:00
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
It is bad practise to logout the user when it changes password unless user voluntarily opts to logout from all the devices:-
Once the user logout , then clears localstorage and redirect to login as:-
Just change the authorization token when user updates the password and check that token is still valid for each request. If not valid, gives 401 error and then redirect to login page.
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.
);
Refer Link:- 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
Thanks for your response, but let me discuss a 'use case' - A user login in multiple places (chrome, safari, mozilla), he then changes the password in chrome, Now the tokens in safari and mozilla should be no longer accepted, how to handle this
– AbdulRehman
Nov 10 at 13:47
What is 'use case' ?
– Ahmad mnzr
Nov 10 at 13:50
So, what interceptor is doing here, it is using Authorization token with each and every request. Thus, in backend if there is something handled to check requested Authorization taken i.e. if Authorization token not matches then it will send 401 error . This works even if browser is refreshed. Thus , in other devices if browser is refreshed, then it will give 401 error.
– Ahmad mnzr
Nov 10 at 13:59
You can even have a look at socket.io. Have a look at stackoverflow.com/questions/30814330/…
– Ahmad mnzr
Nov 10 at 14:04
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
It is bad practise to logout the user when it changes password unless user voluntarily opts to logout from all the devices:-
Once the user logout , then clears localstorage and redirect to login as:-
Just change the authorization token when user updates the password and check that token is still valid for each request. If not valid, gives 401 error and then redirect to login page.
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.
);
Refer Link:- 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
Thanks for your response, but let me discuss a 'use case' - A user login in multiple places (chrome, safari, mozilla), he then changes the password in chrome, Now the tokens in safari and mozilla should be no longer accepted, how to handle this
– AbdulRehman
Nov 10 at 13:47
What is 'use case' ?
– Ahmad mnzr
Nov 10 at 13:50
So, what interceptor is doing here, it is using Authorization token with each and every request. Thus, in backend if there is something handled to check requested Authorization taken i.e. if Authorization token not matches then it will send 401 error . This works even if browser is refreshed. Thus , in other devices if browser is refreshed, then it will give 401 error.
– Ahmad mnzr
Nov 10 at 13:59
You can even have a look at socket.io. Have a look at stackoverflow.com/questions/30814330/…
– Ahmad mnzr
Nov 10 at 14:04
add a comment |
up vote
0
down vote
It is bad practise to logout the user when it changes password unless user voluntarily opts to logout from all the devices:-
Once the user logout , then clears localstorage and redirect to login as:-
Just change the authorization token when user updates the password and check that token is still valid for each request. If not valid, gives 401 error and then redirect to login page.
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.
);
Refer Link:- 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
Thanks for your response, but let me discuss a 'use case' - A user login in multiple places (chrome, safari, mozilla), he then changes the password in chrome, Now the tokens in safari and mozilla should be no longer accepted, how to handle this
– AbdulRehman
Nov 10 at 13:47
What is 'use case' ?
– Ahmad mnzr
Nov 10 at 13:50
So, what interceptor is doing here, it is using Authorization token with each and every request. Thus, in backend if there is something handled to check requested Authorization taken i.e. if Authorization token not matches then it will send 401 error . This works even if browser is refreshed. Thus , in other devices if browser is refreshed, then it will give 401 error.
– Ahmad mnzr
Nov 10 at 13:59
You can even have a look at socket.io. Have a look at stackoverflow.com/questions/30814330/…
– Ahmad mnzr
Nov 10 at 14:04
add a comment |
up vote
0
down vote
up vote
0
down vote
It is bad practise to logout the user when it changes password unless user voluntarily opts to logout from all the devices:-
Once the user logout , then clears localstorage and redirect to login as:-
Just change the authorization token when user updates the password and check that token is still valid for each request. If not valid, gives 401 error and then redirect to login page.
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.
);
Refer Link:- 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
It is bad practise to logout the user when it changes password unless user voluntarily opts to logout from all the devices:-
Once the user logout , then clears localstorage and redirect to login as:-
Just change the authorization token when user updates the password and check that token is still valid for each request. If not valid, gives 401 error and then redirect to login page.
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.
);
Refer Link:- 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
edited Nov 9 at 21:21
answered Nov 9 at 21:07
Ahmad mnzr
818219
818219
Thanks for your response, but let me discuss a 'use case' - A user login in multiple places (chrome, safari, mozilla), he then changes the password in chrome, Now the tokens in safari and mozilla should be no longer accepted, how to handle this
– AbdulRehman
Nov 10 at 13:47
What is 'use case' ?
– Ahmad mnzr
Nov 10 at 13:50
So, what interceptor is doing here, it is using Authorization token with each and every request. Thus, in backend if there is something handled to check requested Authorization taken i.e. if Authorization token not matches then it will send 401 error . This works even if browser is refreshed. Thus , in other devices if browser is refreshed, then it will give 401 error.
– Ahmad mnzr
Nov 10 at 13:59
You can even have a look at socket.io. Have a look at stackoverflow.com/questions/30814330/…
– Ahmad mnzr
Nov 10 at 14:04
add a comment |
Thanks for your response, but let me discuss a 'use case' - A user login in multiple places (chrome, safari, mozilla), he then changes the password in chrome, Now the tokens in safari and mozilla should be no longer accepted, how to handle this
– AbdulRehman
Nov 10 at 13:47
What is 'use case' ?
– Ahmad mnzr
Nov 10 at 13:50
So, what interceptor is doing here, it is using Authorization token with each and every request. Thus, in backend if there is something handled to check requested Authorization taken i.e. if Authorization token not matches then it will send 401 error . This works even if browser is refreshed. Thus , in other devices if browser is refreshed, then it will give 401 error.
– Ahmad mnzr
Nov 10 at 13:59
You can even have a look at socket.io. Have a look at stackoverflow.com/questions/30814330/…
– Ahmad mnzr
Nov 10 at 14:04
Thanks for your response, but let me discuss a 'use case' - A user login in multiple places (chrome, safari, mozilla), he then changes the password in chrome, Now the tokens in safari and mozilla should be no longer accepted, how to handle this
– AbdulRehman
Nov 10 at 13:47
Thanks for your response, but let me discuss a 'use case' - A user login in multiple places (chrome, safari, mozilla), he then changes the password in chrome, Now the tokens in safari and mozilla should be no longer accepted, how to handle this
– AbdulRehman
Nov 10 at 13:47
What is 'use case' ?
– Ahmad mnzr
Nov 10 at 13:50
What is 'use case' ?
– Ahmad mnzr
Nov 10 at 13:50
So, what interceptor is doing here, it is using Authorization token with each and every request. Thus, in backend if there is something handled to check requested Authorization taken i.e. if Authorization token not matches then it will send 401 error . This works even if browser is refreshed. Thus , in other devices if browser is refreshed, then it will give 401 error.
– Ahmad mnzr
Nov 10 at 13:59
So, what interceptor is doing here, it is using Authorization token with each and every request. Thus, in backend if there is something handled to check requested Authorization taken i.e. if Authorization token not matches then it will send 401 error . This works even if browser is refreshed. Thus , in other devices if browser is refreshed, then it will give 401 error.
– Ahmad mnzr
Nov 10 at 13:59
You can even have a look at socket.io. Have a look at stackoverflow.com/questions/30814330/…
– Ahmad mnzr
Nov 10 at 14:04
You can even have a look at socket.io. Have a look at stackoverflow.com/questions/30814330/…
– Ahmad mnzr
Nov 10 at 14:04
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232243%2flogout-of-all-devices-nodejs-and-angular-6%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean by all devices? Do you log user in mobile, desktop... or multiple devices?
– Maihan Nijat
Nov 9 at 19:37
How do you store login tokens? If it's in the database, you should remove all tokens from this user after the user has changed his password
– Variable
Nov 9 at 19:45
Currently, the NodeJS return a JWT Token, which I save in the localStorage, I don't know whats the best practice
– AbdulRehman
Nov 9 at 19:54
@Variable If i store the token in database, then should I have to check it (perform database query) at each request, which is a costly job
– AbdulRehman
Nov 9 at 20:00