Authentication

In previous example we already had access to database and user model which allow us to use user model globally as model to create user login. To create authentication in Slim, we have to create authentication controller, View and create a form where user can sign up with account and last we have to insert new user login record in our database.

1. Create user login View

Inside the resources/view create new folder called auth and add new file signup.twig

{%  extends 'templates/app.twig' %}

{% block content %}
 <div class="row">
 <div class="col-md-8 col-md-offset-2">
 <div class="card">
 <div class="card-header text-center">Sign up</div>
 <div class="card-body">
 <div class="card-text">
 <form action="{{ path_for('auth.signup') }}" method="post" 
                            autocomplete="off">
 <div class="form-group">
 <label for="email">Email</label>
 <input type="email" name="email" id="email" 
                                placeholder="your@domain.com" class="form-control">
 </div>
 <div class="form-group">
 <label for="name">Name</label>
 <input type="text" name="name" id="name" 
                                    class="form-control">
 </div>
 <div class="form-group">
 <label for="password">Password</label>
 <input type="password" name="password" id="password" 
                                    class="form-control">
 </div>
 <button type="submit" class="btn btn-default">Sign Up</button>
 </form>
 </div>
 </div>
 </div>
 </div>
 </div>
{% endblock %}

Step 2: Define User Model in app/Models/User.php 

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

Class User extends Model {
 protected $table = 'users';

 protected $fillable = [
 'email',
 'name',
 'password',
    ];

}
Step 3: Create authController
Inside the app/Controllers folder add new folder called Auth and add new file called AuthController.php. In AuthController.php we have two methods
1. getSignUp ( to render the view template auth/signup.twig file)
    This method is called when we sent get request to http://localhost/testSlim/public/auth/signup
2. postSignUp  (This is called when we submit the form)
    This method is called when we submit the login form, it will create new user and insert new user into our      database table user.

Add following code

<?php

namespace App\Controllers\Auth;

use App\Models\User;
use App\Controllers\Controller;

class AuthController extends Controller {

 public function getSignUp($req, $res){
 return $this->view->render($res, 'auth/signup.twig');
    }

 public function postSignUp($req, $res){
 // var_dump($res->getParams());
       $user = User::create([
 'name' => $req->getParam('email'),
 'email' => $req->getParam('name'),
 'password' => password_hash($req->getParam('password'), PASSWORD_DEFAULT), 
        ]);

 return $res->withRedirect($this->router->pathFor('home'));
    }
}
For the password we are using php password hashing API, first argument is data that we want to hash and second argument is algorithm we want to use.

Step 3: Define route for signup get and post methods, add following code in app/routes.php 

<?php

$app->get('/', 'HomeController:index')->setName('home');
$app->get('/auth/signup', 'AuthController:getSignUp')->setName('auth.signup');
$app->post('/auth/signup', 'AuthController:postSignUp');
We can attach name to route for controller, so we can used is as reference in our code of view and controller.

Step 4: Define AuthController in our bootstrap/app.php 

$container['AuthController'] = function($container) {
 return new \App\Controllers\Auth\AuthController($container);
};
Step 5: Insert new user account in our database table
    http://localhost/testSlim/public/auth/signup