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!

2 Answers 2

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']
Sign up to request clarification or add additional context in comments.

1 Comment

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! :)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.