Created
April 24, 2019 11:14
-
-
Save mhkoca/fdfd905dacacad457ba3275fe93111ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, NgZone } from '@angular/core'; | |
import { Router, ActivatedRoute } from '@angular/router'; | |
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; | |
import { first } from 'rxjs/operators'; | |
import { AuthenticationService } from '../../services/authentication.service'; | |
import { AlertService } from '../../services/alert.service'; | |
import { CookieService } from 'ngx-cookie-service'; | |
@Component({ | |
selector: 'app-login', | |
templateUrl: './login.component.html', | |
styleUrls: ['./login.component.css'] | |
}) | |
export class LoginComponent implements OnInit { | |
loginForm: FormGroup; | |
loading = false; | |
submitted = false; | |
returnUrl: string; | |
get frm() { return this.loginForm.controls; } | |
constructor( | |
private formBuilder: FormBuilder, | |
private route: ActivatedRoute, | |
private router: Router, | |
private authenticationService: AuthenticationService, | |
private alertService: AlertService, | |
private zone: NgZone, | |
private cookieService : CookieService | |
) { | |
if(cookieService.get('remember')) { | |
this.frm.username.setValue(this.cookieService.get('username')); | |
this.frm.RememberMe.setValue(this.cookieService.get('remember')); | |
} | |
} | |
ngOnInit() { | |
this.loginForm = this.formBuilder.group({ | |
username: ['', Validators.required], | |
password: ['', Validators.required], | |
RememberMe: [''] | |
}); | |
this.authenticationService.logout(); | |
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/'; | |
} | |
onSubmit() { | |
this.submitted = true; | |
if (this.loginForm.invalid) { | |
return; | |
} | |
this.loading = true; | |
this.authenticationService.login(this.frm.username.value, this.frm.password.value) | |
.pipe(first()) | |
.subscribe( | |
data => { | |
this.cookieService.set('username', this.frm.username.value); | |
this.cookieService.set('remember', this.frm.RememberMe.value); | |
this.zone.run(() => this.router.navigate(['../'])); | |
}, | |
error => { | |
this.alertService.error(error); | |
this.loading = false; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment