mirror of
https://github.com/CaramelFur/Picsur.git
synced 2025-10-26 06:56:06 +01:00
75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { Router } from '@angular/router';
|
|
import { AutoUnsubscribe } from 'ngx-auto-unsubscribe-decorator';
|
|
import { Permission } from 'picsur-shared/dist/dto/permissions.dto';
|
|
import { HasFailed } from 'picsur-shared/dist/types';
|
|
import { SnackBarType } from 'src/app/models/dto/snack-bar-type.dto';
|
|
import { UserPassModel } from 'src/app/models/forms-dto/userpass.dto';
|
|
import { PermissionService } from 'src/app/services/api/permission.service';
|
|
import { UserService } from 'src/app/services/api/user.service';
|
|
import { Logger } from 'src/app/services/logger/logger.service';
|
|
import { UtilService } from 'src/app/util/util-module/util.service';
|
|
import { LoginControl } from '../../../models/forms/login.control';
|
|
|
|
@Component({
|
|
templateUrl: './login.component.html',
|
|
styleUrls: ['./login.component.scss'],
|
|
})
|
|
export class LoginComponent implements OnInit {
|
|
private readonly logger = new Logger('LoginComponent');
|
|
|
|
showRegister = false;
|
|
|
|
model = new LoginControl();
|
|
|
|
constructor(
|
|
private userService: UserService,
|
|
private permissionService: PermissionService,
|
|
private router: Router,
|
|
private utilService: UtilService,
|
|
) {}
|
|
|
|
ngOnInit(): void {
|
|
const state = history.state as UserPassModel;
|
|
if (state) {
|
|
this.model.putData(state);
|
|
history.replaceState(null, '');
|
|
}
|
|
|
|
this.onPermissions();
|
|
}
|
|
|
|
@AutoUnsubscribe()
|
|
onPermissions() {
|
|
return this.permissionService.live.subscribe((permissions) => {
|
|
this.showRegister = permissions.includes(Permission.UserRegister);
|
|
});
|
|
}
|
|
|
|
async onSubmit() {
|
|
const data = this.model.getData();
|
|
if (HasFailed(data)) {
|
|
return;
|
|
}
|
|
|
|
const user = await this.userService.login(data.username, data.password);
|
|
if (HasFailed(user)) {
|
|
this.logger.error(user.getReason());
|
|
this.utilService.showSnackBar(
|
|
'Login failed, please try again',
|
|
SnackBarType.Error,
|
|
);
|
|
return;
|
|
}
|
|
|
|
this.utilService.showSnackBar('Login successful', SnackBarType.Success);
|
|
this.router.navigate(['/']);
|
|
}
|
|
|
|
async onRegister() {
|
|
this.router.navigate(['/user/register'], {
|
|
state: this.model.getRawData(),
|
|
});
|
|
}
|
|
}
|