重定向后保留尝试的 URL 参数
Keep attempted URL parameter after a redirection
在我的 Angular 项目中,我刚刚实施了 Amazon Cognito Hosted UI 来处理我的登录,如下所示:
现在,当我的用户点击我的登录按钮时,他们会被重定向到另一个 URL,例如 https://[domain].amazoncognito.com/login?redirect_uri=http://localhost:4200/&response_type=code&client_id=[some id]
以登录并重定向回我的 Angular URL 如果成功。
在实施之前,我有一个简单的登录页面和一个 authguard 来保护我的路由,存储了尝试的 URL 并在登录后重定向回来 .而且因为我只上了 Angular,所以一切都很顺利。
现在,由于 Cognito Hosted UI 的运行,当我的用户被重定向到 Hosted UI 时,尝试的 URL 参数丢失了。
我的问题:
有没有办法在这种重定向下保留我尝试的 URL 参数?
更多信息
我不再有登录组件,因为它由 Cognito Hosted UI 处理。
这是调用我的 Cognito Hosted UI 的组件的摘录:
navbar.component.ts
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.scss']
})
export class NavbarComponent implements OnDestroy, OnInit {
[...]
constructor(
@Inject(LOCALE_ID) protected localeId: string,
private router: Router,
private authService: AuthService,
private usersService: UsersService
) { }
[...]
signIn() {
this.authService.signIn();
}
}
navbar.component.html
<div class="navbar-item" *appShowIfSignedIn="false">
<a class="button" (click)="signIn()">Sign in</a>
</div>
这是我的 AuthService 的摘录:
auth.service.ts
@Injectable({ providedIn: 'root' })
export class AuthService {
private url = 'https://' + '[domain].auth.eu-central-1.amazoncognito.com' + '/login?redirect_uri=' + 'http://localhost:4200/' + '&response_type=' + 'code' + '&client_id=' + '[some id]';
constructor(
private ngZone: NgZone,
private authStore: AuthStore
) {
Hub.listen('auth', this, 'AuthCodeListener');
}
onHubCapsule(capsule: any) {
const { channel, payload } = capsule;
if (channel === 'auth' && payload.event === 'signIn') {
this.ngZone.run(() => {
this.currentAuthenticatedUser().subscribe(
(user: any) => {
const isSignedIn = true;
const accessToken = user.signInUserSession.accessToken.jwtToken;
const sub = user.signInUserSession.accessToken.payload['sub'];
this.authStore.update({
signedIn: isSignedIn,
accessToken: accessToken,
sub: sub
});
}
);
});
}
}
signIn() {
window.location.assign(this.url);
}
感谢您的帮助
我找到了一种方法,只需将我尝试的 URL 参数存储在我的本地存储中即可。
这样,即使从 Hosted UI.
重定向回来,我也可以检索我尝试的 URL 参数
在我的 Angular 项目中,我刚刚实施了 Amazon Cognito Hosted UI 来处理我的登录,如下所示:
现在,当我的用户点击我的登录按钮时,他们会被重定向到另一个 URL,例如 https://[domain].amazoncognito.com/login?redirect_uri=http://localhost:4200/&response_type=code&client_id=[some id]
以登录并重定向回我的 Angular URL 如果成功。
在实施之前,我有一个简单的登录页面和一个 authguard 来保护我的路由,存储了尝试的 URL 并在登录后重定向回来 .而且因为我只上了 Angular,所以一切都很顺利。
现在,由于 Cognito Hosted UI 的运行,当我的用户被重定向到 Hosted UI 时,尝试的 URL 参数丢失了。
我的问题: 有没有办法在这种重定向下保留我尝试的 URL 参数?
更多信息
我不再有登录组件,因为它由 Cognito Hosted UI 处理。
这是调用我的 Cognito Hosted UI 的组件的摘录:
navbar.component.ts
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.scss']
})
export class NavbarComponent implements OnDestroy, OnInit {
[...]
constructor(
@Inject(LOCALE_ID) protected localeId: string,
private router: Router,
private authService: AuthService,
private usersService: UsersService
) { }
[...]
signIn() {
this.authService.signIn();
}
}
navbar.component.html
<div class="navbar-item" *appShowIfSignedIn="false">
<a class="button" (click)="signIn()">Sign in</a>
</div>
这是我的 AuthService 的摘录:
auth.service.ts
@Injectable({ providedIn: 'root' })
export class AuthService {
private url = 'https://' + '[domain].auth.eu-central-1.amazoncognito.com' + '/login?redirect_uri=' + 'http://localhost:4200/' + '&response_type=' + 'code' + '&client_id=' + '[some id]';
constructor(
private ngZone: NgZone,
private authStore: AuthStore
) {
Hub.listen('auth', this, 'AuthCodeListener');
}
onHubCapsule(capsule: any) {
const { channel, payload } = capsule;
if (channel === 'auth' && payload.event === 'signIn') {
this.ngZone.run(() => {
this.currentAuthenticatedUser().subscribe(
(user: any) => {
const isSignedIn = true;
const accessToken = user.signInUserSession.accessToken.jwtToken;
const sub = user.signInUserSession.accessToken.payload['sub'];
this.authStore.update({
signedIn: isSignedIn,
accessToken: accessToken,
sub: sub
});
}
);
});
}
}
signIn() {
window.location.assign(this.url);
}
感谢您的帮助
我找到了一种方法,只需将我尝试的 URL 参数存储在我的本地存储中即可。
这样,即使从 Hosted UI.
重定向回来,我也可以检索我尝试的 URL 参数