EDIT
je pense que c'est effectivement la fonction de hashage bcrypt qui ne fonctionne pas
lorsque je passe en plaintext, le login fonctionne
j'ai essayé en SHA512 pour voir en essayant de décrypte en ligne et il ne me trouve aucune correspondance...
Bonjour,
je débute en Symfony et j'ai suivi le tuto de Lior sur Symfony et tout fonctionne à l'exception de la connexion
j'utilise Symfony 4 lié à une base MySql (MAMP)
j'ai créé des utilisateurs (class Abonne), en utilisant l'encoder bcrypt pour le mot de passe et mes utilisateurs sont correctement enregistrés en base
cependant, tout se passe comme si mon mot de passe n'était pas reconnu et j'obtiens le message "Invalid credentials." (qui devrait être traduit d'ailleurs mais on verra après).
çà fait 3h que je cherche en vain
j'ai vu la méthode que j'utilise sur d'autre tuto en ligne et cela semble fonctionner...
d'avance merci pour votre aide
security.yaml
Code yaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 security: encoders: App\Entity\Abonne: algorithm: bcrypt # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers providers: in_memory: { memory: ~ } in_database: entity: class: App\Entity\Abonne property: email firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: anonymous: true provider: in_database form_login: login_path: security_login check_path: security_login
securityController.php
abonne.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 <?php namespace App\Controller; use App\Entity\Abonne; use App\Form\RegistrationType; use Symfony\Component\HttpFoundation\Request; use Doctrine\Common\Persistence\ObjectManager; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; class SecurityController extends AbstractController { /** * @Route("/inscription", name="security_registration") */ public function registration(Request $request, ObjectManager $manager,UserPasswordEncoderInterface $encoder) { $abonne = new Abonne(); $form = $this->createForm(RegistrationType::class, $abonne); $form->handleRequest($request); if($form->isSubmitted() && $form->isValid()){ $hash = $encoder->encodePassword($abonne, $abonne->getMotDePasseAbonne()); $abonne->setMotDePasseAbonne($hash); $manager->persist($abonne); $manager->flush(); return $this->redirectToRoute('security_login'); } return $this->render('security/registration.html.twig', [ 'form' => $form->createView() ]); } /** * @Route("/connexion", name="security_login") */ public function login(Request $request, AuthenticationUtils $authenticationUtils){ // get the login error if there is one $error = $authenticationUtils->getLastAuthenticationError(); // last username entered by the user $lastUsername = $authenticationUtils->getLastUsername(); return $this-> render('security/login.html.twig', array( 'last_username' => $lastUsername, 'error' => $error,)); } }
login.html.twig
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 <?php namespace App\Entity; use Serializable; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; /** * @ORM\Entity(repositoryClass="App\Repository\AbonneRepository") * @UniqueEntity( * fields={"email"}, * message= "L'email que vous avez tapé est déjà utilisé" * ) */ class Abonne implements UserInterface { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $nomAbonne; /** * @ORM\Column(type="string", length=255) */ private $PrenomAbonne; /** * @ORM\Column(type="string", length=20) * @Assert\Length(min="8", minMessage="Votre mot de passe doit faire 8 caractères minimum") */ private $motDePasseAbonne; /** * @Assert\EqualTo(propertyPath="motDePasseAbonne", message="Mot de passe non confirmé") */ public $confirmMotDePasseAbonne; /** * @ORM\Column(type="string", length=255) * @Assert\Email() */ private $email; public function getId(): ?int { return $this->id; } public function getNomAbonne(): ?string { return $this->nomAbonne; } public function getUsername(): ?string { return $this->nomAbonne; } public function setNomAbonne(string $nomAbonne): self { $this->nomAbonne = $nomAbonne; return $this; } public function getPrenomAbonne(): ?string { return $this->PrenomAbonne; } public function setPrenomAbonne(string $PrenomAbonne): self { $this->PrenomAbonne = $PrenomAbonne; return $this; } public function getMotDePasseAbonne(): ?string { return $this->motDePasseAbonne; } public function getPassword(): ?string { return $this->motDePasseAbonne; } public function setMotDePasseAbonne(string $motDePasseAbonne): self { $this->motDePasseAbonne = $motDePasseAbonne; return $this; } public function getEmail(): ?string { return $this->email; } public function setEmail(string $email): self { $this->email = $email; return $this; } //méthode à implémenter public function eraseCredentials(){} public function getSalt() { return null; } public function getRoles() { return['ROLE_USER']; } }
Code twig : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 {% extends 'base.html.twig' %} {% block body %} <h1>Connexion</h1> {% if error %} <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div> {% endif %} <form action="{{ path('security_login') }}" method="post"> <div class="form-group"> <input placeholder="Adresse email" required name="_username" value="{{ last_username }}" type="text" class="form-control"> </div> <div class="form-group"> <input placeholder="Mot de passe" required name="_password" type="password" class="form-control"> </div> <div class="form-group"> <button class="btn btn-success">Connexion</button> </div> </form> {% endblock %}
Partager