Skip to content

Commit 7031eb4

Browse files
committed
implement user authentication with auth0
1 parent cf513ad commit 7031eb4

19 files changed

Lines changed: 1586 additions & 92 deletions

File tree

NOTICE

Lines changed: 1175 additions & 47 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
# Next.js/JavaScript: Starter Web App Code Sample
1+
# Next.js/JavaScript: Basic User Authentication Code Sample
22

3-
This JavaScript code sample demonstrates how to build web applications using Next.js and React. This Next.js code sample uses the App Router.
3+
This JavaScript code sample demonstrates **how to implement user authentication** in Next.js web applications using Auth0.
44

5-
Visit the ["Next.js/JavaScript Code Samples: App Security in Action"](https://developer.auth0.com/resources/code-samples/web-app/nextjs-javascript) section of the ["Auth0 Developer Resources"](https://developer.auth0.com/resources) to explore how you can secure Next.js applications written in JavaScript by implementing user authentication 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.
6+
7+
Visit the ["Next.js/JavaScript Code Sample: User Authentication For Basic Apps"](https://developer.auth0.com/resources/code-samples/web-app/nextjs/basic-authentication) page for instructions on how to configure and run this code sample and how to integrate it with an external API server of your choice.
68

79
## Why Use Auth0?
810

911
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).
10-
11-
## Set Up and Run the Next.js Project
12-
13-
Install the project dependencies:
14-
15-
```bash
16-
npm install
17-
```
18-
19-
Execute this command to run your Next.js application in development:
20-
21-
```bash
22-
npm run dev
23-
```

app/api/auth/[auth0]/route.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { handleAuth, handleLogin } from "@auth0/nextjs-auth0";
2+
3+
export const GET = handleAuth({
4+
login: handleLogin({
5+
authorizationParams: {
6+
prompt: "login",
7+
},
8+
returnTo: "/profile",
9+
}),
10+
signup: handleLogin({
11+
authorizationParams: {
12+
prompt: "login",
13+
screen_hint: "signup",
14+
},
15+
returnTo: "/profile",
16+
}),
17+
});

app/api/messages/admin/route.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { withApiAuthRequired } from "@auth0/nextjs-auth0";
12
import { NextResponse } from "next/server";
23

3-
export async function GET() {
4+
const GET = withApiAuthRequired(async () => {
45
const message = {
56
text: "This is an admin message.",
67
};
78

89
return NextResponse.json(message);
9-
}
10+
});
11+
12+
export { GET };
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { withApiAuthRequired } from "@auth0/nextjs-auth0";
12
import { NextResponse } from "next/server";
23

3-
export async function GET() {
4+
const GET = withApiAuthRequired(async () => {
45
const message = {
56
text: "This is a protected message.",
67
};
78

89
return NextResponse.json(message);
9-
}
10+
});
11+
12+
export { GET };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const LoginButton = () => {
2+
return (
3+
<a className="button__login" href="/api/auth/login">
4+
Log In
5+
</a>
6+
);
7+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const LogoutButton = () => {
2+
return (
3+
<a className="button__logout" href="/api/auth/logout">
4+
Log Out
5+
</a>
6+
);
7+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const SignupButton = () => {
2+
return (
3+
<a className="button__sign-up" href="/api/auth/signup">
4+
Sign Up
5+
</a>
6+
);
7+
};
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
"use client";
2+
3+
import { useUser } from "@auth0/nextjs-auth0/client";
4+
import { LoginButton } from "../../buttons/login-button";
5+
import { LogoutButton } from "../../buttons/logout-button";
6+
import { SignupButton } from "../../buttons/signup-button";
7+
18
export const NavBarButtons = () => {
2-
return <div />;
9+
const { user } = useUser();
10+
11+
return (
12+
<div className="nav-bar__buttons">
13+
{!user && (
14+
<>
15+
<SignupButton />
16+
<LoginButton />
17+
</>
18+
)}
19+
{user && (
20+
<>
21+
<LogoutButton />
22+
</>
23+
)}
24+
</div>
25+
);
326
};
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
"use client";
2+
3+
import { useUser } from "@auth0/nextjs-auth0/client";
14
import { NavBarTab } from "./nav-bar-tab";
25

36
export const NavBarTabs = () => {
7+
const { user } = useUser();
8+
49
return (
510
<div className="nav-bar__tabs">
611
<NavBarTab path="/profile" label="Profile" />
712
<NavBarTab path="/public" label="Public" />
8-
<NavBarTab path="/protected" label="Protected" />
9-
<NavBarTab path="/admin" label="Admin" />
13+
{user && (
14+
<>
15+
<NavBarTab path="/protected" label="Protected" />
16+
<NavBarTab path="/admin" label="Admin" />
17+
</>
18+
)}
1019
</div>
1120
);
1221
};

0 commit comments

Comments
 (0)