Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update readme with steps, add images, fix http code #6

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ After following the quickstart you'll have learned how to:

- Install `@clerk/fastify`
- Set your Clerk API keys
- Add Clerk's middleware
- Add `<ClerkProvider />` and Clerk components
- Configure `clerkPlugin` for all routes
- Use `getAuth()` to access the current auth state
- Use `getAuth()` to protect specific routes
- Configure `clerkPlugin` for specific routes

## Deploy

Expand Down
Binary file added public/dark-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/hero.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/light-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 16 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ const fastify = Fastify({ logger: true });
fastify.register(clerkPlugin);

// Declare a route and access the auth state for this request
fastify.get("/", async (req, reply) => {
fastify.get("/", async (request, reply) => {
const { userId } = getAuth(request);
const user = userId ? await clerkClient.users.getUser(userId) : null;

return reply.send({
message: "Authentication state retrieved successfully.",
user,
});
});

// Protect a route and return 403 if user is unauthenticated
fastify.get("/protected", async (request, reply) => {
try {
// Get userID from JWT passed in Authorization header
const { userId } = getAuth(req);
const { userId } = getAuth(request);

if (!userId) {
return reply.code(401).send({ error: "Unauthorized request." });
return reply.code(403).send({ error: "Unauthorized request." });
victoriaxyz marked this conversation as resolved.
Show resolved Hide resolved
}

const user = await clerkClient.users.getUser(userId);
Expand All @@ -34,9 +44,8 @@ fastify.get("/", async (req, reply) => {
const start = async () => {
try {
await fastify.listen({ port: 8080 });
fastify.log.info("Server is running on port 8080.");
} catch (err) {
fastify.log.error(err);
} catch (error) {
fastify.log.error(error);
process.exit(1);
}
};
Expand Down