I started the Mercure hub with this command on Windows10 :
mercure --jwt-key="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOltdfX0.Oo0yg7y4yMa1vr_bziltxuTCqb8JVHKxp-f_FwwOim0" --addr="localhost:3000" --allow-anonymous --cors-allowed-origins="*"
The hub run successfully
I want to publish updates from Symfony :
In the .env file there is :
###> symfony/mercure-bundle ###
# See https://symfony.com/doc/current/mercure.html#configuration
MERCURE_PUBLISH_URL=http://mercure/.well-known/mercure
# The default token is signed with the secret key: !ChangeMe!
MERCURE_JWT_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOltdfX0.Oo0yg7y4yMa1vr_bziltxuTCqb8JVHKxp-f_FwwOim0
###< symfony/mercure-bundle ###
In the lexik_jwt_authentication.yaml
:
lexik_jwt_authentication:
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
token_ttl: '%env(JWT_TOKEN_TTL)%'
user_identity_field: username
Then in the Symfony :
/**
* @RestPost("/api/insertUpdateSynonyms", name ="api_insert_update_synonyms")
* @RestRequestParam(name="old_cle", nullable=true)
* @RestRequestParam(name="add_synonymes", nullable=true)
* @RestRequestParam(name="new_cle", nullable=true)
* @RestRequestParam(name="new_synonymes", nullable=true)
*
* @return JsonResponse
*/
public function insertUpdateSynonyms(ParamFetcher $paramFetcher, SearchService $searchService, PublisherInterface $publisher)
{
try {
$retour = $searchService->insertUpdateSynonyms($paramFetcher);
$update = new Update(
'http://bdc.pulse.mg/notif/suggestioncontenu',
json_encode(['msg' => '============================= new content updated ! ................................'])
);
$publisher($update);
} catch (Exception $e) {
$retour = array('error' => array('error_code' => $e->getCode(), 'message' => $e->getMessage()));
}
$response = new JsonResponse();
$response->setData($retour);
return $response;
}
In the Angular side :
insertSynonyme(){
const data = {
old_cle: this.synoForm.value.cle_syno,
add_synonymes: this.dataSynonyme.oldSynonyme
};
this.generalServ.insertSynonyme(data).subscribe(
res => {
console.log(res);
if(res.code === 200){
this.toastr.success('Sent');
this.submitted = true;
// this._document.defaultView.location.reload();
}
});
}
At runtime the console shows error_code 401 : HTTP/1.1 401 Unauthorized returned for "http://localhost:3000/.well-known/mercure
So what is wrong ?
Source: Symfony4 Questions
Was this helpful?
0 / 0