# How to use refresh tokens with a self-hosted login

**URL:** <https://community.auth0.com/t/how-to-use-refresh-tokens-with-a-self-hosted-login/76627>\
**Category:** Get Help\
**Tags:** custom-login, login, auth0\
**Created:** [January 18, 2022, 10:06pm UTC](https://community.auth0.com/t/how-to-use-refresh-tokens-with-a-self-hosted-login/76627 "2022-01-18T22:06:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cthomas](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cthomas/32/34442_2.png) [@cthomas](https://community.auth0.com/u/cthomas)\
**Post date:** [January 18, 2022, 10:06pm UTC](https://community.auth0.com/t/how-to-use-refresh-tokens-with-a-self-hosted-login/76627/1 "2022-01-18T22:06:33Z")

</div>

So I am working to implement a sign with Auth0 where we are using auth0.js WebAuth.login() with email and password against a database to login. This much of it works.

But how do refresh tokens work? Everywhere I look I see that refresh tokens can only be used in instances where the WebAuth.authenticate() is used instead (where Auth0’s hosted login page is used). I am wondering if it is possible to use refreshTokens to get authTokens where login is hosted on our site without any redirecting.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [January 20, 2022, 10:13pm UTC](https://community.auth0.com/t/how-to-use-refresh-tokens-with-a-self-hosted-login/76627/3 "2022-01-20T22:13:22Z")

</div>

Hi @cthomas,

Welcome to the Auth0 Community!

In order to return a Refresh Token you will need to pass `scope:offline_access`.

With that said, you should be aware of all of the risks associated with refresh tokens and embedded login if you are taking this approach.

---

<div class="post-metadata">

**Author:** ![cthomas](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cthomas/32/34442_2.png) [@cthomas](https://community.auth0.com/u/cthomas)\
**Post date:** [January 20, 2022, 10:28pm UTC](https://community.auth0.com/t/how-to-use-refresh-tokens-with-a-self-hosted-login/76627/4 "2022-01-20T22:28:01Z")

</div>

Thank you.

Please explain the how of it better?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [January 20, 2022, 10:47pm UTC](https://community.auth0.com/t/how-to-use-refresh-tokens-with-a-self-hosted-login/76627/5 "2022-01-20T22:47:49Z")

</div>

Which part? The security risks or how to implement?

---

<div class="post-metadata">

**Author:** ![cthomas](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cthomas/32/34442_2.png) [@cthomas](https://community.auth0.com/u/cthomas)\
**Post date:** [January 20, 2022, 11:31pm UTC](https://community.auth0.com/t/how-to-use-refresh-tokens-with-a-self-hosted-login/76627/6 "2022-01-20T23:31:10Z")

</div>

I suppose, both. Why is it so risky? But we want our own styled and build signup and login flow.

Also, how do I exchange refresh tokens using our own flow without the UX?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [January 21, 2022, 4:35pm UTC](https://community.auth0.com/t/how-to-use-refresh-tokens-with-a-self-hosted-login/76627/7 "2022-01-21T16:35:56Z")

</div>

> [@cthomas](#):
>
> Why is it so risky?

There are lots of resources out there describing the risks of the password grant and why it isn’t recommended. Auth0 isn’t the only one saying it shouldn’t be used in this type of scenario.

[This is a good starting point.](https://www.scottbrady91.com/oauth/why-the-resource-owner-password-credentials-grant-type-is-not-authentication-nor-suitable-for-modern-applications)

If you want to fully customize, but still use a redirect-based flow that Auth0 hosts, you should check this out. [Customize Classic Universal Login Pages](https://auth0.com/docs/customize/universal-login-pages/customization-classic)

> [@cthomas](#):
>
> Also, how do I exchange refresh tokens using our own flow without the UX?

I’m not sure I understand this question. How do you exchange the token without what part of the user experience?

---

<div class="post-metadata">

**Author:** ![cthomas](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cthomas/32/34442_2.png) [@cthomas](https://community.auth0.com/u/cthomas)\
**Post date:** [January 21, 2022, 7:50pm UTC](https://community.auth0.com/t/how-to-use-refresh-tokens-with-a-self-hosted-login/76627/8 "2022-01-21T19:50:49Z")

</div>

I meant, maybe I understood or not, that refresh tokens are part of an Auth0 session, managed on the auth0 servers. But the webAuth.login() with password grant doesn’t run a session on auth0 servers, but is more of an offline session? From the documentation I read, I’m under the impression that refresh tokens only work with sessions started under auth0’s login UX, like webAuth.authorize() where there is a redirect to another login flow.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [January 21, 2022, 7:59pm UTC](https://community.auth0.com/t/how-to-use-refresh-tokens-with-a-self-hosted-login/76627/9 "2022-01-21T19:59:54Z")

</div>

> [@cthomas](#):
>
> But the webAuth.login() with password grant doesn’t run a session on auth0 servers,

There is no session cookie that is created with this flow, that is correct.

> [@cthomas](#):
>
> I’m under the impression that refresh tokens only work with sessions started under auth0’s login UX

Do you mean the Universal Login _UI_ ? It’s possible to get a Refresh Token without using the UI. As I pointed out in my original response, you are able to return a Refresh Token with the password grant by passing the offline\_access scope and the correct application config in the dashboard.

Now, that doesn’t mean we would recommend using that flow from an untrusted client, but it is certainly possible. 😄

Using these flows (non-rotating refresh token and password grant) outside of a highly trusted application is not recommended. I would recommend using one of Auth0’s SDKs (or [quickstarts](https://auth0.com/docs/quickstarts/)) and universal login if you aren’t familiar with the risks and nuance of these flows.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 14, 2022, 6:19pm UTC](https://community.auth0.com/t/how-to-use-refresh-tokens-with-a-self-hosted-login/76627/10 "2022-03-14T18:19:23Z")

</div>


