Get 2024 Templates Mega Bundle!14 Bootstrap, Vue & React Templates + 3 Vector Sets
Get for 99$

angular 8 getting logged user's id


hi!
i'm using metronic angular with ionic. I connected to my wep abi login page.I works fine. I wanted to take loggend user's id or token etc. on a page. could you write me a simple expample code.

I wrote like this
ngOnInit(): void {
this.bindTickets();
this.user$ = this.auth.currentUserSubject.asObservable();
}

when i console.log i can see all data but I couldn't parse (or something)
thank you


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


You can access the user's ID by subscribing to this.user$ and extracting the necessary information.

Here's a basic example of how you can do it:

import { Component, OnInit } from "@angular/core";
import { AuthService } from "path-to-your-auth-service"; // Replace with your actual import path

@Component({
selector: "app-your-component",
templateUrl: "your-component.component.html",
})
export class YourComponent implements OnInit {
user$;

constructor(private auth: AuthService) {}

ngOnInit(): void {
// Assuming currentUserSubject contains user data
this.user$ = this.auth.currentUserSubject.asObservable();

// Subscribe to user$ to access user data
this.user$.subscribe((user) => {
if (user) {
// Access user properties like ID or token
const userId = user.id; // Replace "id" with the actual property name
console.log("User ID:", userId);
}
});
}
}


If you can see the data in the console, it should be accessible and usable in your component as shown in the code example above.

Thanks



thank you happy

It worked, just same writing

ngOnInit(): void {
this.bindTickets();
this.user$ = this.auth.currentUserSubject.asObservable();
this.user$.subscribe((user) => {
if (user) {
const userId = user.Id; //
console.log("User ID:", userId);
}
});
}



You're welcome! I'm glad to hear that it worked for you. If you have any more questions or need further assistance with your code or any other topic, feel free to ask. happy


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(