I'm new in symfony and tried to implement a auth service that allows me to register+login+getting the logged in user for my angular app. the login worked like a charm, got the token and connected successfully to the app, but when I tried to get the logged in user via token, I get "anon" when I tested it in postman, It's kinda weird because I checked every related post in github/stackoverflow, but none of them worked, I'm stuck for 6 hours now. Here is some of my code.
the method in my controller :
/**
* @Route("/get/profile", name="api_get_user")
* @Method("POST")
*/
public function getUserAction()
{
$user = $this->get('security.token_storage')->getToken()->getUser();
$serializer = $this->get('jms_serializer');
$jsonContent = $serializer->serialize($user, 'json');
$response = new Response(json_encode(array('response' => 'OK', 'data'=> $jsonContent)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
My Security.yml :
# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
healthcheck:
pattern: ^/ping$
anonymous: true
api_docs:
pattern: ^/doc
anonymous: true
api_register:
pattern: ^/register
anonymous: true
api_password_reset:
pattern: ^/password/reset
anonymous: true
login:
pattern: ^/api/login
stateless: true
anonymous: true
form_login:
check_path: /api/login_check
require_previous_session: false
username_parameter: username
password_parameter: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator : security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4
logout: true
anonymous: true
access_control:
- { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
my User class :
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use JMS\Serializer\Annotation as JMSSerializer;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* User constructor.
*/
public function __construct()
{
parent::__construct();
}
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @JMSSerializer\Expose
* @JMSSerializer\Type("string")
* @JMSSerializer\Groups({"users_all","users_summary"})
*/
protected $id;
/**
* @JMSSerializer\Expose
* @JMSSerializer\Type("string")
* @JMSSerializer\Groups({"users_all","users_summary"})
*/
protected $username;
/**
* @var string The email of the user.
*
* @JMSSerializer\Expose
* @JMSSerializer\Type("string")
* @JMSSerializer\Groups({"users_all","users_summary"})
*/
protected $email;
}
As I said in postman when I put the token I get anon as a response it means anonymous and I don't know why. Thanks.