

Autologin using Cookie / Remember Me in Dotkernel
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.

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 *
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>
Save my name, email, and website in this browser for the next time I comment.
Recommend
-
49
Automatically logging in your users when they come back to your app is a great way to keep them engaged. If you can prevent distracting them from the authentica...
-
6
containersDotKernel is built around the PSR-11 dependency container. We have chosen
-
1
Dotkernel Admin version 3 launched – DotKernel PSR-7 Middleware Applications Posted by admin on July 30, 2020 |
-
7
Doctrine caching in DotKernel Using an ORM in production without any sort of cache strategy is a very bad move. The Database server chokes; lots of CPU cycles wasted only to generate metadata an...
-
9
Posted by Merlin on April 28, 2021 | 1 Comment...
-
3
Using Postman documentation in DotKernel API 3 Starting from version 3.0 DotKernel API provides it’s documentation using Postman. In this article we will go into the details of crea...
-
5
Posted by Merlin on July 9, 2021 | No Comments...
-
5
Posted by Merlin on June 29, 2021 | No Comments
-
3
Dotkernel Frontend version 3 launched – DotKernel PSR-7 Middleware Applications Posted by admin on May 19, 2020 |
-
5
root/root). Useful for private development environments, USE AT YOUR OWN RISK. Copied from install on Ubuntu Server 12.04. · GitHub Instantly share code, notes, and snippets. ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK