Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Simple login form

Hi,

I have created simple registration page following steps in the tutorial https://olddocs.phalconphp.com/en/3.0.3/reference/tutorial.html

I want to create simple login page, how can I check if the user exists in the database?

I have created SignupController and the form, but I have no idea how to pass form data into controller and show the results.

Here is my "try"., but I feel that I'm trying to do this wrong way.

<?php

use Phalcon\Mvc\Controller;

class LoginController extends Controller 
{

    public function indexAction() 
    {

    }

    public function loginAction()
    {

        if ($this->request->isPost()) {
            $password = $this->request->getPost("password");
            $email = $this->request->getPost("email");
        }

        $success = Users::findFirst(array(
                    "username = :username: AND email = :email:",
                    "bind" == array(
                    "username" => $username,
                    "email" => $email,
        )));

        if ($success) {
            echo ('OK');
        } else {
            echo "Sorry, the following problems were generated: ";

            $messages = $user->getMessages();

            foreach ($messages as $message) {
                echo $message->getMessage(), "<br/>";
            }
        }

        $this->view->disable();
    }

}

Thank you for help in advance, Regards!



78.8k
edited Oct '17

Hi, https://github.com/calinrada/PhalconUserPlugin/blob/master/lib/Auth/Auth.php#L30

you can check that library to see how other "problems" are being handled. I advice you NOT to use it, but create you own, until you find how to handle those kind a stuff



1.5k
edited Oct '17

Thank you for your response, anyway I'm looking for simple solution. I understand, I should look at better examples of coding, but my knowledge is not on that level, and I will not understand what is going on in this code example. What is simple to you, is probably not clear enough for the beginner like me :) I not wanna be offensive in any way, but please understand my point of view. I have asked how to build a simple boat, you showed me warship plans to see how it should be done ;-)



78.8k
Accepted
answer
edited Oct '17

public function loginAction() {

            $sessions = $this->getDI()->getShared("session");

            if ($sessions->has("user_id")) {
                //if user is already logged we dont need to do anything 
                // so we redirect them to the main page
                return $this->response->redirect("/");
            }

            if ($this->request->isPost()) {

                $password = $this->request->getPost("password");
                $email = $this->request->getPost("email");

                if ($email === "") {
                    $this->flashSession->error("return enter your email");
                    //pick up the same view to display the flash session errors
                    return $this->view->pick("login");
                }

                if ($password === "") {
                    $this->flashSession->error("return enter your password");
                    //pick up the same view to display the flash session errors
                    return $this->view->pick("login");
                }

                $user = Users::findFirst([
                    "conditions" => "email = ?0 AND password = ?1",
                    "bind" == [
                        0 => $email,
                        1 => $this->security->hash($password)
                    ]
                ]);

                if (false === $user) {
                    $this->flashSession->error("wrong user / password");
                } else {
                    $sessions->set("user_id", $user->id);
                }
            }

        }


1.5k

Hi,

Thank you, I have learned this lesson. You helped me a lot!

Greetings!