-
Notifications
You must be signed in to change notification settings - Fork 2
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
vouill
committed
Dec 18, 2019
0 parents
commit f5d4574
Showing
9 changed files
with
89 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
package-lock.json | ||
.idea |
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,8 @@ | ||
const authWrapper = require('../utils/authenticate-wrapper'); | ||
|
||
exports.handler = authWrapper(function (event, context, callback) { | ||
callback(null, { | ||
statusCode: 200, | ||
body: "You can edit our amazing products" | ||
}); | ||
}) |
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 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
exports.handler = function(event, context, callback) { | ||
jwt.sign({ foo: 'bar' }, 'mySecret', function(err, token) { | ||
callback(null, { | ||
statusCode: 200, | ||
body: token | ||
}); | ||
}); | ||
}; |
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,6 @@ | ||
exports.handler = function(event, context, callback) { | ||
callback(null, { | ||
statusCode: 200, | ||
body: "You can see our amazing products" | ||
}); | ||
}; |
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,3 @@ | ||
[build] | ||
functions = "functions" | ||
publish = "src/" |
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,14 @@ | ||
{ | ||
"name": "sqreenity-auth-func", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"jsonwebtoken": "^8.5.1" | ||
} | ||
} |
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 @@ | ||
# Netlify function JWT authentication example | ||
|
||
This repo is a code example repo related to this article. | ||
|
||
It shows using function wrappers in order to authenticate specific netlify functions. | ||
|
||
|
||
|
||
Try me live! | ||
[](https://app.netlify.com/start/deploy?repository=https://github.com/sqreen/article-serverless-auth-example) |
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,11 @@ | ||
<div> | ||
<div> | ||
<a href="/.netlify/functions/login">Get JWT token</a> | ||
</div> | ||
<div> | ||
<a href="/.netlify/functions/unauthenticated-route">Go to an unauthenticated route</a> | ||
</div> | ||
<div> | ||
<a href="/.netlify/functions/authenticated-route">Go to an authenticated route</a> | ||
</div> | ||
</div> |
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,24 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
module.exports = fn => (...args) => { | ||
const [context] = args; | ||
// using context.queryStringParameters.token as a token holder is for the sake of the demo. | ||
// use cookies or an authorization header if you want to use it in prod | ||
if(!context.queryStringParameters.token) { | ||
return unauthResp(...args); | ||
} | ||
jwt.verify(context.queryStringParameters.token, 'mySecret', function(err, decoded) { | ||
if(err) { | ||
return unauthResp(...args); | ||
} | ||
return fn(...args) | ||
}); | ||
}; | ||
|
||
|
||
const unauthResp = (event, context, callback) => { | ||
callback(null, { | ||
statusCode: 400, | ||
body: 'Not authenticated', | ||
}); | ||
}; |