This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(src/server): formatting + add expiresIn for JWT
- Loading branch information
Showing
2 changed files
with
44 additions
and
41 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,26 +1,27 @@ | ||
import {Router} from 'express' | ||
import jwt from 'jsonwebtoken' | ||
import chalk from 'chalk' | ||
// import validator from 'validator' | ||
import {JWT_TOKEN} from 'common/api' | ||
// Import validator from 'validator' | ||
// {isLength, trim, isAlphanumeric, escape} | ||
const router = Router() | ||
|
||
// define the home page route | ||
// Define the home page route | ||
router.post('/', (req, res) => { | ||
// const {username, password} = req.body | ||
// const usernameValidated = validator.isLength(0, 36).is | ||
// const passwordValidated = | ||
const data = {username: 'cool_username_for_testing'} | ||
jwt.sign(data, process.env.JWT_SECRET, (err, token) => { | ||
if (err) { | ||
throw new Error( | ||
`Cant create JWT token based on input data: ${JSON.stringify(data)}`, | ||
err | ||
) | ||
} | ||
console.log(chalk.yellow(`Generated token for user: ${data.username}`)) | ||
res.json({token}) | ||
}) | ||
// Const {username, password} = req.body | ||
// const usernameValidated = validator.isLength(0, 36).is | ||
// const passwordValidated = | ||
const data = {username: 'cool_username_for_testing'} | ||
jwt.sign(data, process.env.JWT_SECRET, {expiresIn: '7d'}, (err, token) => { | ||
if (err) { | ||
throw new Error( | ||
`Cant create JWT token based on input data: ${JSON.stringify(data)}`, | ||
err | ||
) | ||
} | ||
console.log(chalk.yellow(`Generated token for user: ${data.username}`)) | ||
res.json({token}) | ||
}) | ||
}) | ||
|
||
export default router |
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