Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vouill committed Dec 18, 2019
0 parents commit f5d4574
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
package-lock.json
.idea
8 changes: 8 additions & 0 deletions functions/authenticated-route.js
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"
});
})
10 changes: 10 additions & 0 deletions functions/login.js
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
});
});
};
6 changes: 6 additions & 0 deletions functions/unauthenticated-route.js
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"
});
};
3 changes: 3 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]
functions = "functions"
publish = "src/"
14 changes: 14 additions & 0 deletions package.json
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"
}
}
10 changes: 10 additions & 0 deletions readme.md
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!
[![Deploy to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/sqreen/article-serverless-auth-example)
11 changes: 11 additions & 0 deletions src/index.html
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>
24 changes: 24 additions & 0 deletions utils/authenticate-wrapper.js
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',
});
};

0 comments on commit f5d4574

Please sign in to comment.