Skip to content

Commit 49cad81

Browse files
committed
implement user authentication with auth0
1 parent d8a91d4 commit 49cad81

17 files changed

Lines changed: 32124 additions & 577 deletions

NOTICE

Lines changed: 31773 additions & 389 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,13 @@
1-
# Vue/JavaScript: Starter SPA Code Sample
1+
# Vue/JavaScript: Basic User Authentication Code Sample
22

3-
This JavaScript code sample demonstrates how to build a Single-Page Application (SPA) using Vue. This Vue code sample builds the API server using the Vue Composition API.
3+
This JavaScript code sample demonstrates **how to implement user authentication** in Vue applications using Auth0. This Vue code sample builds the Single-Page Application (SPA) using the Vue Composition API.
44

5-
Visit the ["Vue/JavaScript Code Samples: SPA Security in Action"](https://developer.auth0.com/resources/code-samples/spa/vue) section of the ["Auth0 Developer Resources"](https://developer.auth0.com/resources) to explore how you can secure Vue applications written in JavaScript by implementing endpoint protection and authorization with Auth0.
5+
This code sample is part of the ["Auth0 Developer Resources"](https://developer.auth0.com/resources), a place where you can explore the authentication and authorization features of the Auth0 Identity Platform.
66

7-
[![Vue/JavaScript Code Samples: SPA Security in Action](https://cdn.auth0.com/blog/hub/code-samples/spa/vue-javascript.png)](https://developer.auth0.com/resources/code-samples/spa/vue)
7+
Visit the ["Vue/JavaScript + Composition API Code Sample: User Authentication For Basic Apps"](https://developer.auth0.com/resources/code-samples/spa/vue/basic-authentication) page for instructions on how to configure and run this code sample and how to integrate it with an API server of your choice to [create a full-stack code sample](https://developer.auth0.com/resources/code-samples/full-stack/hello-world/basic-access-control/spa).
8+
9+
[![Vue/JavaScript + Composition API Code Sample: User Authentication For Basic Apps](https://cdn.auth0.com/blog/hub/code-samples/spa/vue-javascript/basic-authentication-with-composition-api.png)](https://developer.auth0.com/resources/code-samples/spa/vue/basic-authentication)
810

911
## Why Use Auth0?
1012

1113
Auth0 is a flexible drop-in solution to add authentication and authorization services to your applications. Your team and organization can avoid the cost, time, and risk that come with building your own solution to authenticate and authorize users. We offer tons of guidance and SDKs for you to get started and [integrate Auth0 into your stack easily](https://developer.auth0.com/resources/code-samples/full-stack).
12-
13-
## Set Up and Run the Vue Project
14-
15-
Install the project dependencies:
16-
17-
```bash
18-
npm install
19-
```
20-
21-
The starter Vue project offers a functional application that consumes data from an external API to hydrate the user interface. For simplicity and convenience, the starter project simulates the external API locally using [`json-server`](https://github.com/typicode/json-server).
22-
23-
However, you can also integrate this starter project with any of the ["Hello World" API code samples, which are available in multiple backend frameworks and programming languages](https://github.com/orgs/auth0-developer-hub/repositories?language=&q=api+hello-world&sort=&type=public).
24-
25-
The compatible API server runs on `http://localhost:6060` by default. As such, to connect your Vue application with that API server, create a `.env` file under the root project directory and populate it with the following environment variables:
26-
27-
```bash
28-
VITE_API_SERVER_URL=http://localhost:6060
29-
```
30-
31-
Next, execute the following command to run the JSON server API:
32-
33-
```bash
34-
npm run api
35-
```
36-
37-
Finally, open another terminal tab and execute this command to run your Vue application:
38-
39-
```bash
40-
npm run dev
41-
```
42-
43-
Visit [`http://localhost:4040/`](http://localhost:4040/) to access the starter application.
44-
45-
In the starter project, all the starter Vue application routes are public. However, you can use Auth0 to get an ID token to hydrate the user profile information present on the `/profile` page with information from a real user. With Auth0, you can also get an access token to make a secure call to an external API to hydrate the messages present in the `/protected` and `/admin` pages.

package-lock.json

Lines changed: 166 additions & 128 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"api": "json-server --host 0.0.0.0 --port 6060 --watch json-server/db.json --routes json-server/routes.json"
1010
},
1111
"dependencies": {
12+
"@auth0/auth0-vue": "^2.0.0",
1213
"axios": "^0.27.2",
1314
"vue": "^3.2.36",
1415
"vue-router": "^4.0.15"

src/app.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
<template>
2-
<router-view />
2+
<div v-if="isLoading" class="page-layout">
3+
<PageLoader />
4+
</div>
5+
<router-view v-else />
36
</template>
7+
8+
<script setup>
9+
import PageLoader from "@/components/page-loader.vue";
10+
import { useAuth0 } from "@auth0/auth0-vue";
11+
12+
const { isLoading } = useAuth0();
13+
</script>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<button class="button__login" @click="handleLogin">Log In</button>
3+
</template>
4+
5+
<script setup>
6+
import { useAuth0 } from "@auth0/auth0-vue";
7+
8+
const { loginWithRedirect } = useAuth0();
9+
10+
const handleLogin = () => {
11+
loginWithRedirect({
12+
appState: {
13+
target: "/profile",
14+
},
15+
authorizationParams: {
16+
prompt: "login",
17+
}
18+
});
19+
};
20+
</script>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<template>
2+
<button class="button__logout" @click="handleLogout">Log Out</button>
3+
</template>
4+
5+
<script setup>
6+
import { useAuth0 } from "@auth0/auth0-vue";
7+
8+
const { logout } = useAuth0();
9+
10+
const handleLogout = () =>
11+
logout({
12+
logoutParams: {
13+
returnTo: window.location.origin,
14+
}
15+
});
16+
</script>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<template>
2+
<button class="button__sign-up" @click="handleSignUp">Sign Up</button>
3+
</template>
4+
5+
<script setup>
6+
import { useAuth0 } from "@auth0/auth0-vue";
7+
8+
const { loginWithRedirect } = useAuth0();
9+
10+
const handleSignUp = () => {
11+
loginWithRedirect({
12+
appState: {
13+
target: "/profile",
14+
},
15+
authorizationParams: {
16+
prompt: "login",
17+
screen_hint: "signup",
18+
}
19+
});
20+
};
21+
</script>
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
<template>
2-
<div />
2+
<div class="nav-bar__buttons">
3+
<template v-if="!isAuthenticated">
4+
<SignupButton />
5+
<LoginButton />
6+
</template>
7+
<template v-if="isAuthenticated">
8+
<LogoutButton />
9+
</template>
10+
</div>
311
</template>
12+
13+
<script setup>
14+
import LoginButton from "@/components/buttons/login-button.vue";
15+
import LogoutButton from "@/components/buttons/logout-button.vue";
16+
import SignupButton from "@/components/buttons/signup-button.vue";
17+
import { useAuth0 } from "@auth0/auth0-vue";
18+
19+
const { isAuthenticated } = useAuth0();
20+
</script>

src/components/navigation/desktop/nav-bar-tabs.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
<div class="nav-bar__tabs">
33
<NavBarTab path="/profile" label="Profile" />
44
<NavBarTab path="/public" label="Public" />
5-
<NavBarTab path="/protected" label="Protected" />
6-
<NavBarTab path="/admin" label="Admin" />
5+
<template v-if="isAuthenticated">
6+
<NavBarTab path="/protected" label="Protected" />
7+
<NavBarTab path="/admin" label="Admin" />
8+
</template>
79
</div>
810
</template>
911

1012
<script setup>
1113
import NavBarTab from "@/components/navigation/desktop/nav-bar-tab.vue";
14+
import { useAuth0 } from "@auth0/auth0-vue";
15+
16+
const { isAuthenticated } = useAuth0();
1217
</script>

0 commit comments

Comments
 (0)