0

I have a problem trying to display a loading message linked to the server response, I managed to do it right for the logout method but I don't know how to do it for the login. Here is the code for my account service.

login(email: string, password: string) {
        return this.http
            .post<User>(
                environment.apiUrl + '/auth/login',
                {
                    email: email,
                    password: password
                })
            .pipe(map(user => {
                // store user details and jwt token in local storage to keep user logged in between page refreshes
                localStorage.setItem('scrb-user', JSON.stringify(user));
                this.userSubject.next(user);
                return user;
            }));
    }

    signup(user: User) {
        return this.http
            .post<User>(
                environment.apiUrl + '/auth/signup',
                user
            );
    }

    

    logout() {
        this.message
            .loading('Action in progress', { nzDuration: 2000 })
            .onClose!.pipe(
                concatMap(() => this.message.success('Loading finished', { nzDuration: 2000 }).onClose!),
                // concatMap(() => this.message.info('Loading finished is finished', { nzDuration: 2000 }).onClose!)
            )
            .subscribe(() => {
                localStorage.removeItem('scrb-user');
                this.userSubject.next(null);
                this.router.navigate(['/account/login']);
                console.log('All completed!');
            });
    }
}

1 Answer 1

1

You can use a variable. Example:

login.component.html

      //...Your form
      <button type="submit" class="btn btn-primary shadow-none" #button>
        <span *ngIf="!this.isLoading">Login</span>
        //Bootstrap Spinner
        <div class="spinner-border spinner-border-sm text-light" role="status" *ngIf="this.isLoading">
        </div>
      </button>

login.component.ts

  sendLogin(form: NgForm) {
    this.isLoading = true;
    this.loginService
      //Your Login Method (I saved it in other file)
      .postLogin(form.value.username, form.value.password)
      .subscribe(
      (data) => {
         this.isLoading = false;
         this.router.navigateByUrl('index');
       },
       (err) => {
         this.isLoading = false;
         alert(err);
       })
       form.reset();
    }
  );

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.