# How to separate Run frontend and backend for frontend?

**URL:** <https://community.auth0.com/t/how-to-separate-run-frontend-and-backend-for-frontend/71872>\
**Category:** Get Help\
**Tags:** api, login, auth0\
**Created:** [October 22, 2021, 10:15am UTC](https://community.auth0.com/t/how-to-separate-run-frontend-and-backend-for-frontend/71872 "2021-10-22T10:15:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dharam.parmar](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dharam.parmar/32/25684_2.png) [@dharam.parmar](https://community.auth0.com/u/dharam.parmar)\
**Post date:** [October 22, 2021, 10:15am UTC](https://community.auth0.com/t/how-to-separate-run-frontend-and-backend-for-frontend/71872/1 "2021-10-22T10:15:23Z")

</div>

Hello team,

I have implemented the backend for the frontend pattern (BFF).  
I followed this article [Backend For Frontend Authentication Pattern with Auth0 and ASP.NET Core](https://auth0.com/blog/backend-for-frontend-pattern-with-auth0-and-dotnet/)  
It works perfectly for me.  
But I want to run both frontend and backend for frontend in different ports. Currently react project and backend run in the same port.  
I want to run different ports to deploy different environments.  
I just want to know is it possible to run separately react project and backend project with differents ports.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [October 22, 2021, 10:49am UTC](https://community.auth0.com/t/how-to-separate-run-frontend-and-backend-for-frontend/71872/2 "2021-10-22T10:49:56Z")

</div>

Hey there!

I guess the potential answer will be as with this Stackoverflow question:

> <https://stackoverflow.com/questions/61007392/it-is-possible-to-run-two-apps-on-different-ports-in-single-django-project>

but maybe I’m wrong and @cibrax will share some insight as he’s the author of that article 🙂

---

<div class="post-metadata">

**Author:** ![cibrax](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cibrax/32/27644_2.png) [@cibrax](https://community.auth0.com/u/cibrax)\
**Post date:** [October 22, 2021, 12:56pm UTC](https://community.auth0.com/t/how-to-separate-run-frontend-and-backend-for-frontend/71872/3 "2021-10-22T12:56:19Z")

</div>

Hello,

The following code change should do that trick

```csharp
app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    //spa.UseReactDevelopmentServer(npmScript: "start");
                    spa.UseProxyToSpaDevelopmentServer("https://yourreactserver");
                }
            });

```

That should point the .NET app to a different development server for the react app running in a different port (and it would use the react proxy to emulate requests from the same domain). Otherwise, if you change domains, this BFF approach will not work as it relies on cookies being passed from the frontend to the backend.

Thanks  
Pablo.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [October 22, 2021, 12:58pm UTC](https://community.auth0.com/t/how-to-separate-run-frontend-and-backend-for-frontend/71872/4 "2021-10-22T12:58:52Z")

</div>

Thanks for helping on this one @cibrax !

---

<div class="post-metadata">

**Author:** ![dharam.parmar](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dharam.parmar/32/25684_2.png) [@dharam.parmar](https://community.auth0.com/u/dharam.parmar)\
**Post date:** [October 25, 2021, 4:54am UTC](https://community.auth0.com/t/how-to-separate-run-frontend-and-backend-for-frontend/71872/5 "2021-10-25T04:54:02Z")

</div>

Thank you for your support. It is very useful information. I will try that and then let you know.  
Thanks again guys

---

<div class="post-metadata">

**Author:** ![dharam.parmar](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dharam.parmar/32/25684_2.png) [@dharam.parmar](https://community.auth0.com/u/dharam.parmar)\
**Post date:** [October 25, 2021, 6:19am UTC](https://community.auth0.com/t/how-to-separate-run-frontend-and-backend-for-frontend/71872/6 "2021-10-25T06:19:31Z")

</div>

Hi Pablo,

```
Thanks for your help. I tried your solution but facing some problems.

```

```auto
app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    //spa.UseReactDevelopmentServer(npmScript: "start");
                    spa.UseProxyToSpaDevelopmentServer("http://localhost:3000/");
                }
            });

```

Backend listen on [https://localhost:44331/](https://localhost:44331/) this URL.

Frontend listen on [http://localhost:3000/](http://localhost:3000/) this URL.

When I am trying to hit login from [http://localhost:3000/](http://localhost:3000/) this URL login method is not called.

When I am trying to hit login from [https://localhost:44331/](https://localhost:44331/) this URL login method is called.

The login method returns ChallengeResult from auth0. I mean return login screen.

```auto
public ActionResult Login(string returnUrl = "/")
        {
            return new ChallengeResult("Auth0", new AuthenticationProperties() { RedirectUri = returnUrl });
        }

```

I do not understand why the login method is not called from [http://localhost:3000/](http://localhost:3000/)

Also, I noticed GetUser method is called from [http://localhost:3000/](http://localhost:3000/)

```auto
 public ActionResult GetUser()
        {
            if (User.Identity.IsAuthenticated)
            {
                var claims = ((ClaimsIdentity)this.User.Identity).Claims.Select(c =>
                    new { type = c.Type, value = c.Value })
                    .ToArray();

                return Json(new { isAuthenticated = true, claims = claims });
            }

            return Json(new { isAuthenticated = false });
        }

```

Here is my AuthController.

```auto
public class AuthController: Controller
    {

        public ActionResult Login(string returnUrl = "/")
        {
            return new ChallengeResult("Auth0", new AuthenticationProperties() { RedirectUri = returnUrl });
        }

        [Authorize]
        public async Task<ActionResult> Logout()
        {
            await HttpContext.SignOutAsync();
            return new SignOutResult("Auth0", new AuthenticationProperties
            {
                RedirectUri = Url.Action("Index", "Home")
                //RedirectUri = Url.Action("Index", "fetch-data")
            });
        }

        public ActionResult GetUser()
        {
            if (User.Identity.IsAuthenticated)
            {
                var claims = ((ClaimsIdentity)this.User.Identity).Claims.Select(c =>
                    new { type = c.Type, value = c.Value })
                    .ToArray();

                return Json(new { isAuthenticated = true, claims = claims });
            }

            return Json(new { isAuthenticated = false });
        }
    }

```

The Login method returned a view and get user method returned data.  
Please help me to understand this problem. I want to work BFF application on different ports.  
Also, With Authentication login and logout. I set up all callback URLs and logout URL.

Thanks

---

<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:** [November 9, 2021, 6:19am UTC](https://community.auth0.com/t/how-to-separate-run-frontend-and-backend-for-frontend/71872/7 "2021-11-09T06:19:53Z")

</div>

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