-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from Baroshem/chore/0.8.0
feat/nuxt3-stable-and-basic-auth
- Loading branch information
Showing
14 changed files
with
734 additions
and
729 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: Basic Auth | ||
description: '' | ||
--- | ||
|
||
This middleware will implement Basic Auth in your application. Only users with correct credentials passed to the browser prompt will be able to see the application. Others will be automatically rejected. | ||
|
||
You can learn more about HTTP Authentication [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#basic_authentication_scheme). | ||
|
||
```ts | ||
export type BasicAuth = { | ||
name: string; | ||
pass: string; | ||
enabled: boolean; | ||
message: string; | ||
} | ||
``` | ||
To write a custom logic for this middleware follow this pattern: | ||
```javascript | ||
// nuxt.config.js | ||
|
||
{ | ||
modules: [ | ||
"nuxt-security", | ||
], | ||
security: { | ||
basicAuth: { | ||
route: '/secret-route', | ||
value: { | ||
name: 'test', | ||
pass: 'test', | ||
enabled: true, | ||
message: 'test' | ||
} | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { defineEventHandler, setHeader, createError, sendError } from 'h3' | ||
import getCredentials from 'basic-auth' | ||
import { useRuntimeConfig } from '#imports' | ||
|
||
type Credentials = { | ||
name: string; | ||
pass: string; | ||
}; | ||
|
||
export type BasicAuth = { | ||
name: string; | ||
pass: string; | ||
enabled: boolean; | ||
message: string; | ||
} | ||
|
||
const securityConfig = useRuntimeConfig().security | ||
|
||
export default defineEventHandler(async (event) => { | ||
const credentials = getCredentials(event.node.req) | ||
const basicAuthConfig: BasicAuth = securityConfig.basicAuth.value | ||
|
||
if (!credentials && !validateCredentials(credentials, basicAuthConfig)) { | ||
setHeader(event, 'WWW-Authenticate', `Basic realm=${basicAuthConfig.message || "Please enter username and password"}`) | ||
sendError(event, createError({ statusCode: 401, statusMessage: 'Access denied' })) | ||
} | ||
}) | ||
|
||
const validateCredentials = (credentials: Credentials, config: BasicAuth): boolean => credentials?.name === config?.name && credentials?.pass === config?.pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
a07faa9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nuxt-security – ./
nuxt-security-git-main-baroshem.vercel.app
nuxt-security.vercel.app
nuxt-security-baroshem.vercel.app