integrasi update password
This commit is contained in:
22
src/app/content/hemat-app/service/login.service.ts
Normal file
22
src/app/content/hemat-app/service/login.service.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { HttpClient, HttpHeaders } from "@angular/common/http";
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
const BASE_URL = 'https://kapi.absys.ninja/hemat';
|
||||
|
||||
@Injectable({
|
||||
providedIn: "root",
|
||||
})
|
||||
export class LoginService {
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
updatePassword(data: any): Observable<any> {
|
||||
const endpoint = `/users`;
|
||||
const url = `${BASE_URL}${endpoint}/reset-password`;
|
||||
const headers = new HttpHeaders({
|
||||
'Content-Type': 'application/json',
|
||||
'x-api-key': 'j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT'
|
||||
});
|
||||
return this.http.put<any>(url, data, { headers });
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,16 @@
|
||||
import { Component, ViewChild } from "@angular/core";
|
||||
import { FormBuilder, FormGroup, NgForm, Validators, AbstractControl, ValidatorFn } from "@angular/forms";
|
||||
import {
|
||||
FormBuilder,
|
||||
FormGroup,
|
||||
NgForm,
|
||||
Validators,
|
||||
AbstractControl,
|
||||
ValidatorFn,
|
||||
} from "@angular/forms";
|
||||
import { ActivatedRoute, Router } from "@angular/router";
|
||||
import { LoginService } from "../../service/login.service";
|
||||
import { ToastrService } from "ngx-toastr";
|
||||
import { AuthService } from "src/app/_services/auth.service";
|
||||
|
||||
@Component({
|
||||
selector: "app-update-password",
|
||||
@@ -17,21 +27,35 @@ export class UpdatePasswordComponent {
|
||||
showNewPass: boolean = false;
|
||||
showConfirmPass: boolean = false;
|
||||
|
||||
storedData: any;
|
||||
|
||||
constructor(
|
||||
private formBuilder: FormBuilder,
|
||||
private router: Router,
|
||||
private route: ActivatedRoute
|
||||
private route: ActivatedRoute,
|
||||
private authService: LoginService,
|
||||
private toastr: ToastrService,
|
||||
private logoutService: AuthService,
|
||||
) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.storedData = JSON.parse(localStorage.getItem("account_info"));
|
||||
this.profilInfo = this.formBuilder.group({
|
||||
userid: this.storedData.sub,
|
||||
currentPass: ["", Validators.required],
|
||||
newPass: ["", [Validators.required, Validators.minLength(8), Validators.pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/)]],
|
||||
confirmPass: ["", [Validators.required, this.matchPassword('newPass')]],
|
||||
newPass: [
|
||||
"",
|
||||
[
|
||||
Validators.required,
|
||||
Validators.minLength(8),
|
||||
Validators.pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/),
|
||||
],
|
||||
],
|
||||
confirmPass: ["", [Validators.required, this.matchPassword("newPass")]],
|
||||
});
|
||||
|
||||
this.profilInfo.get('newPass').valueChanges.subscribe(() => {
|
||||
this.profilInfo.get('confirmPass').updateValueAndValidity();
|
||||
this.profilInfo.get("newPass").valueChanges.subscribe(() => {
|
||||
this.profilInfo.get("confirmPass").updateValueAndValidity();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -73,15 +97,54 @@ export class UpdatePasswordComponent {
|
||||
}
|
||||
|
||||
cancel() {
|
||||
// Handle cancel action
|
||||
this.profilInfo = this.formBuilder.group({
|
||||
userid: this.storedData.sub,
|
||||
currentPass: ["", Validators.required],
|
||||
newPass: [
|
||||
"",
|
||||
[
|
||||
Validators.required,
|
||||
Validators.minLength(8),
|
||||
Validators.pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/),
|
||||
],
|
||||
],
|
||||
confirmPass: ["", [Validators.required, this.matchPassword("newPass")]],
|
||||
});
|
||||
}
|
||||
|
||||
saveEdit() {
|
||||
// Handle save action
|
||||
console.log(this.profilInfo);
|
||||
console.log(this.profilInfo.invalid);
|
||||
console.log(this.profilInfo.valid);
|
||||
|
||||
if (this.profilInfo.valid) {
|
||||
const dataPassword = {
|
||||
userid: this.profilInfo.value.userid,
|
||||
password: this.profilInfo.value.newPass,
|
||||
};
|
||||
this.authService.updatePassword(dataPassword).subscribe(
|
||||
(res) => {
|
||||
this.toastr.success("Success", "Update Password Completed.", {
|
||||
timeOut: 2000,
|
||||
closeButton: true,
|
||||
});
|
||||
this.logout()
|
||||
},
|
||||
(error) => {
|
||||
console.error(error);
|
||||
this.toastr.error("Error", "Something went wrong!", {
|
||||
timeOut: 2000,
|
||||
closeButton: true,
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
logout() {
|
||||
if (localStorage.getItem('currentUser')) {
|
||||
this.logoutService.doLogout().then(res => {
|
||||
window.location.href = '/login';
|
||||
}, err => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
toggleCurrentPassVisibility() {
|
||||
|
||||
Reference in New Issue
Block a user