Guest User

Untitled

a guest
Apr 5th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. <?php
  2. namespace AppBundleServices;
  3.  
  4. use FirebaseJWTJWT;
  5.  
  6. class JwtAuth{
  7.  
  8. public $manager;
  9. public $key;
  10.  
  11. public function __construct($manager){
  12. $this-> manager = $manager;
  13. $this-> key = 'soytuclavesecreta12344321';
  14. }
  15. public function signup($email,$password, $getHash = null){
  16. //Buscamos en la base de datos el usuario con findOneBy
  17. $user = $this->manager->getRepository('BackendBundle:User')->findOneBy(array(
  18. "email" => $email,
  19. "password" => $password
  20. ));
  21. $signup = false;
  22. if(is_object($user)){
  23. $signup = true;
  24. }
  25. if($signup ==true){
  26. //generar token
  27. $token = array(
  28. 'sub' => $user->getId(),
  29. 'email' => $user->getEmail(),
  30. 'name' => $user->getName(),
  31. 'surname' => $user->getSurname(),
  32. 'iat' => time(),
  33. 'exp' => time()+(7*24*60*60)
  34. );
  35.  
  36. $jwt = JWT::encode($token, $this->key, 'HS256');
  37. $decoded = JWT::decode($jwt, $this->key, array('HS256'));
  38.  
  39. if($getHash == null){
  40. $data = $jwt;
  41. }else{
  42. $data = $decoded;
  43. }
  44.  
  45. }else{
  46. $data = array(
  47. 'status' => 'error',
  48. 'data'=> 'Login failed'
  49. );
  50. }
  51. return $data;
  52. }
  53. public function checkToken($jwt,$getIdentity = false){
  54. $auth = false;
  55. try{
  56. $decoded = JWT::decode($jwt, $this->key, array('HS256'));
  57. }catch(UnexpectedValueException $e){
  58. $auth = false;
  59. }catch(DomainException $e){
  60. $auth = false;
  61. }
  62. if(is_object($decoded) && isset($decoded -> sub)){
  63. $auth = true;
  64. }else{
  65. $auth = false;
  66. }
  67. if($getIdentity == false){
  68. return $auth;
  69. }else{
  70. return $decoded;
  71. }
  72. }
  73. }
  74.  
  75. import { Component, OnInit } from '@angular/core';
  76. import { Router, ActivatedRoute, Params } from '@angular/router';
  77. import {UserService} from '../services/user.service';
  78. @Component({
  79. selector: 'login',
  80. templateUrl: '../views/login.html',
  81. providers: [UserService]
  82. })
  83. export class LoginComponent implements OnInit{
  84. public title: string;
  85. public user;
  86. public identity;
  87. public token;
  88. constructor(
  89. private _route: ActivatedRoute,
  90. private _router: Router,
  91. private _userService: UserService
  92. ){
  93. this.title = 'Identifícate';
  94. this.user = {
  95. "email":"",
  96. "password":"",
  97. "gethash": "false"
  98. };
  99. }
  100. ngOnInit(){
  101. console.log('El componente login.component ha sido cargado');
  102. console.log(JSON.parse(localStorage.getItem('identity')));
  103. this.logout();
  104. this.redirectIfIdentity();
  105. }
  106.  
  107. onSubmit(){
  108. console.log(this.user);
  109. this._userService.signup(this.user).subscribe(
  110. response => {
  111. this.identity = response;
  112. if(this.identity.length <= 1){
  113. console.log('Error en el servidor');
  114. }{
  115. if(!this.identity.status){
  116. localStorage.setItem('identity', JSON.stringify(this.identity));
  117. //get token
  118. this.user.getHash=null;
  119. this._userService.signup(this.user).subscribe(
  120. response => {
  121. this.token = response;
  122. if(this.identity.length <= 1){
  123. console.log('Error en el servidor');
  124. }{
  125. if(this.identity.status){
  126. localStorage.setItem('token', JSON.stringify(this.identity));
  127.  
  128. //window.location.href= "/";
  129. }
  130. }
  131. },
  132. error => {
  133. console.log(<any>error)
  134. }
  135. );
  136. }
  137. }
  138. },
  139. error => {
  140. console.log(<any>error)
  141. }
  142. );
  143. }
  144.  
  145. import { Injectable } from '@angular/core';
  146. import { Http, Response, Headers } from '@angular/http';
  147. import "rxjs/add/operator/map";
  148. import { Observable } from 'rxjs/Observable';
  149. import { GLOBAL } from './global';
  150. @Injectable()
  151. export class UserService{
  152.  
  153. public url: string;
  154. public identity;
  155. public token;
  156. constructor(private _http: Http){
  157. this.url = GLOBAL.url;
  158. }
  159. signup(user_to_login){
  160. let json = JSON.stringify(user_to_login);
  161. let params = "json="+json;
  162. let headers = new Headers({'Content-Type':'application/x-www-form-urlencoded'});
  163. return this._http.post(this.url+'/login', params, {headers: headers})
  164. .map(res => res.json());
  165. }
  166.  
  167. <div class="col-md-12">
  168.  
  169. <h3>{‌{title}}</h3>
  170.  
  171. <div class="identity_alert alert alert-success" *ngIf= "identity && identity.sub">
  172. Te has logueado correctamente Bienvenido {‌{identity.email}}
  173. </div>
  174.  
  175. <div class="identity_alert alert alert-danger" *ngIf= "identity && identity.data">
  176. No te has logueado correctamente
  177. </div>
  178.  
  179. <form #loginForm="ngForm" (ngSubmit)="onSubmit()" class = "col-md-7 no-padding">
  180.  
  181. <p>
  182. <label>Email:</label>
  183. <input type="email" class="form-control" name="email" #email="ngModel" [(ngModel)]="user.email" required/>
  184. <span *ngIf="!email.valid && email.touched">El email no es válido</span>
  185. </p>
  186.  
  187. <p>
  188. <label>Contraseña:</label>
  189. <input type="password" class="form-control" name="password" #password="ngModel" [(ngModel)]="user.password"
  190. required/>
  191. <span *ngIf="!password.valid && password.touched">La contraseña no es válida</span>
  192. </p>
  193.  
  194. <input type="submit" value="Entrar" class="btn btn-primary"[disabled]="!loginForm.form.valid"/>
  195.  
  196. </form>
  197. </div>
Add Comment
Please, Sign In to add comment