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/"
target="_blank"
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
>
</p>

View File

@ -40,4 +40,15 @@ export class LoginService {
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 { LoginService } from "../../service/login.service";
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({
selector: "app-profil-information",
templateUrl: "./profil-information.component.html",
@ -15,9 +26,10 @@ export class ProfilInformationComponent {
activeTab: string = "profile-info2";
submitted = false;
storedData: any;
currentUser: any;
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;
constructor(
@ -28,12 +40,13 @@ export class ProfilInformationComponent {
ngOnInit(): void {
this.storedData = JSON.parse(localStorage.getItem("account_info"));
this.currentUser = JSON.parse(localStorage.getItem("currentUser"));
this.profilInfo = this.formBuilder.group({
firstName: ["", Validators.required],
lastName: ["", Validators.required],
email: ["", [Validators.required, Validators.email]],
phone: ["", [Validators.required, Validators.pattern(/^[0-9]{10,15}$/)]],
image: [null]
image: [null],
});
this.dataProfil(this.storedData.sub);
this.profilInfo.valueChanges.subscribe(() => {
@ -42,15 +55,17 @@ export class ProfilInformationComponent {
}
dataProfil(userId) {
this.authService.getDataProfil(userId).subscribe(data => {
this.authService.getDataProfil(userId).subscribe((data) => {
this.profilInfo.patchValue({
firstName: data.data.firstname,
lastName: data.data.lastname,
email: data.data.email,
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;
}
});
@ -60,13 +75,13 @@ export class ProfilInformationComponent {
if (event.target.files && event.target.files[0]) {
const file = event.target.files[0];
this.fileSelected = file;
if (!file.type.startsWith('image')) {
if (!file.type.startsWith("image")) {
this.toastr.error("Warning", "Please select an image file.", {
timeOut: 5000,
closeButton: true,
});
this.profilInfo.patchValue({
image: null
image: null,
});
return;
}
@ -119,11 +134,24 @@ export class ProfilInformationComponent {
this.authService.updateProfile(formData, this.storedData.sub).subscribe(
(response) => {
console.log("Profile updated successfully:", response);
this.toastr.success("success", "Profile updated successfully.", {
timeOut: 5000,
closeButton: true,
});
// Handle success response
this.authService
.updateUserProfile(this.currentUser.refresh_token)
.subscribe((resp) => {
const decodedToken = jwtDecode<CustomJwtPayload>(resp.access_token);
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) => {
console.error("Profile update failed:", error);

View File

@ -138,7 +138,7 @@
<p
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>
<!-- <div class="card-body">
<a

View File

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

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 478 KiB