import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { NativeLoginService } from '../../services/login/native-login.service'; import { TokenService, TokenProvider } from '../../services/login/token.service'; import { Router } from '@angular/router'; import '../../../assets/custom.js'; import { AuthService } from 'app/services/auth/auth.service'; import { Credential } from 'app/models/login/Credential'; import { ActivatedRoute, Params } from '@angular/router'; import { NgZone } from '@angular/core'; @Component({ selector: 'app-main-sign-in', templateUrl: './main-sign-in.component.html', styleUrls: ['./main-sign-in.component.css'] }) export class MainSignInComponent implements OnInit { nativeLoginForm: any; creds: any = { "username": "", "password": "" }; constructor(private fb: FormBuilder, private nativeLogin: NativeLoginService, private tokenService: TokenService, private router: Router, private authservice: AuthService, public route: ActivatedRoute, private zone: NgZone) { } createProjectEditorForm() { this.nativeLoginForm = this.fb.group({ username: ['', Validators.required], password: ['', Validators.required] }); } ngOnInit() { this.createProjectEditorForm(); } login() { //login using the credentials // this.nativeLogin.login(this.creds.username, this.creds.password).subscribe( // response => { // simple_notifier("success",null,"Successful login"); // this.tokenService.login(response['token'], TokenProvider.native, this.creds.username, response['email']); // }, // err => { // simple_notifier("danger",null,"Failed to login"); // } // ); let credentials = new Credential(); credentials.username = this.creds.username; credentials.secret = this.creds.password; this.authservice.nativeLogin(credentials).subscribe( res => this.onLogInSuccess(res), error => this.onLogInError(error) ); } public onLogInSuccess(logoutMessage: any) { // this.snackBar.openFromComponent(SnackBarNotificationComponent, { // data: { message: 'GENERAL.SNACK-BAR.SUCCESSFUL-LOGIN', language: this.language }, // duration: 3000, // extraClasses: ['snackbar-success'] // }); this.route.queryParams.subscribe((params: Params) => { let redirectUrl = params['returnUrl'] ? params['returnUrl'] : '/'; this.zone.run(() => this.router.navigate([redirectUrl])); }) } public onLogInError(errorMessage: string) { // this.snackBar.openFromComponent(SnackBarNotificationComponent, { // data: { message: 'GENERAL.SNACK-BAR.UNSUCCESSFUL-LOGIN', language: this.language }, // duration: 3000, // extraClasses: ['snackbar-warning'] // }) } }