This is a straightforward query.
I am attempting to set up a login system using Angular 5 for the front end and Drupal 8 for the backend. The connection is established successfully, I can send JSON data to the Drupal site, and it returns a CSRF token to me.
Now, my goal is to save this token in the localstorage.
Below is the login function defined in my login.service.ts file:
login (user: User): Observable<User> {
const url = `${this.mainUrl}user/login?_format=json`;
const loginReturn = this.http.post(url, user, httpHaljson);
console.log (loginReturn);
return loginReturn
.map(user =>
{
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify(user));
return user;
})
.pipe(
tap((user: User) => this.log(`Token `+ JSON.stringify(user.csrf_token))),
catchError(this.handleError<User>('login'))
);
}
If I try something like this:
.map(user =>
{
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify(user));
console.log ('object' + JSON.stringify(user));
console.log ('CSRF TOKEN' + JSON.stringify(user.csrf_token));
return user;
})
The console properly displays the csrf token without any issues, but there's an error message on the console:
Console Output:
object{"current_user":{"uid":"4","name":"cravushedal"},"csrf_token":"lY4vX3Ns_PrBkpPqoit4PEuEhXlimhKJ-xBt6ouUMXc","logout_token":"KOvJljv47rO3tRV3OG37ZKH57VWAG2gGxPe8nwCmN7A"}
login.service.ts:39 ERROR TS2339:
Property 'csrf_token' does not exist on type 'Object'.
The error states: ERROR in src/app/login.service.ts(37,65): error TS2339: Property 'csrf_token' does not exist on type 'Object'. https://i.sstatic.net/s3oyj.png