Skip to content

Commit 5214fde

Browse files
author
Jill Grant
authored
Merge pull request #279917 from craigshoemaker/swa/direct-deploy-quickstart
[Static Web Apps] New: Direct deploy quickstart & add authentication tutorial
2 parents 974a35e + 735e3b1 commit 5214fde

14 files changed

+460
-7
lines changed

articles/static-web-apps/TOC.yml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@
1313
- name: Build your first static web app
1414
expanded: true
1515
items:
16-
- name: Visual Studio Code
17-
href: getting-started.md
18-
- name: Azure portal
19-
href: get-started-portal.md
20-
displayName: github, devops
21-
- name: Azure CLI
22-
href: get-started-cli.md
16+
- name: Web frameworks
17+
href: deploy-web-framework.md
18+
- name: Developer tools
19+
items:
20+
- name: Visual Studio Code
21+
href: getting-started.md
22+
- name: Azure portal
23+
href: get-started-portal.md
24+
displayName: github, devops
25+
- name: Azure CLI
26+
href: get-started-cli.md
27+
- name: Scenarios
28+
items:
29+
- name: Build a site with a web framework
30+
items:
31+
- name: 1 - Pick a web framework
32+
href: deploy-web-framework.md
33+
- name: 2 - Add authentication
34+
href: add-authentication.md
2335
- name: Frameworks & libraries
2436
items:
2537
- name: Overview
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: Add authentication to your static site in Azure Static Web Apps
3+
description: Learn to add authentication to your static site with Azure Static Web Apps.
4+
services: static-web-apps
5+
author: craigshoemaker
6+
ms.service: static-web-apps
7+
ms.topic: tutorial
8+
ms.date: 07/02/2024
9+
ms.author: cshoe
10+
---
11+
12+
# Add authentication to your static site in Azure Static Web Apps
13+
14+
This article is part two in a series that show you how to deploy your first site to Azure Static Web Apps. Previously, you created and deployed a static site with the [web framework](./deploy-web-framework.md) of your choice.
15+
16+
In this article, you add authentication to your site and run the site locally before deploying to the cloud.
17+
18+
## Prerequisites
19+
20+
This tutorial continues from the previous tutorial, and has the same [prerequisites](deploy-web-framework.md#prerequisites).
21+
22+
## Authentication and authorization
23+
24+
Azure Static Web Apps makes it easy to use common authentication providers like Microsoft Entra and Google without writing security-related code.
25+
26+
> [!NOTE]
27+
> You can optionally [register a custom provider and assign custom roles](./authentication-custom.md) for more fine-grained control when using backend APIs.
28+
29+
In this article, you configure your site to use Microsoft Entra ID for authentication.
30+
31+
## Add authentication
32+
33+
In the last article, you created a `staticwebapp.config.json` file. This file [controls many features](./configuration.md) for Azure Static Web Apps, including authentication.
34+
35+
1. Update the `staticwebapp.config.json` to match the following configuration.
36+
37+
```json
38+
{
39+
"navigationFallback": {
40+
"rewrite": "/index.html"
41+
},
42+
"routes": [
43+
{
44+
"route": "/*",
45+
"allowedRoles": [ "authenticated" ]
46+
}
47+
],
48+
"responseOverrides": {
49+
"401": {
50+
"statusCode": 302,
51+
"redirect": "/.auth/login/aad"
52+
}
53+
}
54+
}
55+
```
56+
57+
The `routes` section allows you to restrict access to named roles. There are two predefined roles: `authenticated` and `anonymous`. If the connected user doesn't have an allowed role, the server returns a "401 Unauthorized" response.
58+
59+
The values in the `responseOverrides` section configure your site so that instead of an unauthenticated user seeing a server error, their browser is redirected to the sign-in page instead.
60+
61+
1. Run the site locally.
62+
63+
To launch the site locally, run the [Static Web Apps CLI](https://azure.github.io/static-web-apps-cli) `start` command.
64+
65+
```bash
66+
npx swa start
67+
```
68+
69+
This command starts the Azure Static Web Apps emulator on `http://localhost:4280`.
70+
71+
This URL is shown in your terminal window after the service starts up.
72+
73+
1. Select the URL to go to the site.
74+
75+
Once you open the site in your browser, the local authentication sign in page is displayed.
76+
77+
![A screen shot of the local authentication sign in page.](./media/add-authentication/local-auth-page.png)
78+
79+
The local authentication sign in page provides an emulation of the real authentication experience without the need for external services. You can create a user ID and select which roles you'd like to apply to the user from this screen.
80+
81+
1. Enter a username, then select **Login**.
82+
83+
Once you authenticate, your site is displayed.
84+
85+
## Deploy the site to Azure
86+
87+
Deploy your site in the same way as you did in the last tutorial.
88+
89+
1. Build your site:
90+
91+
```bash
92+
npx swa build
93+
```
94+
95+
1. Deploy your site to the static web app:
96+
97+
```bash
98+
npx swa deploy --app-name swa-demo-site
99+
```
100+
101+
The URL for your site is displayed once the deployment is finished. Select the site URL to open the site in the browser. The standard Microsoft Entra ID sign in page is displayed:
102+
103+
![Screen shot of the Microsoft authentication sign in page.](./media/add-authentication/remote-auth-page.png)
104+
105+
Sign in with your Microsoft account.
106+
107+
[!INCLUDE [Clean up resources](../../includes/static-web-apps/quickstart-direct-deploy-clean-up-resources.md)]
108+
109+
## Related content
110+
111+
* [Authentication and authorization](./authentication-authorization.yml)
112+
* [Custom authentication](./authentication-custom.md)
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
---
2+
title: Deploy your web app to Azure Static Web Apps.
3+
description: Learn to deploy your web app to Azure Static Web Apps.
4+
services: static-web-apps
5+
author: craigshoemaker
6+
ms.service: static-web-apps
7+
ms.topic: article
8+
ms.date: 07/02/2024
9+
ms.author: cshoe
10+
zone_pivot_groups: swa-web-framework
11+
---
12+
13+
# Deploy your web app to Azure Static Web Apps
14+
15+
In this article, you create a new web app with the framework of your choice, run it locally, then deploy to Azure Static Web Apps.
16+
17+
## Prerequisites
18+
19+
To complete this tutorial, you need:
20+
21+
| Resource | Description |
22+
|---|---|
23+
| [Azure subscription][1] | If you don't have one, you can [create an account for free][1]. |
24+
| [Node.js][2] | Install version 20.0 or later. |
25+
| [Azure CLI][3] | Install version 2.6x or later. |
26+
27+
You also need a text editor. For work with Azure, [Visual Studio Code][4] is recommended.
28+
29+
You can run the app you create in this article on the platform of your choice including: Linux, macOS, Windows, or Windows Subsystem for Linux.
30+
31+
## Create your web app
32+
33+
1. Open a terminal window.
34+
35+
::: zone pivot="vanilla-js"
36+
37+
2. Select an appropriate directory for your code, then run the following commands.
38+
39+
```bash
40+
npm create vite@latest swa-vanilla-demo -- --template=vanilla
41+
cd swa-vanilla-demo
42+
npm install
43+
npm run dev
44+
```
45+
46+
As you run these commands, the development server prints the URL of your website. Select the link to open it in your default browser.
47+
48+
![Screen shot of the generated vanilla web application.][img-vanilla-js]
49+
50+
::: zone-end
51+
52+
::: zone pivot="angular"
53+
54+
2. Select an appropriate directory for your code, then run the following commands.
55+
56+
```bash
57+
npx --package @angular/cli@latest ng new swa-angular-demo --ssr=false --defaults
58+
cd swa-angular-demo
59+
npm start
60+
```
61+
62+
As you run these commands, the development server prints the URL of your website. Select the link to open it in your default browser.
63+
64+
![Screen shot of the generated angular web application.][img-angular]
65+
66+
::: zone-end
67+
68+
::: zone pivot="react"
69+
70+
2. Select an appropriate directory for your code, then run the following commands.
71+
72+
```bash
73+
npm create vite@latest swa-react-demo -- --template react
74+
cd swa-react-demo
75+
npm install
76+
npm run dev
77+
```
78+
79+
As you run these commands, the development server prints the URL of your website. Select the link to open it in your default browser.
80+
81+
![Screen shot of the generated react web application.][img-react]
82+
83+
::: zone-end
84+
85+
::: zone pivot="vue"
86+
87+
2. Select an appropriate directory for your code, then run the following commands.
88+
89+
```bash
90+
npm create vite@latest swa-vue-demo -- --template vue
91+
cd swa-vue-demo
92+
npm install
93+
npm run dev
94+
```
95+
96+
As you run these commands, the development server prints the URL of your website. Select the link to open it in your default browser.
97+
98+
![Screen shot of the generated Vue web application.][img-vue]
99+
100+
::: zone-end
101+
102+
3. Select <kbd>Cmd/Ctrl</kbd>+<kbd>C</kbd> to stop the development server.
103+
104+
[!INCLUDE [Create an Azure Static Web App](../../includes/static-web-apps/quickstart-direct-deploy-create.md)]
105+
106+
[!INCLUDE [Build your site for deployment](../../includes/static-web-apps/quickstart-direct-deploy-build.md)]
107+
108+
::: zone pivot="angular"
109+
110+
> [!WARNING]
111+
> Angular v17 and later place the distributable files in a subdirectory of the output path that you can choose. The SWA CLI doesn't know the specific location of the directory. The following steps show you how to set this path correctly.
112+
113+
Locate the generated *index.html* file in your project in the *dist/swa-angular-demo/browser* folder.
114+
115+
1. Set the `SWA_CLI_OUTPUT_LOCATION` environment variable to the directory containing the *index.html* file:
116+
117+
# [bash](#tab/bash)
118+
119+
```bash
120+
export SWA_CLI_OUTPUT_LOCATION="dist/swa-angular-demo/browser"
121+
```
122+
123+
# [csh](#tab/csh)
124+
125+
```bash
126+
setenv SWA_CLI_OUTPUT_LOCATION "dist/swa-angular-demo/browser"
127+
```
128+
129+
# [PowerShell](#tab/pwsh)
130+
131+
```powershell
132+
$env:SWA_CLI_OUTPUT_LOCATION="dist/swa-angular-demo/browser"
133+
```
134+
135+
# [CMD](#tab/cmd)
136+
137+
```bash
138+
set SWA_CLI_OUTPUT_LOCATION="dist/swa-angular-demo/browser"
139+
```
140+
141+
---
142+
143+
::: zone-end
144+
145+
## Deploy your site to Azure
146+
147+
Deploy your code to your static web app:
148+
149+
```bash
150+
npx swa deploy --env production
151+
```
152+
153+
It might take a few minutes to deploy the application. Once complete, the URL of your site is displayed.
154+
155+
![Screen shot of the deploy command.][img-deploy]
156+
157+
On most systems, you can select the URL of the site to open it in your default browser.
158+
159+
[!INCLUDE [Clean up resources](../../includes/static-web-apps/quickstart-direct-deploy-clean-up-resources.md)]
160+
161+
## Next steps
162+
163+
> [!div class="nextstepaction"]
164+
> [Add authentication](./add-authentication.md)
165+
166+
## Related content
167+
168+
* [Authentication and authorization](./authentication-authorization.yml)
169+
* [Database connections](./database-overview.md)
170+
* [Custom Domains](./custom-domain.md)
171+
172+
<!-- Links -->
173+
[1]: https://azure.microsoft.com/free
174+
[2]: https://nodejs.org/
175+
[3]: /cli/azure/install-azure-cli
176+
[4]: https://code.visualstudio.com
177+
178+
<!-- Images -->
179+
[img-deploy]: media/deploy-screenshot.png
180+
[img-vanilla-js]: media/deploy-web-framework/vanilla-js-screenshot.png
181+
[img-angular]: media/deploy-web-framework/angular-screenshot.png
182+
[img-react]: media/deploy-web-framework/react-screenshot.png
183+
[img-vue]: media/deploy-web-framework/vue-screenshot.png
45.8 KB
Loading
26.1 KB
Loading
10.3 KB
Loading
32.6 KB
Loading
25.8 KB
Loading
20.7 KB
Loading
21.9 KB
Loading

0 commit comments

Comments
 (0)