edit profil refresh token

This commit is contained in:
Fuzi_fauzia 2024-07-09 11:28:30 +07:00
parent 1edfbc7149
commit d8dc510742
6 changed files with 56 additions and 15 deletions

View File

@ -13,7 +13,7 @@
href="https://allbestsistem.com/" href="https://allbestsistem.com/"
target="_blank" target="_blank"
style="background-color: #000000 !important;" style="background-color: #000000 !important;"
>Smart Building Management Systems (V@2024-07-8.03) >Smart Building Management Systems (V@2024-07-9.01)
</a></span </a></span
> >
</p> </p>

View File

@ -40,4 +40,15 @@ export class LoginService {
return this.http.post(`${url}`, data, { headers }); return this.http.post(`${url}`, data, { headers });
} }
updateUserProfile(data: any): Observable<any> {
const body = { refresh_token: data };
const endpoint = `/users`;
const url = `${BASE_URL}${endpoint}/refresh-token`;
const headers = new HttpHeaders({
"x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT",
});
return this.http.post(`${url}`, body, { headers });
}
} }

View File

@ -2,7 +2,18 @@ import { Component, ViewChild } from "@angular/core";
import { FormBuilder, FormGroup, NgForm, Validators } from "@angular/forms"; import { FormBuilder, FormGroup, NgForm, Validators } from "@angular/forms";
import { LoginService } from "../../service/login.service"; import { LoginService } from "../../service/login.service";
import { ToastrService } from "ngx-toastr"; import { ToastrService } from "ngx-toastr";
import { jwtDecode } from "jwt-decode";
interface CustomJwtPayload {
exp: number;
scope: string;
iat: number;
preferred_username: string;
name: string;
email: string;
family_name: string;
given_name: string;
sub: string;
}
@Component({ @Component({
selector: "app-profil-information", selector: "app-profil-information",
templateUrl: "./profil-information.component.html", templateUrl: "./profil-information.component.html",
@ -15,9 +26,10 @@ export class ProfilInformationComponent {
activeTab: string = "profile-info2"; activeTab: string = "profile-info2";
submitted = false; submitted = false;
storedData: any; storedData: any;
currentUser: any;
disableButton: boolean = false; disableButton: boolean = false;
url: any = 'https://www.w3schools.com/howto/img_avatar.png'; url: any = "https://www.w3schools.com/howto/img_avatar.png";
fileSelected: any = null; fileSelected: any = null;
constructor( constructor(
@ -28,12 +40,13 @@ export class ProfilInformationComponent {
ngOnInit(): void { ngOnInit(): void {
this.storedData = JSON.parse(localStorage.getItem("account_info")); this.storedData = JSON.parse(localStorage.getItem("account_info"));
this.currentUser = JSON.parse(localStorage.getItem("currentUser"));
this.profilInfo = this.formBuilder.group({ this.profilInfo = this.formBuilder.group({
firstName: ["", Validators.required], firstName: ["", Validators.required],
lastName: ["", Validators.required], lastName: ["", Validators.required],
email: ["", [Validators.required, Validators.email]], email: ["", [Validators.required, Validators.email]],
phone: ["", [Validators.required, Validators.pattern(/^[0-9]{10,15}$/)]], phone: ["", [Validators.required, Validators.pattern(/^[0-9]{10,15}$/)]],
image: [null] image: [null],
}); });
this.dataProfil(this.storedData.sub); this.dataProfil(this.storedData.sub);
this.profilInfo.valueChanges.subscribe(() => { this.profilInfo.valueChanges.subscribe(() => {
@ -42,15 +55,17 @@ export class ProfilInformationComponent {
} }
dataProfil(userId) { dataProfil(userId) {
this.authService.getDataProfil(userId).subscribe(data => { this.authService.getDataProfil(userId).subscribe((data) => {
this.profilInfo.patchValue({ this.profilInfo.patchValue({
firstName: data.data.firstname, firstName: data.data.firstname,
lastName: data.data.lastname, lastName: data.data.lastname,
email: data.data.email, email: data.data.email,
phone: data.data.phone, phone: data.data.phone,
image: [null] image: [null],
}); });
if (data.data.image_path !== "https://kapi.absys.ninja/hemat/image/null") { if (
data.data.image_path !== "https://kapi.absys.ninja/hemat/image/null"
) {
this.url = data.data.image_path; this.url = data.data.image_path;
} }
}); });
@ -60,13 +75,13 @@ export class ProfilInformationComponent {
if (event.target.files && event.target.files[0]) { if (event.target.files && event.target.files[0]) {
const file = event.target.files[0]; const file = event.target.files[0];
this.fileSelected = file; this.fileSelected = file;
if (!file.type.startsWith('image')) { if (!file.type.startsWith("image")) {
this.toastr.error("Warning", "Please select an image file.", { this.toastr.error("Warning", "Please select an image file.", {
timeOut: 5000, timeOut: 5000,
closeButton: true, closeButton: true,
}); });
this.profilInfo.patchValue({ this.profilInfo.patchValue({
image: null image: null,
}); });
return; return;
} }
@ -119,11 +134,24 @@ export class ProfilInformationComponent {
this.authService.updateProfile(formData, this.storedData.sub).subscribe( this.authService.updateProfile(formData, this.storedData.sub).subscribe(
(response) => { (response) => {
console.log("Profile updated successfully:", response); console.log("Profile updated successfully:", response);
this.toastr.success("success", "Profile updated successfully.", { this.authService
timeOut: 5000, .updateUserProfile(this.currentUser.refresh_token)
closeButton: true, .subscribe((resp) => {
}); const decodedToken = jwtDecode<CustomJwtPayload>(resp.access_token);
// Handle success response localStorage.setItem("account_info", JSON.stringify(decodedToken));
const userProfile = {
access_token: resp.access_token,
refresh_token: resp.refresh_token,
displayName: decodedToken.name,
buildingId: 4,
};
localStorage.setItem("currentUser", JSON.stringify(userProfile));
window.location.reload();
this.toastr.success("success", "Profile updated successfully.", {
timeOut: 5000,
closeButton: true,
});
});
}, },
(error) => { (error) => {
console.error("Profile update failed:", error); console.error("Profile update failed:", error);

View File

@ -138,7 +138,7 @@
<p <p
class="card-subtitle line-on-side text-muted text-center font-small-3 mx-2 my-1" class="card-subtitle line-on-side text-muted text-center font-small-3 mx-2 my-1"
> >
<span>(v@2024.07.8.03)</span> <span>(v@2024.07.9.01)</span>
</p> </p>
<!-- <div class="card-body"> <!-- <div class="card-body">
<a <a

View File

@ -79,6 +79,8 @@ export class LoginComponent implements OnInit {
localStorage.setItem("account_info", JSON.stringify(decodedToken)); localStorage.setItem("account_info", JSON.stringify(decodedToken));
const userProfile = { const userProfile = {
access_token: response.access_token,
refresh_token: response.refresh_token,
displayName: decodedToken.name, displayName: decodedToken.name,
buildingId: 4, buildingId: 4,
}; };

BIN
src/assets/images/bg1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 478 KiB