diff --git a/src/app/modules/login/login/login.component.ts b/src/app/modules/login/login/login.component.ts index b71b110..8d41b23 100644 --- a/src/app/modules/login/login/login.component.ts +++ b/src/app/modules/login/login/login.component.ts @@ -22,6 +22,8 @@ export class LoginComponent implements OnInit { public isSubmitClicked: boolean = false; public isTwoFactorForm: boolean = false; public isTwoFactorFormSubmitted: boolean = false; + private userId: string; + public loginForm: FormGroup = this.formBuilder.group({ email: ['', [Validators.required, Validators.email] @@ -29,6 +31,7 @@ export class LoginComponent implements OnInit { password: ['', [Validators.required, passwordValidator]] }); + public twoFAForm: FormGroup = this.formBuilder.group({ code: ['', [Validators.required, Validators.minLength(8)]] }); @@ -57,7 +60,13 @@ export class LoginComponent implements OnInit { this.toastr.success("", response.message); this.isSubmitClicked = false; this.loginForm.reset(); - localStorage.setItem("token", response.token); + if (response.token) { + localStorage.setItem("token", response.token); + } + if (response.userId) { + this.userId = response.userId; + } + this.isTwoFactorForm = response.TwoFactorAuth; this.navbarEmitter.navbarEvents("user-logged-in"); // check if user has token stored if (response.twoFactorAuth === true) { @@ -100,10 +109,12 @@ export class LoginComponent implements OnInit { public twoFactorCodeSubmit(event: any): void { if (!this.isTwoFactorFormSubmitted && this.twoFAForm.valid) { this.isTwoFactorFormSubmitted = true; - let decodedToken = this.authGuard.getDecodedToken(); - let body = this.getPutTwoFAVerificationBody(decodedToken); - this.http.putAuthenticated(environment.apiUrl + "UserLogin/ValidateHotpCode", body).subscribe((response: any) => { + let body = this.getPutTwoFAVerificationBody(); + this.http.putAuthenticated("UserLogin/ValidateHotpCode", body).subscribe((response: any) => { this.isTwoFactorFormSubmitted = false; + if (response.token) { + localStorage.setItem("token", response.token); + } this.toastr.success(response.message, ""); setTimeout(() => { this.router.navigate(['/user-home']); @@ -115,9 +126,9 @@ export class LoginComponent implements OnInit { } } - private getPutTwoFAVerificationBody(decodedToken: any): object { + private getPutTwoFAVerificationBody(): object { return { - "UserId": decodedToken["id"], + "UserId": this.userId, "HotpCode": this.twoFAForm.value["code"], "UserAgent": this.userAgent.getUserAgent() } diff --git a/src/app/modules/user-home/user-settings/user-settings.component.html b/src/app/modules/user-home/user-settings/user-settings.component.html index 5bc2edc..a331722 100644 --- a/src/app/modules/user-home/user-settings/user-settings.component.html +++ b/src/app/modules/user-home/user-settings/user-settings.component.html @@ -65,5 +65,11 @@ } +
+ + +
\ No newline at end of file diff --git a/src/app/modules/user-home/user-settings/user-settings.component.ts b/src/app/modules/user-home/user-settings/user-settings.component.ts index 8a2fca5..68cb33d 100644 --- a/src/app/modules/user-home/user-settings/user-settings.component.ts +++ b/src/app/modules/user-home/user-settings/user-settings.component.ts @@ -16,6 +16,7 @@ export class UserSettingsComponent implements OnInit { public userPasswordForm: FormGroup; public isNewUsernameSubmitted: boolean = false; public isNewPasswordSubmitted: boolean = false; + public two2FAEnabled: boolean = false; constructor( private formBuilder: FormBuilder, @@ -37,8 +38,41 @@ export class UserSettingsComponent implements OnInit { confirmNewPassword: ['', [Validators.required, passwordValidator]], currentPassword: ['', [Validators.required, passwordValidator]] }); + + this.get2FAStatus(); + } + + private get2FAStatus(): void { + const url = environment.apiUrl + "TwoFA/Get2FAStatus"; + this.httpService.getAuthenticated(url).subscribe((response: any) => { + this.two2FAEnabled = response.result; + }); + } + + private change2FAToDisabled(): void { + const url = environment.apiUrl + "TwoFA/TurnOff2FA"; + this.httpService.putAuthenticated(url, null).subscribe((response: any) => { + this.two2FAEnabled = false; + }); + } + + private chang2FAToEnabled(): void { + const url = environment.apiUrl + "TwoFA/TurnOn2FA"; + this.httpService.putAuthenticated(url, null).subscribe((response: any) => { + this.two2FAEnabled = true; + }); } + public handle2FAChange(event: any) { + debugger; + let isChecked: boolean = event.target.checked; + if (isChecked) { + this.chang2FAToEnabled(); + } else { + this.change2FAToDisabled(); + } + } + public newUsernameSubmit(): void { if (this.userNameForm.valid) { this.isNewUsernameSubmitted = true;