I need to redirect a user with a form post after I receive a response from an http call that contains the url and a session token.
I am following this SO post
I keep getting cannot POST /login.
The action of my form is set to http://localhost:4200/login - not the updated action I receive from the http call.
My template:
<form ngNoForm
[formGroup]='formGroup'
[action]='websiteUrl'
method="post"
#redirectForm
>
<input type="hidden" name="SessionId" [value]='sessionId' />
</form>
LoginComponent
export class LoginComponent implements OnInit, AfterViewInit {
@ViewChild('redirectForm')formElement!: ElementRef;
sessionId: string = '';
websiteUrl: string = '';
formGroup = this.formBuilder.group({
SessionId: ''
});
constructor(
private partnerDomainService: PartnerDomainService,
private amplifyService: AmplifyService,
private httpClient: HttpClient,
private configurationService: ConfigurationService,
private formBuilder: FormBuilder
) {}
ngAfterViewInit(): void {
debugger;
this.formElement.nativeElement.submit();
}
async ngOnInit(): Promise<void> {
await this.getLoginData();
}
private async getLoginData(): Promise<void> {
const loginEndpoint = `${this.configurationService.loginApi}/api/authenticate/federatedlogin`;
const partnerDomain = this.partnerDomainService.getPartnerDomain();
const identityToken = await this.amplifyService.getIdentityToken(
partnerDomain
);
const headers: HttpHeaders = new HttpHeaders({
Authorization: `Bearer ${identityToken}`,
DomainName: partnerDomain,
});
this.httpClient
.post<LoginResponse>(loginEndpoint, null, { headers: headers })
.pipe(
tap((loginResponse: LoginResponse) => {
this.websiteUrl = loginResponse.WebsiteURL;
this.sessionId = loginResponse.SessionCookie;
})
)
.subscribe();
}
}
http://localhost:4200/login?Routefrom@angular/router? likethis.route.navigate(...)