Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controllers\Auth;
- use App\Models\User;
- use App\Controllers\Controller;
- use Respect\Validation\Validator as v;
- class AuthController extends Controller
- {
- public function getSignOut($request, $response)
- {
- unset($_SESSION['user']);
- $this->flash->addMessage('info', 'You have logged out!');
- return $response->withRedirect($this->router->pathFor(home));
- }
- public function getSignin($request, $response)
- {
- return $this->view->render($response, 'auth/signin.twig');
- }
- public function postSignin($request, $response)
- {
- $auth = $this->auth->attempt(
- $request->getParam('username'),
- $request->getParam('password')
- );
- if(!$auth) {
- $this->flash->addMessage('error', 'Could not sign you in with those credentials');
- return $response->withRedirect($this->router->pathFor('auth.signin'));
- }
- return $response->withRedirect($this->router->pathFor('home'));
- }
- public function getSignUp($request, $response)
- {
- return $this->view->render($response, 'auth/signup.twig');
- }
- public function postSignUp($request, $response)
- {
- $validation = $this->validator->validate($request, [
- 'username' => v::noWhitespace()->notEmpty(),
- // 'email' => v::noWhitespace()->notEmpty()->email()->EmailAvailable(),
- 'name' => v::noWhitespace()->notEmpty(),
- 'password' => v::noWhitespace()->notEmpty(),
- ]);
- if($validation->failed()) {
- return $response->withRedirect($this->router->pathFor('auth.signup'));
- }
- User::Create([
- 'email' => $request->getParam('email'),
- 'username' => $request->getParam('username'),
- 'name' => $request->getParam('name'),
- 'password' => password_hash($request->getParam('password'), PASSWORD_DEFAULT),
- ]);
- //$this->mail->SMTPDebug = 3; // Enable verbose debug output
- $this->mail->isSMTP(); // Set mailer to use SMTP
- $this->mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
- $this->mail->SMTPAuth = true; // Enable SMTP authentication
- $this->mail->Username = 'test'; // SMTP username
- $this->mail->Password = 'test'; // SMTP password
- $this->mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
- $this->mail->Port = 587; // TCP port to connect to
- $this->mail->setFrom('no-reply@infprod.pro', 'INFPROD');
- $this->mail->addAddress($request->getParam('email'));
- $this->mail->Subject = 'You have registered an account!';
- $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') . ' ";
- $this->mail->AltBody = 'Thanks for joining our site!';
- if(!$this->mail->send()) {
- echo 'Message could not be sent.';
- echo 'Mailer Error: ' . $this->mail->ErrorInfo;
- } else {
- echo 'Message has been sent';
- }
- $this->flash->addMessage('info', 'You have been signed up!');
- $this->auth->attempt($request->getParam('username'), $request->getParam('password'));
- return $response->withRedirect($this->router->pathFor('home'));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement