0

I have an issue, more or less the same as this guy: Symfony 3 authenticate user against remote API

But my problem is, what do I do with the token?

I used guard, I made an api service where I can retrieve the token, but where can I store it? I wanted to set a cookie but I can't write the cookie from my service, or I won't be able to start the session, so in the answer I don't understand the: "store token in your frontend storage".

There is really no way for me to store the token in a cookie?

Thank you!

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
Arvi89
  • 41
  • 8

2 Answers2

2

Why shouldn't it be possible not to use sessions from services? Inject the session component into your service and you're good to go.

class TheService {
  public function __construct(SessionInterface $session) {
    $this->session = $session;
  }

  public function stuff() {
    $this->session->set('my_token', 'h3ll0');
  }
}

See http://symfony.com/doc/current/components/http_foundation/sessions.html

In case you're on a current version of Symfony its autowiring feature will take care of injecting the session service, otherwise you'd need to configure the service in your services.yml:

services:
  my_service:
    class: AppBundle\Service\TheService
    public: true
    arguments: ['@session']
nehalist
  • 1,434
  • 18
  • 45
  • Actually I can use the session, I don't know why I was not thinking about that, I said I couldn't store in a cookie, but the session was a much better choice anyway. Thx! :) – Arvi89 Feb 08 '18 at 23:46
1

It's not so clear what he meant, but i think the best solution is storing it in the session variable.

Yassine Younes
  • 870
  • 10
  • 22