[英]Navigate to the same component after the session expire
I have a token based API where a session is valid for 10 mins if the user is idle for more than 10 mins token will be changed and error response will be received from the API where the user will be redirected to the login page.我有一个基于令牌的 API,如果用户空闲超过 10 分钟,会话有效期为 10 分钟,令牌将被更改,并且将从用户将被重定向到登录页面的 API 接收到错误响应。
Currently if the user login again it redirects to the main page, but my requirement is the user should be able to login to the same page where he left before.目前,如果用户再次登录,它会重定向到主页,但我的要求是用户应该能够登录到他之前离开的同一页面。
can i use some sessions to store the value of the router where the user is active and after the session is terminated i can use the same session value and navigate to the same page where he left?.我可以使用一些会话来存储用户处于活动状态的路由器的值,并且在会话终止后我可以使用相同的会话值并导航到他离开的同一页面吗?
for a simulation i have created a application Stackblitz where i am using the setTimout function and navigating to login page after 10 seconds.为了进行模拟,我创建了一个应用程序Stackblitz ,我在其中使用 setTimout 函数并在 10 秒后导航到登录页面。
Suppose say the user clicks login it redirects to the user page and from there he navigates to dashboard component after 10 seconds it redirects to the login page if i click login again it want to navigate to the same component where user left假设说用户单击登录它重定向到用户页面并从那里他导航到仪表板组件 10 秒后它重定向到登录页面如果我再次单击登录它想导航到用户离开的同一个组件
Login Page登录页面
<div id="login">
<div class="container">
<div id="login-row" class="row justify-content-center align-items-center">
<div id="login-column" class="col-md-6">
<div id="login-box" class="col-md-12">
<form id="login-form" class="form" action="" method="post">
<h3 class="text-center text-info">Login</h3>
<div class="form-group">
<label for="username" class="text-info">Username:</label><br>
<input [(ngModel)]= "userName" type="text" name="username" id="username" class="form-control">
</div>
<div class="form-group">
<label for="password" class="text-info">Password:</label><br>
<input [(ngModel)]= "password" type="password" name="password" id="password" class="form-control">
</div>
<div class="form-group">
<input type="submit" (click)="formValidation()" name="submit" class="btn btn-info btn-md" value="submit">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
Login.component.ts登录.component.ts
formValidation(){
console.log(this.userName);
console.log(this.password);
this.router.navigate(['user'])
}
Suggest me the better way to handle the router navigation after the session is terminated.建议我在会话终止后处理路由器导航的更好方法。 Link to edit Stackblitz
编辑Stackblitz的链接
I'm doing it like this.我是这样做的。 When the error response is received from api i redirect the user to login page with this.router.navigate(['/login'], { queryParams: { returnUrl: state .url }});
当从 api 收到错误响应时,我将用户重定向到登录页面 this.router.navigate(['/login'], { queryParams: { returnUrl: state .url }});
(" state " is RouterStateSnapshot from @angular/router) (“状态”是来自@angular/router 的 RouterStateSnapshot)
and on the login page i get the return url from query with:在登录页面上,我从查询中获取返回 url:
this.returnUrl = this.route.snapshot.queryParams['returnUrl']; this.returnUrl = this.route.snapshot.queryParams['returnUrl'];
(" route " is ActivatedRoute from @angular/router) (“路由”是来自@angular/router 的 ActivatedRoute)
and on successful login i redirect the user: this.router.navigate([this.returnUrl]);成功登录后,我重定向用户:this.router.navigate([this.returnUrl]);
(" router " is Router from @angular/router) (“路由器”是来自@angular/router 的路由器)
I have a token based API where a session is valid for 10 mins if the user is idle for more than 10 mins token will be changed and error response will be received from the API where the user will be redirected to the login page.我有一个基于令牌的 API,如果用户空闲超过 10 分钟,则会话有效期为 10 分钟,令牌将被更改,并且将从 API 收到错误响应,用户将被重定向到登录页面。
Currently if the user login again it redirects to the main page, but my requirement is the user should be able to login to the same page where he left before.目前,如果用户再次登录,它会重定向到主页,但我的要求是用户应该能够登录到他之前离开的页面。
can i use some sessions to store the value of the router where the user is active and after the session is terminated i can use the same session value and navigate to the same page where he left ?.我可以使用一些会话来存储用户处于活动状态的路由器的值吗?在会话终止后,我可以使用相同的会话值并导航到他离开的同一页面吗?。
for a simulation i have created a application Stackblitz where i am using the setTimout function and navigating to login page after 10 seconds.对于模拟,我创建了一个应用程序Stackblitz ,我在其中使用 setTimout 函数并在 10 秒后导航到登录页面。
Suppose say the user clicks login it redirects to the user page and from there he navigates to dashboard component after 10 seconds it redirects to the login page if i click login again it want to navigate to the same component where user left假设说用户点击登录它重定向到用户页面,然后他从那里导航到仪表板组件 10 秒后它重定向到登录页面如果我再次单击登录它想导航到用户离开的相同组件
Login Page登录页面
<div id="login">
<div class="container">
<div id="login-row" class="row justify-content-center align-items-center">
<div id="login-column" class="col-md-6">
<div id="login-box" class="col-md-12">
<form id="login-form" class="form" action="" method="post">
<h3 class="text-center text-info">Login</h3>
<div class="form-group">
<label for="username" class="text-info">Username:</label><br>
<input [(ngModel)]= "userName" type="text" name="username" id="username" class="form-control">
</div>
<div class="form-group">
<label for="password" class="text-info">Password:</label><br>
<input [(ngModel)]= "password" type="password" name="password" id="password" class="form-control">
</div>
<div class="form-group">
<input type="submit" (click)="formValidation()" name="submit" class="btn btn-info btn-md" value="submit">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
Login.component.ts登录.component.ts
formValidation(){
console.log(this.userName);
console.log(this.password);
this.router.navigate(['user'])
}
Suggest me the better way to handle the router navigation after the session is terminated.建议我在会话终止后处理路由器导航的更好方法。 Link to edit Stackblitz
链接到编辑Stackblitz
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.