代码之家  ›  专栏  ›  技术社区  ›  Johan Rin

重定向后保留尝试的URL参数

  •  0
  • Johan Rin  · 技术社区  · 7 年前

    在Angular项目中,我只是实现了Amazon Cognito托管的UI来处理我的登录:

    Amazon Cognito Hosted UI

    现在,当我的用户单击我的登录按钮时,他们会被重定向到另一个URL https://[domain].amazoncognito.com/login?redirect_uri=http://localhost:4200/&response_type=code&client_id=[some id] 登录并重定向回我的角度URL以防成功。

    已存储尝试的URL并在登录后重新定向 . 因为我只是有角度,所以一切都很顺利。

    我的问题:

    更多信息

    这是调用我的Cognito托管UI的组件的摘录:

    导航栏.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();
      }
    

    导航栏.component.html

    <div class="navbar-item" *appShowIfSignedIn="false">
      <a class="button" (click)="signIn()">Sign in</a>
    </div>
    

    以下是我的AuthService摘录:

    @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);
      }
    

    谢谢你的帮助

    0 回复  |  直到 7 年前
        1
  •  0
  •   Johan Rin    7 年前

    我找到了一种方法,只需将尝试的URL参数存储在本地存储中。

    这样,即使从托管的UI重定向回来,我也可以检索我尝试的URL参数。

    推荐文章