Skip to content

Commit

Permalink
fix #2 add bearer support
Browse files Browse the repository at this point in the history
  • Loading branch information
rrd108 committed Mar 8, 2024
1 parent a6149e4 commit 2dc94f7
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/module.ts
Expand Up @@ -7,7 +7,8 @@ import {

// Module options TypeScript interface definition
export interface ModuleOptions {
tokenHeader: string;
tokenHeader?: string;
prefix?: string;
noAuthRoutes: string[];
}

Expand Down
5 changes: 4 additions & 1 deletion src/runtime/server/middleware/tokenAuth.ts
Expand Up @@ -25,10 +25,13 @@ export default defineEventHandler(async (event) => {
});
}

const strippedToken = token
.toLowerCase()
.replace(`${options.prefix?.toLowerCase()} `, "");
let user;
try {
const prisma = new PrismaClient();
user = await prisma.users.findFirst({ where: { token } });
user = await prisma.users.findFirst({ where: { token: strippedToken } });
} catch (error) {
console.error({ error });
}
Expand Down
31 changes: 31 additions & 0 deletions test/bearer.test.ts
@@ -0,0 +1,31 @@
import { describe, it, expect } from "vitest";
import { fileURLToPath } from "node:url";
import { setup, $fetch } from "@nuxt/test-utils/e2e";

describe("middleware", async () => {
await setup({
rootDir: fileURLToPath(new URL("./fixtures/bearer", import.meta.url)),
});

it("deny access with an invalid token", async () => {
try {
const response = await $fetch("/api/users", {
method: "GET",
headers: { authorization: "invalidTestToken" },
});
expect(true).toBe(false);
} catch (err) {
const typedErr = err as { statusCode: number; statusMessage: string };
expect(typedErr.statusCode).toBe(401);
expect(typedErr.statusMessage).toBe("Authentication error");
}
});

it("allow access with valid bearer token", async () => {
const response = await $fetch("/api/users", {
method: "GET",
headers: { authorization: "Bearer 270fsdg04%rt2f6$)b4eblok0dfgauranga" },
});
expect(response.results[0].name).toBe("Gauranga");
});
});
6 changes: 6 additions & 0 deletions test/fixtures/bearer/app.vue
@@ -0,0 +1,6 @@
<template>
<div>basic</div>
</template>

<script setup>
</script>
10 changes: 10 additions & 0 deletions test/fixtures/bearer/nuxt.config.ts
@@ -0,0 +1,10 @@
import NuxtTokenAuthentication from "../../../src/module";

export default defineNuxtConfig({
modules: [NuxtTokenAuthentication],
nuxtTokenAuthentication: {
noAuthRoutes: ["GET:/api/route_noauth"],
tokenHeader: "Authorization",
prefix: "Bearer",
},
});
5 changes: 5 additions & 0 deletions test/fixtures/bearer/package.json
@@ -0,0 +1,5 @@
{
"private": true,
"name": "basic",
"type": "module"
}
3 changes: 3 additions & 0 deletions test/fixtures/bearer/server/api/route_noauth.ts
@@ -0,0 +1,3 @@
export default defineEventHandler(async (event) => {
return { result: "Gauranga" };
});
3 changes: 3 additions & 0 deletions test/fixtures/bearer/server/api/users.get.ts
@@ -0,0 +1,3 @@
export default defineEventHandler(async (event) => {
return { results: [{ id: 1, name: "Gauranga" }] };
});

0 comments on commit 2dc94f7

Please sign in to comment.