Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

feat(settings): make appSecret optional #45

Merged
merged 1 commit into from
Aug 3, 2020
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { auth } from 'nexus-plugin-jwt-auth'

// Enables the JWT Auth plugin without permissions
use(auth({
appSecret: "<YOUR SECRET>" // required
appSecret: "<YOUR SECRET>" // optional if using custom verify function
}))
```

Expand Down Expand Up @@ -63,7 +63,7 @@ const protectedPaths = [

// Enables the JWT Auth plugin with permissions
use(auth({
appSecret: "<YOUR SECRET>", // required
appSecret: "<YOUR SECRET>", // optional if using custom verify function
protectedPaths // optional
}))
```
Expand Down
14 changes: 9 additions & 5 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ export const plugin: RuntimePlugin<Settings, 'required'> = settings => project =
return {
token: await settings.verify(req)
}
} else if (settings.useCookie && req.cookies && settings.cookieName && req.cookies[settings.cookieName]) {
const token = req.cookies[settings.cookieName];
} else if (settings.appSecret != undefined) {
let token

if (settings.useCookie && req.cookies && settings.cookieName && req.cookies[settings.cookieName]) {
token = req.cookies[settings.cookieName];
} else if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
token = req.headers.authorization.split(' ')[1]
}

return verifyToken(token, settings.appSecret);
} else if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
const token = req.headers.authorization.split(' ')[1]
verifyToken(token, settings.appSecret)
}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request } from "express";

export type Settings = {
appSecret: string;
appSecret?: string;
protectedPaths?: string[];
useCookie?: boolean;
cookieName?: string;
Expand Down