0

As i have searched in the stackoverflow a similar question is also afloat, here is my

Login OTP Component:

    onSubmitValidOTP() {        
      this.authenticationService.ValidOTP(this.fo.OtpControl.value, username, role)
        .pipe(first())
        .subscribe(
          data => {
            console.log(data);            
          },
          error => {
            console.log(error);            
          });
  }

Auth Validate Service:

ValidOTP(OTP, UserName, type) {    
    return this.http.post<any>(`Staff/ValidateOTP`, { OTP, UserName, type })
      .pipe(map(bool => {        
        return bool;
      }));
  }

Staff Controller:

[AllowAnonymous]
        [HttpPost("ValidateOTP")]
        internal IActionResult ValidOTP(string OTP, string UserName, string type)
        {
            bool Result = false;
            if (type == "SuperAdmin")
            {
                Users _Users = _Context.Users.FirstOrDefault(j => j.Username == UserName);
                if (_Users != null)
                {
                    
                }
            }
            else
            {
                Staff _Staff = _Context.Staffs.FirstOrDefault(j => j.Username == UserName);
                if (_Staff != null)
                {
                    
                }
            }
            return Ok(new { Result = Result });
        } 

Here is my error which is shown on the chrome developer console.
validate Otp

Update:
Here is my login.ts below

import { AlertService, AuthenticationService } from '../_services';
import { first } from 'rxjs/operators';
@Component({
  templateUrl: './login.html'
})

export class LoginComponent implements OnInit {      
  returnUrl: string;
  constructor(private Router: Router,        
    private route: ActivatedRoute,
    private router: Router,
    private authenticationService: AuthenticationService,    
  ) {
    this.pageSettings.pageEmpty = true;
    if (this.authenticationService.currentUserValue) {
      this.router.navigate(['/']);
    }
  }    

  get f() { return this.loginForm.controls; }  
  onSubmit() {
    this.authenticationService.login(this.f.LoginUserNameControl.value, this.f.PasswordUserNameControl.value)
      .pipe(first())
      .subscribe(
        data => {
          if (data.validIp) {
            this.router.navigate([this.returnUrl]);
          }
          else {
            this.FormSubmitBool = false;
            this.VerifyOTP = true;
          }
        },
        error => {
          console.log(error);
          this.alertService.error(error);
          this.loading = false;
        });
  }    

  ValidOTP(OTP, UserName, type) {    
    return this.http.post<any>(`Staff/ValidateOTP`, { OTP, UserName, type })
      .pipe(map(bool => {        
        return bool;
      }));

Its the login.ts file which i have, i have trimmed it also so that you can understand it better in a way.

Update 2:
here is my authentication.service.ts below

    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { BehaviorSubject, Observable } from 'rxjs';
    import { map } from 'rxjs/operators';
    import { User } from '../_models';
    
    @Injectable({ providedIn: 'root' })
    export class AuthenticationService {
      private currentUserSubject: BehaviorSubject<User>;  
      public currentUser: Observable<User>;
    
      constructor(private http: HttpClient) {
        this.currentUserSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem('currentUser')));
        this.currentUser = this.currentUserSubject.asObservable();
      }
    
      public get currentUserValue(): User {
        return this.currentUserSubject.value;
      }
    
      login(username, password) {
{ username, password })
`Users/authenticate`, { username, password })
        return this.http.post<any>(`Staff/authenticate`, { username, password })
          .pipe(map(user => {
            localStorage.setItem('currentUser', JSON.stringify(user));
            this.currentUserSubject.next(user);
            return user;
          }));
      }
    
      ValidOTP(OTP, UserName, type) {
        return this.http.post<any>(`Staff/ValidateOTP`, { OTP, UserName, type })
          .pipe(map(bool => {        

            return bool;
          }));
      }
    
      logout() {
        // remove user from local storage and set current user to null
        localStorage.removeItem('currentUser');
        this.currentUserSubject.next(null);
      }
    }

Update 3:
Here is the update again which shows the console.log error.

onSubmitValidOTP() {        
    let IsUpdated = this.authenticationService.ValidOTP(this.fo.OtpControl.value, JSON.parse(localStorage.getItem('currentUser')).username, JSON.parse(localStorage.getItem('currentUser')).role)
    if (IsUpdated) {
      console.log(IsUpdated);
      IsUpdated
        .pipe(first())
        .subscribe(
          data => {
            console.log(data);            
          },
          error => {
            console.log(error);            
          });

    }
    else {
      this.InvalidOtp = false;
    }
  }
10
  • Post login.ts code Commented Jul 26, 2020 at 19:15
  • @Sajeetharan i have updated the code for the reference and also trimmed it a bit. Commented Jul 26, 2020 at 19:36
  • Can you please tell elaborate ? Your service getting 404. I think your are trying to get message property inside object like this yourDataObject.message. Commented Jul 26, 2020 at 19:57
  • @karthicvel i have updated the authentication.service.ts so that can help you understand the situation i have. Commented Jul 26, 2020 at 20:11
  • The code you have posted does not have the statement that tries to access a property named message Commented Jul 26, 2020 at 22:37

1 Answer 1

1

I suppose you are using different project in for angular and web api so as i see that you have used.

[AllowAnonymous]
[HttpPost("ValidateOTP")]
internal IActionResult ValidOTP(string OTP, string UserName, string type)

Please change internal into public as error shown above by you is 404 message for zone.js.

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

1 Comment

nice catch for the solution.

Your Answer

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