mirror of
https://github.com/CaramelFur/Picsur.git
synced 2025-11-13 15:25:39 +01:00
Add ability to login
This commit is contained in:
@@ -1,15 +1,57 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { Component, OnDestroy, OnInit } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { AutoUnsubscribe } from 'ngx-auto-unsubscribe-decorator';
|
||||
import { EUser } from 'picsur-shared/dist/entities/user.entity';
|
||||
import { HasFailed } from 'picsur-shared/dist/types';
|
||||
import { UserService } from 'src/app/api/user.service';
|
||||
import { SnackBarType } from 'src/app/models/snack-bar-type';
|
||||
import { UtilService } from 'src/app/util/util.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-header',
|
||||
templateUrl: './header.component.html',
|
||||
styleUrls: ['./header.component.scss'],
|
||||
})
|
||||
export class HeaderComponent {
|
||||
constructor(private router: Router) {}
|
||||
export class HeaderComponent implements OnInit {
|
||||
private currentUser: EUser | null = null;
|
||||
|
||||
public get user() {
|
||||
return this.currentUser;
|
||||
}
|
||||
|
||||
public get isLoggedIn() {
|
||||
return this.currentUser !== null;
|
||||
}
|
||||
|
||||
constructor(
|
||||
private router: Router,
|
||||
private userService: UserService,
|
||||
private utilService: UtilService
|
||||
) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.subscribeUser();
|
||||
}
|
||||
|
||||
@AutoUnsubscribe()
|
||||
subscribeUser() {
|
||||
return this.userService.liveUser.subscribe((user) => {
|
||||
console.log('user', user);
|
||||
this.currentUser = user;
|
||||
});
|
||||
}
|
||||
|
||||
doLogin() {
|
||||
this.router.navigate(['/login']);
|
||||
}
|
||||
|
||||
doLogout() {
|
||||
const user = this.userService.logout();
|
||||
if (HasFailed(user)) {
|
||||
this.utilService.showSnackBar(user.getReason(), SnackBarType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
this.utilService.showSnackBar('Logout successful', SnackBarType.Success);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user