I'm building an app where a shop owner connects to their Judge.me account for further operations.
Regarding the authentication to Judge.me, here's what I've done :
credentials.jinja
<form method="POST">
<article class="card-block">
<h4>Connect Judge.me account</h4>
<label>
<p>Shop domain</p>
<input type="text" name="shop_domain" placeholder="example.myshopify.com" required="">
</label>
<label>
<p>Private API key</p>
<input type="text" name="api_token" placeholder="xxxxxx xxxxx xxxxx xxxxx" required="">
<small>
You can find your private API key
<a href="#" title="find api key">on the Judge.me admin dashboard</a>.
</small>
</label>
</article>
<div class="form-actions">
<span></span>
<button class="button main" type="submit">Connect</button>
</div>
</form>
views.py
@router.post("/credentials/")
async def connect_to_judgeme(
request: Request,
shop_domain: str = Form(),
api_token: str = Form(),
merchant: MerchantDoc = Depends(get_authenticated_merchant),
) -> Response:
"""Connect the merchant to their Judgeme account."""
judgeme_client = JudgemeClient(judgeme_api_token=api_token)
# Check the submitted Judgeme credentials
try:
await judgeme_client.get(
path="https://judge.me/api/v1/reviews", params={"api_token": api_token, "shop_domain": shop_domain}
)
except Rest401Error as e:
# Override error message to make it clearer for the user
e.message = "Shop domain or api token is wrong. Please check your credentials in your Judgeme dashboard."
utils.Flash.add_message(request=request, message=e.message, level=utils.FlashLevel.ERROR)
return templates.TemplateResponse(
"judgeme/credentials.jinja",
context={
"request": request,
"form": {},
"merchant": merchant,
},
)
merchant.judgeme_shop_domain = shop_domain # Set shop_domain of merchant
merchant.judgeme_access_token = api_token # Set access_token of merchant
await merchant.save()
return RedirectResponse(url="/judgeme/pages/", status_code=303)
The authentication part works fine and done like this, with the status_code = 303
, the redirection from a POST at /judgeme/pages/credentials/
to /judgeme/pages/
works fine too : the user submits their credentials and when they are the right ones, they are recorded and there's a redirection to /judgeme/pages/
endpoint.
My question here is can we do that redirection without having to put another specific status_code
than the default 307
one ?
With the default status_code = 307
, I get a Method not allowed
error which I understand, as the /judgeme/pages/
endpoint does not have a POST method defined for it.
I've also tried to return a TemplateResponse
(see below) rather than make a redirection with RedirectResponse
.
@router.post("/credentials/")
async def connect_to_judgeme(
request: Request,
shop_domain: str = Form(),
api_token: str = Form(),
merchant: MerchantDoc = Depends(get_authenticated_merchant),
) -> Response:
"""Connect the merchant to their Judgeme account."""
judgeme_client = JudgemeClient(judgeme_api_token=api_token)
# Check the submitted Judgeme credentials
try:
await judgeme_client.get(
path="https://judge.me/api/v1/reviews", params={"api_token": api_token, "shop_domain": shop_domain}
)
except Rest401Error as e:
# Override error message to make it clearer for the user
e.message = "Shop domain or api token is wrong. Please check your credentials in your Judgeme dashboard."
utils.Flash.add_message(request=request, message=e.message, level=utils.FlashLevel.ERROR)
return templates.TemplateResponse(
"judgeme/credentials.jinja",
context={
"request": request,
"form": {},
"merchant": merchant,
},
)
merchant.judgeme_shop_domain = shop_domain # Set shop_domain of merchant
merchant.judgeme_access_token = api_token # Set access_token of merchant
await merchant.save()
return templates.TemplateResponse(
"judgeme/home.jinja",
context={
"request": request,
"form": {},
"merchant": merchant,
},
)
The issue here is that the TemplateResponse
of home.jinja
template is well rendered but the url stays the previous judgeme/pages/credentials/
.
Is there any other way to deal with the redirection or where am I getting things wrong ?