Implement login check in user menu and update AuthService integration

This commit is contained in:
Milad Raeisi
2024-12-11 20:23:52 +04:00
parent 58e5f0ce57
commit 36d467c19d
4 changed files with 42 additions and 37 deletions

View File

@@ -179,7 +179,7 @@ export class PostComponent implements OnDestroy {
});
}
trackByToken(index: number, item: any): any {
return item.id || index;
}

View File

@@ -10,32 +10,32 @@
</button>
<mat-menu [xPosition]="'before'" #userActions="matMenu">
<button mat-menu-item>
<span class="flex flex-col leading-none">
<span>Logged in as</span>
<span class="mt-1.5 text-md font-medium">{{
user()?.display_name || user?.name || 'Unknown User'
}}</span>
</span>
</button>
<mat-divider class="my-2"></mat-divider>
<button mat-menu-item (click)="Switch()">
<mat-icon [svgIcon]="'heroicons_outline:user-circle'"></mat-icon>
<span>Switch account</span>
</button>
<button mat-menu-item (click)="profile()">
<mat-icon [svgIcon]="'heroicons_outline:user-circle'"></mat-icon>
<span>Profile</span>
</button>
<button mat-menu-item [matMenuTriggerFor]="settingsMenu">
<mat-icon [svgIcon]="'heroicons_outline:cog-8-tooth'"></mat-icon>
<span>Options</span>
</button>
<mat-divider class="my-2"></mat-divider>
<button mat-menu-item (click)="logout()">
<mat-icon [svgIcon]="'heroicons_outline:arrow-right-on-rectangle'"></mat-icon>
<span>logout</span>
</button>
<ng-container *ngIf="authService.isLoggedIn(); else loginMenu">
<button mat-menu-item (click)="Switch()">
<mat-icon [svgIcon]="'heroicons_outline:user-circle'"></mat-icon>
<span>Switch account</span>
</button>
<button mat-menu-item (click)="profile()">
<mat-icon [svgIcon]="'heroicons_outline:user-circle'"></mat-icon>
<span>Profile</span>
</button>
<button mat-menu-item [matMenuTriggerFor]="settingsMenu">
<mat-icon [svgIcon]="'heroicons_outline:cog-8-tooth'"></mat-icon>
<span>Options</span>
</button>
<mat-divider class="my-2"></mat-divider>
<button mat-menu-item (click)="logout()">
<mat-icon [svgIcon]="'heroicons_outline:arrow-right-on-rectangle'"></mat-icon>
<span>logout</span>
</button>
</ng-container>
<ng-template #loginMenu>
<button mat-menu-item (click)="authService.promptLogin()">
<mat-icon [svgIcon]="'heroicons_outline:login'"></mat-icon>
<span>Login</span>
</button>
</ng-template>
</mat-menu>
<!-- Settings Menu -->

View File

@@ -22,6 +22,7 @@ import { Router, RouterModule } from '@angular/router';
import { StorageService } from 'app/services/storage.service';
import { SignerService } from 'app/services/signer.service';
import { NostrLoginService } from 'app/services/nostr-login.service';
import { AuthService } from 'app/services/auth.service';
@Component({
selector: 'user',
@@ -49,13 +50,17 @@ export class UserComponent {
private router = inject(Router);
private sanitizer = inject(DomSanitizer);
private nostrLoginService = inject(NostrLoginService);
public authService = inject(AuthService);
constructor() {
// Initialize userPubKey signal
this.userPubKey.set(this.signerService.getPublicKey());
// Load user profile
this.loadUserProfile();
// Check if user is logged in
if (this.authService.isLoggedIn()) {
// Load user profile
this.loadUserProfile();
}
// Subscribe to configuration changes
effect(() => {
@@ -89,7 +94,7 @@ export class UserComponent {
Switch(): void {
this.nostrLoginService.switchAccount();
}
profile(): void {
this.router.navigate(['/profile']);
}

View File

@@ -9,30 +9,30 @@ import { AngorConfirmationService } from '@angor/services/confirmation';
export class AuthService {
private signerService = inject(SignerService);
private router = inject(Router);
private angorConfirmationService = inject(AngorConfirmationService); // Ensure this is injected here
isLoggedIn(): boolean {
return !!this.signerService.getPublicKey();
}
promptLogin(): void {
const angorConfirmationService = inject(AngorConfirmationService);
const dialogRef = angorConfirmationService.open({
title: 'Login Required',
message: 'You need to be logged in to perform this action. Would you like to login now?',
const dialogRef = this.angorConfirmationService.open({
title: 'Login',
message: 'Would you like to login now?',
icon: {
show: true,
name: 'heroicons_solid:login',
name: 'heroicons_solid:user',
color: 'primary',
},
actions: {
confirm: {
show: true,
label: 'Login',
label: 'Yes, Login',
color: 'primary',
},
cancel: {
show: true,
label: 'Cancel',
label: 'No, Thanks',
},
},
dismissible: true,