Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Contest Signup Webtask
'use latest';
import express from 'express';
import { fromExpress } from 'webtask-tools';
import bodyParser from 'body-parser';
const app = express();
var jwt = require('express-jwt');
var jwtCheck = jwt({
secret: new Buffer('AUTH0_CLIENT_SECRET', 'base64'),
audience: 'AUTH0_CLIENT_ID'
});
app.use(bodyParser.json());
app.get('/', (req, res) => {
const HTML = renderView({
title: 'Sorteo Nodeconfar'
});
res.set('Content-Type', 'text/html');
res.status(200).send(HTML);
});
app.post('/tshirt', jwtCheck, function(req, res) {
req.webtaskContext.storage.get(function(err, data) {
if(err) return res.send(400);
data = data || [];
data.push({ 'twitter': '@' + req.user.screen_name, 'name': req.user.name, 'email': req.user.email, time: Date.now() });
req.webtaskContext.storage.set(data, function(err){
if(err) return res.send(400);
res.send(200);
});
})
});
app.get('/_sorteo', function(req, res) {
req.webtaskContext.storage.get(function(err, data) {
var result = '';
for (var i=0; i<data.length; i++) {
result += (i+1) + '. ' + data[i].name + ' (' + (data[i].email || data[i].twitter) + ')<br>';
}
res.send(result);
});
});
app.get('/_reset', function(req, res) {
req.webtaskContext.storage.set([], {force: 1}, function() { res.send(200)});
});
module.exports = fromExpress(app);
function renderView(locals) {
return `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>${locals.title}</title>
<script src="//cdn.auth0.com/js/lock/10.5/lock.min.js"></script>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<link href="https://cdn.auth0.com/styleguide/4.8.24/index.min.css" rel="stylesheet" />
<style>
.wrapper {
text-align: center;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="btn btn-success btn-lg" id="btn-login">WANT MY TSHIRT </div>
<p id="mainframe" style="display:none">El mainframe esta procesando tu request... <br>Sorteo en el break de las 4:15pm </p>
<img src="http://i.giphy.com/QKKV7KFrG9XMY.gif" id="fail" style="display:none">
<img src="http://i.giphy.com/kVaj8JXJcDsqs.gif" id="goodluck" style="display:none">
</div>
<script>
window.addEventListener('load', function() {
var lock = new Auth0Lock('AUTH0_CLIENTID', 'AUTH0_DOMAIN', { languageDictionary: { title: "nodeconfar" }, rememberLastLogin: false });
var btn_login = document.getElementById('btn-login');
btn_login.addEventListener('click', function() {
lock.show({ auth: {
params: {scope: 'openid email nickname name screen_name'},
}});
});
lock.on("authenticated", function(authResult) {
$('#btn-login').hide();
$('#mainframe').show();
lock.getProfile(authResult.idToken, function(error, profile) {
if (error) {
// Handle error
return;
}
localStorage.setItem('token', authResult.idToken);
// Display user information
$.ajax({
type : 'POST',
url : '/nodeconfar/tshirt?webtask_no_cache=1',
headers : {
Authorization : 'Bearer ' + localStorage.getItem('token')
}
}).done(function(data) {
$('#goodluck').show();
}).fail(function(data) {
$('#fail').show();
});
});
});
});
</script>
</body>
</html>
`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment