Advertisement
Guest User

Untitled

a guest
May 6th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. import {Injectable, Inject} from "angular2/core";
  2. import {Http, Headers} from "angular2/http";
  3. import 'rxjs/add/operator/map';
  4. import {User} from "../User/User";
  5.  
  6.  
  7. @Injectable()
  8. export class loginService {
  9.  
  10. constructor(private _http:Http) {
  11.  
  12. }
  13.  
  14. login(user) {
  15.  
  16. // var creds = "username=" + "khalil" + "&password=" + 3; .map(res=>res.text());
  17. var headers = new Headers();
  18. headers.append('Content-Type', 'application/json');
  19. return this._http.post('http://localhost:8080/login', user, {
  20. headers: headers
  21. })
  22. .map(res=>res.json());
  23.  
  24. }
  25.  
  26. import {Component, Inject} from 'angular2/core';
  27. import {Router} from "angular2/router";
  28. import {loginService} from "./login.service";
  29. import {HTTP_PROVIDERS, Http} from "angular2/http";
  30. import {User} from "../User/User";
  31. import {ControlGroup, FormBuilder, Validators, Control} from "angular2/common";
  32.  
  33. @Component({
  34. selector: 'login',
  35. templateUrl: '../dev/login/login.html',
  36. providers: [HTTP_PROVIDERS, loginService]
  37. })
  38.  
  39. export class loginComponent {
  40. public _user:User;
  41. loginForm:ControlGroup;
  42.  
  43. constructor(private _router:Router,
  44. private _loginService:loginService,
  45. private _formBuilder:FormBuilder) {
  46.  
  47.  
  48. this._user = {
  49. firstName: '',
  50. lastName: '',
  51. tel: '',
  52. email: '',
  53. creationDate: new Date(),
  54. compteMatriculaire: '',
  55. enabled: false,
  56. password: '',
  57. structure: ''
  58. };
  59.  
  60. }
  61.  
  62. login() {
  63. console.log("email"+this._user.email);
  64. this._loginService.login(this._user).subscribe(
  65. (data:User) => {
  66. this._user = data;
  67. },
  68. error => alert('Erreur ' + error),
  69. () => {
  70. console.log("finished " + this._user.email + " " + this._user.password);
  71. //this._router.navigate(['Home']);
  72. }
  73. );
  74. }
  75.  
  76. <div class="container">
  77.  
  78. <form #loginForm="ngForm" (ngSubmit)="login()" class="form-signin">
  79. <h2 class="form-signin-heading">Please sign in</h2>
  80. <label for="email" class="sr-only">Email</label>
  81. <input type="email" ngControl="email" [(ngModel)]="_user.email" #email id="email" class="form-control" placeholder="email" required autofocus>
  82. <label for="password" class="sr-only">Password</label>
  83. <input type="password" ngControl="password" [(ngModel)]="_user.password" #password id="password" class="form-control" placeholder="Password" required>
  84. <div class="checkbox">
  85. <label>
  86. <input type="checkbox" value="remember-me"> Remember me
  87. </label>
  88. </div>
  89. <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
  90. </form>
  91. </div>
  92.  
  93. @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
  94. public ResponseEntity<User_khalil> login(User_khalil user) {
  95. System.out.println("------- " + user.getEmail() + user.getPassword());
  96.  
  97. User_khalil u = userRepository_khalil.findOneByEmailAndPassword(user.getEmail(), user.getPassword());
  98. if (u != null) {
  99. return new ResponseEntity<User_khalil>(user, HttpStatus.OK);
  100. }
  101. return new ResponseEntity<User_khalil>(user, HttpStatus.OK);
  102. }
  103.  
  104. return this._http.post('http://localhost:8080/login', JSON.stringify(user), {
  105. headers: headers
  106. })
  107. .map(res=>res.json());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement