SHARE
TWEET

Untitled

a guest Dec 23rd, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Observable } from 'rxjs/Observable';
  2. import { AuthService } from './../services/auth.service';
  3. import { Component, Inject } from "@angular/core";
  4. import { FormGroup, FormControl, FormBuilder, Validators } from
  5. '@angular/forms';
  6. import { Router } from "@angular/router";
  7.  
  8.  
  9. @Component({
  10. selector: "login",
  11. templateUrl: "./login.component.html",
  12. //styleUrls: ['./login.component.less']
  13. })
  14. export class LoginComponent {
  15. title: string;
  16. form: FormGroup;
  17. constructor(private router: Router,
  18. private fb: FormBuilder,
  19. private authService: AuthService,
  20. @Inject('BASE_URL') private baseUrl: string) {
  21.  
  22. this.title = "User Login";
  23. // initialize the form
  24. this.createForm();
  25.  }
  26. createForm() {
  27.  this.form = this.fb.group({
  28.   Username: ['', Validators.required],
  29.   Password: ['', Validators.required]
  30. });
  31. }
  32. onSubmit() {
  33. var url = this.baseUrl + "api/token/auth";
  34. var username = this.form.value.Username;
  35. var password = this.form.value.Password;
  36.  
  37. this.authService.login(username, password)
  38.   .subscribe(res => {
  39.     // login successful
  40.     // outputs the login info through a JS alert.
  41.     // IMPORTANT: remove this when test is done.
  42.     alert("Login successful! "
  43.       + "USERNAME: "
  44.       + username
  45.       + " TOKEN: "
  46.       + this.authService.getAuth()!.token
  47.     );
  48.     this.router.navigate(["home"]);
  49.   },
  50.   err => {
  51.     // login failed
  52.     console.log(err)
  53.     this.form.setErrors({
  54.       "auth": "Incorrect username or password"
  55.     });
  56.   });
  57.  }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top