Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Authenticated Webtask
var app = new (require('express'))();
var ejs = require('ejs');
var wt = require('webtask-tools');
Function.prototype.stringify = function () {
var match = this.toString().match(/[^]*\/\*([^]*)\*\/\s*\}$/);
return match ? match[1] : '';
};
app.get('/', function (req, res) {
res.send(ejs.render(home.stringify(), {}));
});
app.get('/how-to', function (req, res) {
res.send(ejs.render(howto.stringify(), { user: req.user }));
});
app.get('/bigbrother', function (req, res) {
if (!req.user.email_verified || req.user.email.indexOf('@auth0.com') < 0) {
res.status(403);
return res.end();
}
req.webtaskContext.storage.get(function (error, data) {
if (error) {
res.status(500);
return res.send(error.message);
}
res.json(data);
})
});
module.exports = wt.fromExpress(app).auth0({
exclude: '/',
loginSuccess: (ctx, req, res, baseUrl) => {
res.writeHead(302, { Location: baseUrl + '/how-to?access_token=' + ctx.accessToken });
res.end();
if (req.user.email) {
ctx.storage.get(function (error, data) {
data = data || { visitors: {} };
data.visitors[ctx.user.email] = new Date().toString();
ctx.storage.set(data, { force: 1 }, function () {});
});
}
}
});
function home() {/*
<!DOCTYPE html5>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="description" content="Authenticated Auth0 Webtasks">
<meta name="keywords" content="javascript, node.js, serverless, microservice, node, webservice, authenitcation, auth0">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.auth0.com/styleguide/1.0.0/img/badge.svg">
<meta name="twitter:title" content="Authenticated Auth0 Webtasks">
<meta name="twitter:description" content="Authenticated Auth0 Webtasks">
<meta name="twitter:creator" content="@auth0">
<meta name="author" content="Auth0 Webtasks">
<link href="https://cdn.auth0.com/styleguide/latest/index.css" rel="stylesheet" />
<title>Authenticated Auth0 Webtasks</title>
<style>
body { background: #222228; }
</style>
</head>
<body class="theme-dark">
<div class="container">
<div class="row text-center">
<h1>Authenticate the Serverless</h1>
<h1><a href="https://auth0.com" title="Go to Auth0!"><img src="https://cdn.auth0.com/styleguide/1.0.0/img/badge.svg" alt="Auth0 badge" /></a></h1>
<h1>Take Webtask. Add Auth0.</h1>
<h1>&nbsp;</h1>
<p><a class="btn btn-success btn-lg" href="hello-auth/login">Find out how</a></p>
</div>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-37952868-22', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>
*/}
function howto() {/*
<!DOCTYPE html5>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="description" content="Authenticated Auth0 Webtasks">
<meta name="keywords" content="javascript, node.js, serverless, microservice, node, webservice, authenitcation, auth0">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.auth0.com/styleguide/1.0.0/img/badge.svg">
<meta name="twitter:title" content="Authenticated Auth0 Webtasks">
<meta name="twitter:description" content="Authenticated Auth0 Webtasks">
<meta name="twitter:creator" content="@auth0">
<meta name="author" content="Auth0 Webtasks">
<link href="https://cdn.auth0.com/styleguide/latest/index.css" rel="stylesheet" />
<title>Authenticated Auth0 Webtasks</title>
<style>
body { background: #222228; }
</style>
</head>
<body class="theme-dark">
<div class="container">
<div class="row text-center">
<h1>Authenticate the Serverless</h1>
<p>Welcome, <%= user.name || user.email %>!</p>
<h1><a href="https://auth0.com" title="Go to Auth0!"><img src="https://cdn.auth0.com/styleguide/1.0.0/img/badge.svg" alt="Auth0 badge" width="50"/></a></h1>
<h2>1. Take Webtask</h2>
<p>Write your backend code as an <a href="https://webtask.io">Auth0 Webtask</a>. Secure with a call to <code>.auth0()</code>.</p>
<pre class="text-left col-md-6 col-md-offset-3">var app = new (require('express'))();
var wt = require('webtask-tools');
app.get('/', function (req, res) {
res.send('Hello, ' + req.user.name);
});
module.exports = wt.fromExpress(app).auth0();</pre>
</div>
<div class="row text-center">
<h2>2. Add Auth0</h2>
<p>Create an <a href="https://auth0.com">Auth0</a> application. Take note of client ID, client secret, and domain.</p>
<p><a href="https://auth0.com" class="btn btn-success btn-lg">Try Auth0 for Free</a><p>
</div>
<div class="row text-center">
<h2>3. Sprinkle magic</h2>
<p>Create the webtask using your Auth0 credentials.</p>
<pre class="text-left col-md-6 col-md-offset-3">npm install -g wt-cli
wt init
wt create webtask.js -s AUTH0_CLIENT_ID=... \
-s AUTH0_CLIENT_SECRET=... \
-s AUTH0_DOMAIN=...</pre>
</div>
<div class="row text-center">
<h2>4. Tweak &amp; adjust</h2>
<p>Use your Auth0 application to select any identity provider,<br>enable multi-factor authentication,<br>perform custom authorization,<br>... and more!<p>
<p>Read more about <a href="https://webtask.io/docs/auth">Auth0 authenticated webtasks</a>...</p>
<div>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-37952868-22', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>
*/}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment