Issue
I have an angular app that is using AWS Amplify. I am calling a function if the user is authenticated. It works fine everytime unless you are a first time login. Then when you sign in with temp credentials and then enter in your new password you will not sign in but it will call the function infinite times. this is how I am calling the function:
`<ng-container *ngIf="authenticator.authStatus === 'authenticated'">
{{ callLoginFunction() }}
</ng-container>`
and this is the function in the ts file:
` callLoginFunction() {
this.router.navigateByUrl('dashboard')
const user = {
username: this.authenticator.user?.username || '', // Use a default value if the username is undefined
tokenPromise: this.JwtService.retrieveToken()
}
this.onLoginFormSubmit(user);
}`
I have tried adding return statements and different ways to fix it. I have went down the rabbit hole of chatGPT but have gotten nowhere. Thanks in advance.
I have found out now that the only way it works is if I do this but this is not really what I want : `
<ng-container *ngIf="authenticator.authStatus === 'authenticated'">
<button (click)="callLoginFunction()"></button>
</ng-container>
`
Solution
I finally found the answer to this... I used amplify Hub. I have this now in the ts file.
ngOnInit() {
Hub.listen('auth', (data) => {
if (data.payload.event === 'signIn') {
// Call your function when the user signs in
this.callLoginFunction();
}
});
}
Answered By - austinsmith33
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.