-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b240a00
commit 41b7f3b
Showing
4 changed files
with
93 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,89 @@ | ||
# foronce | ||
|
||
- [foronce](#foronce) | ||
- [Usage](#usage) | ||
- [Examples](#examples) | ||
- [One Time Email Validation](#one-time-email-validation) | ||
- [API](#api) | ||
|
||
> TOTP in a few functions | ||
## Usage | ||
|
||
- Install the `foronce` library | ||
|
||
```sh | ||
; npm install foronce | ||
``` | ||
|
||
### Examples | ||
|
||
#### One Time Email Validation | ||
|
||
```ts | ||
import { generateTOTPSecret, totp, isTOTPValid } from 'foronce' | ||
|
||
app.post('/login', (req, res) => { | ||
const email = req.body.email | ||
const secret = generateTOTPSecret() | ||
|
||
const user = new User() | ||
user.email = email | ||
user.OTPSecret = secret | ||
await user.save() | ||
|
||
const otp = totp(secret) | ||
|
||
EmailProvider.sendLoginEmail(email, otp) | ||
}) | ||
|
||
app.post('/verify', (req, res) => { | ||
const { email, otp } = req.body | ||
const user = await User.find({ | ||
where: { | ||
email: email, | ||
}, | ||
}) | ||
|
||
if (!isTOTPValid(user.OTPSecret, otp)) { | ||
res.status(400) | ||
res.send({ message: 'Invalid OTP' }) | ||
return | ||
} | ||
|
||
res.send(generateLoginToken(user)) | ||
return | ||
}) | ||
``` | ||
|
||
## API | ||
|
||
```ts | ||
export function totp( | ||
secret: string, | ||
when?: number, | ||
options?: { | ||
period?: number | ||
algorithm?: 'sha1' | 'sha256' | 'sha512' | ||
} | ||
): string | ||
|
||
export function isTOTPValid( | ||
secret: string, | ||
token: string, | ||
options?: { | ||
period?: number | ||
algorithm?: 'sha1' | 'sha256' | 'sha512' | ||
} | ||
): boolean | ||
|
||
export function generateTOTPURL( | ||
secret: string, | ||
options: { | ||
company: string | ||
email: string | ||
} | ||
): string | ||
|
||
export function generateTOTPSecret(num?: number): string | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"name": "foronce", | ||
"version": "0.0.2", | ||
"description": "TOTP in a few functions", | ||
"repository": "[email protected]:dumbjs/foronce.git", | ||
"license": "MIT", | ||
"author": "Reaper <[email protected]>", | ||
|
@@ -39,10 +40,6 @@ | |
"*.{js,css,md,json}": "prettier --write" | ||
}, | ||
"prettier": "@barelyhuman/prettier-config", | ||
"dependencies": { | ||
"hi-base32": "^0.5.1", | ||
"uncrypto": "^0.1.3" | ||
}, | ||
"devDependencies": { | ||
"@barelyhuman/prettier-config": "^1.0.0", | ||
"@types/node": "^20.10.8", | ||
|
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