Guest User

Untitled

a guest
Apr 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. <h1>{{ title }}</h1>
  2. <nav>
  3. <a routerLink="/posts"> Home </a>
  4. <a *ngIf="!logged" routerLink="/login"> Login </a>
  5. <a *ngIf="logged" routerLink="/posts" (click) = "logout()"> Logout</a>
  6. </nav>
  7. <router-outlet></router-outlet>
  8.  
  9. import { Component} from '@angular/core';
  10. import { LoginService } from './login.service';
  11. import { ActivatedRoute } from '@angular/router';
  12. import { Location } from '@angular/common';
  13.  
  14. @Component({
  15. selector: 'app-root',
  16. templateUrl: './app.component.html',
  17. styleUrls: ['./app.component.css']
  18. })
  19. export class AppComponent {
  20. title = 'My new Blog!';
  21.  
  22. constructor(private loginservice: LoginService){ }
  23.  
  24. logout(){
  25. this.loginservice.logged = false;
  26. this.loginservice.username ='';
  27.  
  28. }
  29.  
  30. }
  31.  
  32. <div>
  33. <h1>Login</h1>
  34. <input type="text" placeholder="Username*" #username>
  35. <input type="text" placeholder="Password*" #password>
  36. <button type="button" (click)="verify_userpass(username.value,password.value)">Submit</button>
  37. <div>
  38. <p>{{message}}</p>
  39. </div>
  40. </div>
  41.  
  42. import { Component, OnInit } from '@angular/core';
  43. import { LoginService } from '../login.service';
  44. import { Location } from '@angular/common';
  45. @Component({
  46. selector: 'app-login',
  47. templateUrl: './login.component.html',
  48. styleUrls: ['./login.component.css']
  49. })
  50. export class LoginComponent implements OnInit {
  51.  
  52. constructor(private loginservice: LoginService,private location: Location) { }
  53.  
  54. ngOnInit() {
  55. }
  56.  
  57. message: string;
  58. username: string = 'Admin';
  59. password: string = 'admin';
  60.  
  61.  
  62.  
  63. verify_user(username,password){
  64. if(username===this.username && password===this.password){
  65. this.loginservice.username = username;
  66. this.location.back();
  67. this.loginservice.logged = true;
  68. }
  69. else{
  70. this.message = 'Invalid credentials';
  71. }
  72. }
  73.  
  74. }
  75.  
  76. import { Injectable } from '@angular/core';
  77.  
  78. @Injectable()
  79. export class LoginService {
  80.  
  81. username : string;
  82. logged : boolean;
  83.  
  84. constructor() {
  85. }
  86.  
  87. }
Add Comment
Please, Sign In to add comment