Is there a way to automatically retrieve the user id of the logged-in user and include it in the payload when they want to update their data without having to manually input their id?
MY LOGIN COMPONENT
export class LoginComponent { studentForm: FormGroup; student: any; constructor( private fb: FormBuilder, private crudService: CrudService, private router: Router, private toastr: ToastrService) { this.studentForm = this.fb.group({ id: ['', Validators.compose([Validators.required])], password: ['', Validators.compose([Validators.required])] }); } saveStudentDetails(values) { const studentData = {}; studentData['id'] = values.id; studentData['password'] = values.password; this.crudService.loginstudent(studentData).subscribe(result => { this.student = result; this.toastr.success('You are logged in', 'Success !', { positionClass: 'toast-bottom-right' }); console.log(this.crudService.loginstudent); this.router.navigate(['/address']); }, err => { console.log('status code ->' + err.status); this.toastr.error('Please try again', 'Error !', { positionClass: 'toast-bottom-right' }); }); }}
// MY EDIT COMPONENT
saveStudentDetails(values) {
// const studentData = new FormData();
const studentData = {};
studentData['s_pNumber'] = values.s_pNumber;
studentData['s_address'] = values.s_address;
studentData['s_pCode'] = values.s_pCode;
studentData['s_city'] = values.s_city;
studentData['s_state'] = values.s_state;
studentData['s_country'] = values.s_country;
this.crudService.createAddress(studentData).subscribe(result => {
// this.student = result;
this.toastr.success('Your data has been inserted', 'Success !', { positionClass: 'toast-bottom-right' });
// this.router.navigate(['/finance']);
},
err => {
console.log('status code ->' + err.status);
this.toastr.error('Please try again', 'Error !', { positionClass: 'toast-bottom-right' });
});
}
// THIS IS MY SERVICE
createAddress(data) {
const postHttpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
postHttpOptions['observe'] = 'response';
return this.http.put(this.url + '/address/${id}', data);
}
I'm unsure if I'm approaching this correctly. Can someone advise me on how to include the user id in the payload sent to the backend? Thank you.