Ask Your Question
0

How can the online status of a user be displayed in real-time using Angular 10 and JWT with ASP.NET Core?

asked 2021-05-25 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-05-16 05:00:00 +0000

qstack gravatar image

To display the online status of a user in real-time using Angular 10 and JWT with ASP.NET Core, you can follow these steps:

  1. Create an API endpoint in ASP.NET Core that returns the online status of a user. This endpoint should take the user's JWT token as input and return the status (online or offline) of the user.

  2. In your Angular 10 application, create a service that calls this API endpoint and retrieves the online status of the user.

  3. Use RxJS to create an Observable that regularly calls this API endpoint and updates the online status of the user in real-time.

  4. Use Angular's built-in directives (e.g. ngClass, ngStyle) to display the online status of the user on the frontend.

  5. When the user logs out or their JWT token expires, stop calling the API endpoint and update the online status of the user to offline.

Here's some sample code that might help you get started:

//backend API endpoint [Authorize] [HttpGet("userstatus")] public IActionResult GetCurrentUserStatus() { var currentUserId = User.FindFirst(ClaimTypes.NameIdentifier).Value; //TODO: retrieve online status of the current user from the database var onlineStatus = GetOnlineStatus(currentUserId); return Ok(onlineStatus); }

//Angular service import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable, timer } from 'rxjs'; import { switchMap } from 'rxjs/operators';

@Injectable({ providedIn: 'root' }) export class UserStatusService { private apiUrl = '/api/userstatus';

constructor(private http: HttpClient) { }

getUserStatus$(token: string): Observable<string> { return timer(0, 5000).pipe( switchMap(() => this.http.get<string>( this.apiUrl, { headers: { 'Authorization': Bearer ${token} } } )) ); } }

//Angular component import { Component, OnDestroy, OnInit } from '@angular/core'; import { Subscription } from 'rxjs'; import { UserStatusService } from './user-status.service';

@Component({ selector: 'app-user-status', template: <span [ngClass]="{ 'badge-success': isOnline, 'badge-danger': !isOnline }"> {{ isOnline ? 'Online' : 'Offline' }} </span> }) export class UserStatusComponent implements OnInit, OnDestroy { isOnline = false; private subscription: Subscription;

constructor(private userStatusService: UserStatusService) { }

ngOnInit() { const token = localStorage.getItem('token'); this.subscription = this.userStatusService.getUserStatus$(token) .subscribe(status => this.isOnline = (status === 'online')); }

ngOnDestroy() { this.subscription.unsubscribe(); this.isOnline = false; } }

Note that this code is just a starting point and will need to be customized to fit your specific requirements.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-05-25 11:00:00 +0000

Seen: 9 times

Last updated: May 16 '21