Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
WingZer0o committed Mar 12, 2024
2 parents 2e1232e + a7e3be0 commit ae31fee
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/app/modules/login/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ 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]
],
password: ['',
[Validators.required, passwordValidator]]
});

public twoFAForm: FormGroup = this.formBuilder.group({
code: ['', [Validators.required, Validators.minLength(8)]]
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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']);
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,11 @@
</div>
}
</form>
<div class="form-check">
<input class="form-check-input" type="checkbox" [checked]="two2FAEnabled" (change)="handle2FAChange($event)">
<label class="form-check-label" for="flexCheckDefault">
2FA Enabled (Email)
</label>
</div>
</div>
</div>
34 changes: 34 additions & 0 deletions src/app/modules/user-home/user-settings/user-settings.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit ae31fee

Please sign in to comment.