Advertisement
Guest User

Untitled

a guest
Oct 9th, 2017
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controllers\Auth;
  4.  
  5. use App\Models\User;
  6. use App\Controllers\Controller;
  7. use Respect\Validation\Validator as v;
  8.  
  9. class AuthController extends Controller
  10. {
  11.  
  12. public function getSignOut($request, $response)
  13. {
  14. unset($_SESSION['user']);
  15. $this->flash->addMessage('info', 'You have logged out!');
  16. return $response->withRedirect($this->router->pathFor(home));
  17. }
  18.  
  19. public function getSignin($request, $response)
  20. {
  21. return $this->view->render($response, 'auth/signin.twig');
  22. }
  23.  
  24. public function postSignin($request, $response)
  25. {
  26. $auth = $this->auth->attempt(
  27. $request->getParam('username'),
  28. $request->getParam('password')
  29. );
  30.  
  31. if(!$auth) {
  32. $this->flash->addMessage('error', 'Could not sign you in with those credentials');
  33. return $response->withRedirect($this->router->pathFor('auth.signin'));
  34. }
  35. return $response->withRedirect($this->router->pathFor('home'));
  36. }
  37.  
  38.  
  39. public function getSignUp($request, $response)
  40. {
  41. return $this->view->render($response, 'auth/signup.twig');
  42. }
  43.  
  44. public function postSignUp($request, $response)
  45. {
  46. $validation = $this->validator->validate($request, [
  47. 'username' => v::noWhitespace()->notEmpty(),
  48. // 'email' => v::noWhitespace()->notEmpty()->email()->EmailAvailable(),
  49. 'name' => v::noWhitespace()->notEmpty(),
  50. 'password' => v::noWhitespace()->notEmpty(),
  51. ]);
  52.  
  53. if($validation->failed()) {
  54. return $response->withRedirect($this->router->pathFor('auth.signup'));
  55. }
  56.  
  57. User::Create([
  58. 'email' => $request->getParam('email'),
  59. 'username' => $request->getParam('username'),
  60. 'name' => $request->getParam('name'),
  61. 'password' => password_hash($request->getParam('password'), PASSWORD_DEFAULT),
  62. ]);
  63.  
  64. //$this->mail->SMTPDebug = 3; // Enable verbose debug output
  65. $this->mail->isSMTP(); // Set mailer to use SMTP
  66. $this->mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
  67. $this->mail->SMTPAuth = true; // Enable SMTP authentication
  68. $this->mail->Username = 'test'; // SMTP username
  69. $this->mail->Password = 'test'; // SMTP password
  70. $this->mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
  71. $this->mail->Port = 587; // TCP port to connect to
  72.  
  73. $this->mail->setFrom('no-reply@infprod.pro', 'INFPROD');
  74. $this->mail->addAddress($request->getParam('email'));
  75.  
  76. $this->mail->Subject = 'You have registered an account!';
  77. $this->mail->Body = "Thanks for joining our site!<br> Login here: <a href=' . $this->router->pathFor('auth.signin') .'>Infprod.pro</a><br>Username:' . $request->getParam('username') . ' ";
  78. $this->mail->AltBody = 'Thanks for joining our site!';
  79.  
  80. if(!$this->mail->send()) {
  81. echo 'Message could not be sent.';
  82. echo 'Mailer Error: ' . $this->mail->ErrorInfo;
  83. } else {
  84. echo 'Message has been sent';
  85. }
  86.  
  87. $this->flash->addMessage('info', 'You have been signed up!');
  88.  
  89. $this->auth->attempt($request->getParam('username'), $request->getParam('password'));
  90.  
  91. return $response->withRedirect($this->router->pathFor('home'));
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement