Symfony authenticate user against remote API

168 views Asked by At

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!

2

There are 2 answers

0
Yassine Younes On BEST ANSWER

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

1
nehalist On

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']