-
Notifications
You must be signed in to change notification settings - Fork 23
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
Showing
9 changed files
with
226 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env node | ||
|
||
function isPalindrome(str) { | ||
const cleanedStr = String(str) | ||
.replace(/[^a-zA-Z0-9]/g, '') | ||
.toLowerCase(); | ||
return cleanedStr === cleanedStr.split('').reverse().join(''); | ||
} | ||
|
||
module.exports = { isPalindrome }; |
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,5 @@ | ||
{ | ||
"language_id": "node", | ||
"path": ".", | ||
"scripts": ["index.js"] | ||
} |
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,5 @@ | ||
{ | ||
"language_id": "node", | ||
"path": ".", | ||
"scripts": ["middleware.js"] | ||
} |
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,45 @@ | ||
#!/usr/bin/env node | ||
|
||
const jwt = require('jsonwebtoken'); | ||
const secret = 'secret'; | ||
|
||
// Mock user validation (DB) | ||
function validUser(user, password) { | ||
return user === 'viferga' && password === '123'; | ||
} | ||
|
||
// Sign in function that returns a token if login is valid | ||
function signin(user, password) { | ||
if (validUser(user, password)) return jwt.sign({ user, password }, secret); | ||
|
||
return false; | ||
} | ||
|
||
// Reverse string function | ||
function reverse({ str }) { | ||
return str.split('').reverse().join(''); | ||
} | ||
|
||
// Sum function | ||
function sum({ a, b }) { | ||
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands | ||
return a + b; | ||
} | ||
|
||
// Middleware for verifying the token | ||
function middleware(f) { | ||
return function (token, args) { | ||
try { | ||
return jwt.verify(token, secret) ? f(args) : 'Invalid token'; | ||
} catch (e) { | ||
return e.message; | ||
} | ||
}; | ||
} | ||
|
||
// Export sign in without middleware and reverse and sum with it | ||
module.exports = { | ||
signin, | ||
reverse: middleware(reverse), | ||
sum: middleware(sum) | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "auth-middleware", | ||
"version": "0.1.0", | ||
"dependencies": { | ||
"jsonwebtoken": "^9.0.0" | ||
} | ||
} |
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