Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

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
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


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



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 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


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(