# How to set up a authorization code flow correctly?

**URL:** <https://community.auth0.com/t/how-to-set-up-a-authorization-code-flow-correctly/162642>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [December 14, 2024, 12:19pm UTC](https://community.auth0.com/t/how-to-set-up-a-authorization-code-flow-correctly/162642 "2024-12-14T12:19:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bryce.wayne.person](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bryce.wayne.person/32/59788_2.png) [@bryce.wayne.person](https://community.auth0.com/u/bryce.wayne.person)\
**Post date:** [December 14, 2024, 12:19pm UTC](https://community.auth0.com/t/how-to-set-up-a-authorization-code-flow-correctly/162642/1 "2024-12-14T12:19:35Z")

</div>

Standard web application. Frontend+Backend(Golang). Frontend uses backend API  
Regular Web Application type in auth0 dashboard. Using authorization code flow.  
I am implementing this scenario:

1. The user goes to the personal account url(aka /login). The backend understands that it does not have a cookie and redirects to AuthURL auth0. The user sign up(sign in) through the login page (passed the authorization code flow). The resulting opaque access token backend save in a cookie. And also i save refresh token for this access token in database.  
Next, all API requests from the frontend are sent with the received opaque access token in cookie.
2. For each API request from the frontend like this, backend do this: **validate the access token(signature)** from the cookie → see if it is expired → if it is expired, then refresh it using refresh token. And set new access token in cookie. P.S. Refresh token rotation is on of course.

In this scenario I encountered 2 problems

- How to get a valid jwt access token(not opaque) whose signature I could then check. To get a valid jwt access token you need to use the API (not application) to be able to pass the “audience”, but for the API I can no longer use Regular web application type and authorization code flow, I get the error “Grant type ‘authorization\_code’ not allowed for the client”. I can only use Mashine To Mashine application type and client credential flow.
- How to change the lifetime of an access token. By default it is 24 hours, which is quite a long time. And again, I can change the lifetime of the access token only if I use the API, encountering all the same consequences as in point 1

As a result, it turns out that if I use authorization code flow, then I cannot:

- I can`t check the received access token(opaque) for signature
- I can’t configure the access token lifetime

Could you please tell me if I understand everything correctly?

---

<div class="post-metadata">

**Author:** ![dawid.matuszczyk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dawid.matuszczyk/32/74550_2.png) [@dawid.matuszczyk](https://community.auth0.com/u/dawid.matuszczyk)\
**Post date:** [December 16, 2024, 2:47pm UTC](https://community.auth0.com/t/how-to-set-up-a-authorization-code-flow-correctly/162642/3 "2024-12-16T14:47:11Z")

</div>

Hi @bryce.wayne.person

Thank you for posting your question,

Based on your description, you can create an API pointing to the backend; this way, when calling for an access token, you can pass your Backend API identifier as the audience, and your access token will be valid JWT. This way, you can set a shorter expiration date for the token.

Here’s a quickstart for Golang as API → [Auth0 Go API SDK Quickstarts: Add authorization to a Go API](https://auth0.com/docs/quickstart/backend/golang/interactive)

If you don’t want to follow that and continue to use the opaque access token, you are not supposed to validate them. Instead, they are validated on the Auth0 server when being passed in the request for the `/userinfo` endpoint.

I hope this answers your questions.  
Thanks  
Dawid

---

<div class="post-metadata">

**Author:** ![bryce.wayne.person](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bryce.wayne.person/32/59788_2.png) [@bryce.wayne.person](https://community.auth0.com/u/bryce.wayne.person)\
**Post date:** [December 16, 2024, 5:31pm UTC](https://community.auth0.com/t/how-to-set-up-a-authorization-code-flow-correctly/162642/4 "2024-12-16T17:31:34Z")

</div>

Thanks for the answer!  
Could you tell me please, but if I use the API as you recommend, I will no longer be able to implement the authorization code flow(only client credential flow). And accordingly, I will not be able to show the sign in/sigh up page to user  
Do I understand correctly?

---

<div class="post-metadata">

**Author:** ![dawid.matuszczyk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dawid.matuszczyk/32/74550_2.png) [@dawid.matuszczyk](https://community.auth0.com/u/dawid.matuszczyk)\
**Post date:** [December 16, 2024, 5:46pm UTC](https://community.auth0.com/t/how-to-set-up-a-authorization-code-flow-correctly/162642/5 "2024-12-16T17:46:06Z")

</div>

Hi @bryce.wayne.person

You don’t need to switch to the client credential flow; you can still use the Authorization code flow. We have excellent documentation on how to call an API using the Authorization Code Flow with setup by setup guide → [Call Your API Using the Authorization Code Flow](https://auth0.com/docs/get-started/authentication-and-authorization-flow/authorization-code-flow/call-your-api-using-the-authorization-code-flow)

Thanks  
Dawid

---

<div class="post-metadata">

**Author:** ![bryce.wayne.person](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bryce.wayne.person/32/59788_2.png) [@bryce.wayne.person](https://community.auth0.com/u/bryce.wayne.person)\
**Post date:** [December 17, 2024, 11:48am UTC](https://community.auth0.com/t/how-to-set-up-a-authorization-code-flow-correctly/162642/6 "2024-12-17T11:48:45Z")

</div>

Thank you!  
Now everything works as it should!  
Please tell me the last question - in the received JWT access token in the aud field I get 2 audiences (an array of 2 values)

- audience of API is what I’m expecting
- and AUTH0\_HOST/userinfo

Is it possible to prevent AUTH0\_HOST/userinfo from coming?

---

<div class="post-metadata">

**Author:** ![dawid.matuszczyk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dawid.matuszczyk/32/74550_2.png) [@dawid.matuszczyk](https://community.auth0.com/u/dawid.matuszczyk)\
**Post date:** [December 17, 2024, 12:35pm UTC](https://community.auth0.com/t/how-to-set-up-a-authorization-code-flow-correctly/162642/7 "2024-12-17T12:35:13Z")

</div>

Hi @bryce.wayne.person,

I’m glad it’s working! You can prevent it from happening by not including the `openid` in the scope of your request, but that also means that your token won’t be usable with the /user info endpoint, and your access token won’t include the user information.

> **[Get Access Tokens](https://auth0.com/docs/secure/tokens/access-tokens/get-access-tokens#multiple-audiences)**
>
> Learn how to request Access Tokens using the Authorize endpoint when authenticating users and include the target audience and scope of access requested by the app and granted by the user.

Thanks  
Dawid

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [December 31, 2024, 12:35pm UTC](https://community.auth0.com/t/how-to-set-up-a-authorization-code-flow-correctly/162642/8 "2024-12-31T12:35:32Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
