5

Autologin using Cookie / Remember Me in Dotkernel

 1 year ago
source link: https://www.dotkernel.com/dotkernel/autologin-cookie-remember-me-feature/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Posted by SergiuB on July 18, 2022 | No Comments

Autologin using Cookie / Remember Me in Dotkernel

This feature is used to automatically log the user who chooses this by checking the remember me box.

Implemented in Dotkernel Frontend starting from Release 3.3.0.

Add remember me button to user interface

To add remember me button to user interface, navigate to src/User/templates/user/login.html.twig and under password element add the following code:

<div class="checkbox remember-me-checkbox">
     {% set rememberMe = form.get('rememberMe') %}
     {{ formElement(rememberMe) }}
     <p>Remember me</p>
</div>

After you’ve added the button to your template, navigate to src/User/src/Form/LoginForm.php and add the following element to your form:

$this->add([
    'name' => 'rememberMe',
    'type' => 'checkbox',
    'attributes' => [
        'class' => 'tooltips',
        'data-toggle' => 'tooltip',
        'title' => 'Remember me',
    ],
]);

After you’ve added the new element navigate src/User/src/InputFilter/LoginInputFilter.php and add the following code, this will add a filter to the remember me element added previously.

$this->add([
            'name' => 'rememberMe',
            'filters' => [
                ['name' => 'StringTrim']
            ],
            'validators' => [
                [
                    'name' => 'NotEmpty',
                    'break_chain_on_failure' => true,
                ]
            ]
        ]);

To add style to your remember me button navigate to src/App/assets/scss/components/_profile.scss and add the following css:

.remember-me-checkbox {
  input {
    display: block;
    float: left;
    margin: 4px 6px 10px 0;
    width: auto;
    height: auto;
  }
}

After you have made all the changes it’s time to compile the css in order to implement the button in interface, to do that run the following command:

npm run prod

Add functionality to remember me button

Now that you’ve added the button let’s move on to its functionality, first you have to navigate to src/User/src/Entity, create a new entity named UserRememberMe.php and modifiy it as in UserRememberMe

After you’ve created the entity, you have to create migration file for the new table using the following command:

vendor/bin/phinx create --configuration=config/migrations.php  RememberUserSchema

After the migration file is created modify it as in user_remember_schema and run the following command to add it to you database:

vendor/bin/phinx migrate --configuration=config/migrations.php

The table generated by migration is used to store data from coockie. The stored data will help to login the user utomatically.

The next step is to create a new middleware, navigate to src/App/src/Middleware and create a new file named RememberMeMiddleware.php and modify it as in RememberMeMiddleware.

After you’ve created the new middleware navigate to config/pipeline.php and add it as in pipeline.

In order to generate the cookie in the next steps you have to add a new key to your local.php, to do that navigate to config/autoload/local.php and add the following code:

'rememberMe' => [
            'cookie' => [
                'name' => 'rememberMe',
                'lifetime' => 3600 * 24 * 30,
                'samesite' => 'Lax',
                'secure' => false,
                'httponly' => true
            ]
        ],

In the following step you have to edit your src/User/src/Service/UserService.php. First you have to add 2 new properties, $defaultSessionManager used to get config and $repository used to get repository. After you have added those properties add the following methods: getRepository(), addRememberMeToken(), deleteRememberMeCookie(). as in UserService.

(don’t forget to add all new methods to interface if needed)

Now, let’s move to src/User/src/Repository/UserRepository.php and add the following methods: saveRememberUser(), getRememberUser(), findRememberMeUser(), deleteExpiredCookies(), removeRememberUser() as in UserRepository.

The last step is to go to src/User/src/Controller/UserController.php, add a new property called $config and edit your loginAction() and logoutAction() as in UserController.

Leave a Reply Cancel Reply

Your email address will not be published. Required fields are marked *

Comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Name *
Email *
Website

Save my name, email, and website in this browser for the next time I comment.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK