Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit } from '@angular/core';
- import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
- import { JhiEventManager } from 'ng-jhipster';
- import { Router } from '@angular/router';
- import { LoginModalService, AccountService, Account } from 'app/core';
- import {FormBuilder} from '@angular/forms';
- import { LoginService } from 'app/core/login/login.service';
- import { StateStorageService } from 'app/core/auth/state-storage.service';
- import {NotificationService} from 'app/entities/notification';
- import {HttpErrorResponse, HttpHeaders, HttpParams, HttpResponse} from '@angular/common/http';
- import {INotification} from 'app/shared/model/notification.model';
- @Component({
- selector: 'jhi-home',
- templateUrl: './home.component.html',
- styleUrls: ['home.scss']
- })
- export class HomeComponent implements OnInit {
- authenticationError: boolean;
- account: Account;
- modalRef: NgbModalRef;
- notifications: INotification[];
- loginForm = this.fb.group({
- username: [''],
- password: [''],
- rememberMe: [true]
- });
- itemsPerPage: any;
- page: any;
- predicate: any;
- reverse: any;
- constructor(
- private accountService: AccountService,
- private loginModalService: LoginModalService,
- private eventManager: JhiEventManager,
- private fb: FormBuilder,
- private loginService: LoginService,
- private stateStorageService: StateStorageService,
- private router: Router,
- protected notificationService: NotificationService,
- ) {}
- ngOnInit() {
- this.loadAllNotifications();
- this.accountService.identity().then((account: Account) => {
- this.account = account;
- });
- this.registerAuthenticationSuccess();
- }
- registerAuthenticationSuccess() {
- this.eventManager.subscribe('authenticationSuccess', message => {
- this.accountService.identity().then(account => {
- this.account = account;
- });
- });
- }
- isAuthenticated() {
- return this.accountService.isAuthenticated();
- }
- cancel() {
- this.authenticationError = false;
- this.loginForm.patchValue({
- username: '',
- password: ''
- });
- }
- login() {
- this.loginService
- .login({
- username: this.loginForm.get('username').value,
- password: this.loginForm.get('password').value,
- rememberMe: this.loginForm.get('rememberMe').value
- })
- .then(() => {
- this.authenticationError = false;
- if (this.router.url === '/register' || /^\/activate\//.test(this.router.url) || /^\/reset\//.test(this.router.url)) {
- this.router.navigate(['']);
- }
- this.eventManager.broadcast({
- name: 'authenticationSuccess',
- content: 'Sending Authentication Success'
- });
- // previousState was set in the authExpiredInterceptor before being redirected to login modal.
- // since login is successful, go to stored previousState and clear previousState
- const redirect = this.stateStorageService.getUrl();
- if (redirect) {
- this.stateStorageService.storeUrl(null);
- this.router.navigateByUrl(redirect);
- }
- })
- .catch(() => {
- this.authenticationError = true;
- });
- }
- register() {
- this.router.navigate(['/register']);
- }
- requestResetPassword() {
- this.router.navigate(['/reset', 'request']);
- }
- loadAllNotifications() {
- this.notificationService.query({
- page: 0,
- size: 5,
- sort: ['id,desc']
- })
- .subscribe( (data) =>
- this.notifications = data.body
- );
- }
- sort() {
- const result = [this.predicate + ',' + (this.reverse ? 'asc' : 'desc')];
- if (this.predicate !== 'id') {
- result.push('id');
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement