Skip to content

Commit

Permalink
add disable2FA, reset recover codes functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
chsakell committed Aug 9, 2019
1 parent 4c69c35 commit fc2a616
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h3>Account management</h3>
<button type="button" (click)="setupAuthenticator()" class="form-control btn">Setup authenticator</button>
</div>
<div class="col-sm-3 col-sm-offset-3" style="margin-bottom: 20px" *ngIf="!displayAuthenticator && accountDetails.twoFactorEnabled">
<button type="button" (click)="resetRecoverCodes()" class="form-control btn">Reset recovery codes</button>
<button [disabled]="!accountDetails.twoFactorEnabled" type="button" (click)="resetRecoverCodes()" class="form-control btn">Reset recovery codes</button>
</div>
<div class="col-sm-3" style="margin-bottom: 20px" *ngIf="!displayAuthenticator && accountDetails.twoFactorEnabled">
<button type="button" (click)="disable2FA()" class="form-control btn btn-danger">Disable 2FA</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class AccountComponent {
self.generatingQrCode = false;
(document.querySelector("#genQrCode > img") as HTMLInputElement).style.margin = "0 auto";
},
1000);
200);

}, error => console.error(error));
}
Expand All @@ -74,6 +74,9 @@ export class AccountComponent {
}

this.displayAuthenticator = false;
this.generatingQrCode = false;
this.accountDetails.hasAuthenticator = true;
this.accountDetails.twoFactorEnabled = true;

} else if (verifyAuthenticatorResult.status === StatusEnum.Error) {
this.errors = verifyAuthenticatorResult.data.toString();
Expand All @@ -95,6 +98,40 @@ export class AccountComponent {
},
error => console.error(error));
}

disable2FA() {
this.http.post(this.baseUrl + 'api/manageaccount/disable2FA', {}).subscribe(result => {

let disable2FAResult = result.json() as ResultVM;

if (disable2FAResult.status === StatusEnum.Success) {
this.stateService.displayNotification({ message: disable2FAResult.message, type: "success" });
this.accountDetails.twoFactorEnabled = false;
} else {
this.stateService.displayNotification({ message: disable2FAResult.message, type: "danger" });
}
},
error => console.error(error));
}

resetRecoverCodes() {
this.http.post(this.baseUrl + 'api/manageaccount/generateRecoveryCodes', {}).subscribe(result => {

let generateRecoverCodesResult = result.json() as ResultVM;

if (generateRecoverCodesResult.status === StatusEnum.Success) {
this.stateService.displayNotification({ message: generateRecoverCodesResult.message, type: "success" });

if (generateRecoverCodesResult.data && generateRecoverCodesResult.data.recoveryCodes) {
// display new recovery codes
this.recoveryCodes = generateRecoverCodesResult.data.recoveryCodes;
}
} else {
this.stateService.displayNotification({ message: generateRecoverCodesResult.message, type: "danger" });
}
},
error => console.error(error));
}
}

interface AccountDetailsVM {
Expand Down
6 changes: 4 additions & 2 deletions AspNetCoreIdentity/Controllers/ManageAccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,12 @@ public async Task<ResultVM> Disable2FA()
};
}

var result = await _userManager.SetTwoFactorEnabledAsync(user, false);

return new ResultVM
{
Status = Status.Success,
Message = "2FA has been successfully disabled"
Status = result.Succeeded ? Status.Success : Status.Error,
Message = result.Succeeded ? "2FA has been successfully disabled" : $"Failed to disable 2FA {result.Errors.FirstOrDefault()?.Description}"
};
}

Expand Down

0 comments on commit fc2a616

Please sign in to comment.