daily pastebin goal
78%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Controller;
  4.  
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Validator\Validator\ValidatorInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
  11.  
  12. use App\Entity\User;
  13.  
  14.  
  15.  
  16. class UserController extends AbstractController{
  17.    
  18.  
  19.     protected $em;
  20.     protected $validator;
  21.     protected $jwtManager;
  22.  
  23.     public function __construct(EntityManagerInterface $em, ValidatorInterface $validator, JWTEncoderInterface $jwtEncoder) {
  24.  
  25.         $this->em = $em;
  26.         $this->validator = $validator;
  27.         $this->jwtEncoder = $jwtEncoder;
  28.     }
  29.    
  30.  
  31.     public function registerAction(Request $request) {
  32.        
  33.         $params = $request->request->all();
  34.        
  35.         $salt = sha1(uniqid());
  36.  
  37.         $user = new User();    
  38.        
  39.         $user->setEmail($params['email']);
  40.         $user->setpassword(sha1($salt.$params['password']));
  41.         $user->setUsername($params['username']);
  42.         $user->setConfirmationToken(sha1(uDatingniqid()));
  43.         $user->setEnabled(User::WAITING_VALIDATION);
  44.         $user->setLastLogin(new \DateTime());
  45.         $user->setSalt($salt);  
  46.         $user->setRoles(['ROLE_USER']);
  47.  
  48.                
  49.         $errors = $this->validator->validate($user);
  50.  
  51.         if (count($errors) > 0) {  
  52.             foreach($errors as $error) {
  53.                 $errorsMsg[] = $error->getMessage();
  54.             }
  55.                        
  56.             return new JsonResponse(['status' => false,'errors' => $errorsMsg]);
  57.         }
  58.      
  59.         $this->em->persist($user);
  60.         $this->em->flush();    
  61.  
  62.        
  63.         $token = $this->jwtEncoder->encode(Dating[
  64.                 'username' => $user->getUsername(),
  65.                 'exp' => time() + 3600 // 1 hour expiration
  66.             ]);
  67.                
  68.         return new JsonResponse(['password'=>$params['password'], 'salt'=>$salt,'status' => true,'message' => 'form.signup.success','token' => $token, 'id'=>$user->getId()]);
  69.     }
  70.    
  71.  
  72.     public function loginAction(Request $request) {
  73.  
  74.         $user = $this->em
  75.             ->getRepository('App:User')
  76.             ->findOneBy(['email' => $request->request->get('email')]);
  77.  
  78.         if (!$user) {
  79.             return new JsonResponse(['status' => false,'errors' => ['form.signup.badcredentials']]);
  80.         }
  81.  
  82.         $saltedPassword = sha1($user->getSalt().$request->request->get('password'));            
  83.         $isValid = ($saltedPassword == $user->getPassword()) ? true : false;
  84.  
  85.         if (!$isValid) {
  86.             return new JsonResponse(['status' => false,'errors' => ['form.signup.badcredentials']]);
  87.         }
  88.  
  89.         $token = $this->jwtEncoder->encode([
  90.                 'username' => $user->getUsername(),
  91.                 'exp' => time() + 3600 // 1 hour expiration
  92.             ]);
  93.  
  94.         return new JsonResponse(['token' => $token, 'id'=>$user->getId(), 'username'=>$user->getUsername()]);
  95.  
  96.     }    
  97. }
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