From 73e16aed32ff72ac6d80f606cbf8073a60655b95 Mon Sep 17 00:00:00 2001 From: Vlad Date: Wed, 27 Apr 2016 01:38:55 +0300 Subject: [PATCH] fixed semicolons, reworked custom-signup example --- examples/custom-signup/.env | 2 + examples/custom-signup/README.md | 13 +++- examples/custom-signup/custom-signup.js | 75 ++++++++++--------- examples/custom-signup/package.json | 24 ++++++ examples/custom-signup/server.js | 28 ++++--- examples/hapi-regular-webapp/index.js | 2 +- examples/nodejs-regular-webapp/app.js | 2 +- .../nodejs-regular-webapp/routes/index.js | 2 +- examples/nodejs-regular-webapp/routes/user.js | 2 +- 9 files changed, 91 insertions(+), 59 deletions(-) create mode 100644 examples/custom-signup/.env create mode 100644 examples/custom-signup/package.json diff --git a/examples/custom-signup/.env b/examples/custom-signup/.env new file mode 100644 index 000000000..18d62c920 --- /dev/null +++ b/examples/custom-signup/.env @@ -0,0 +1,2 @@ +AUTH0_TOKEN= +AUTH0_DOMAIN= diff --git a/examples/custom-signup/README.md b/examples/custom-signup/README.md index e2796fc7b..4fa730427 100644 --- a/examples/custom-signup/README.md +++ b/examples/custom-signup/README.md @@ -4,7 +4,12 @@ All that is needed to run this example is a web server. Here's an easy way to do just that: 1. Install `node` -2. Run `npm install -g serve` -3. Run `serve` to start serving the front-end -4. Run `node server.js` to run the server -5. Browse to [`http://localhost:3000`](http://localhost:3000) +2. Set the ClientId, domain, callbackURL in the `custom-signup.js`. You also need to set the Domain and Token (which can be generated [here](https://auth0.com/docs/api/v2)) for your Auth0 app as environment variables in the `.env` file with the following names respectively: `AUTH0_DOMAIN`, `AUTH0_TOKEN`: +``` +AUTH0_DOMAIN={your_domain}.auth0.com +AUTH0_TOKEN={token} +``` +3. Run `npm install -g serve` +4. Run `serve` to start serving the front-end +5. Run `node server.js` to run the server +6. Browse to [`http://localhost:3000`](http://localhost:3000) diff --git a/examples/custom-signup/custom-signup.js b/examples/custom-signup/custom-signup.js index 024b3ac72..4bee57e33 100644 --- a/examples/custom-signup/custom-signup.js +++ b/examples/custom-signup/custom-signup.js @@ -1,40 +1,41 @@ $(function () { -var CONNECTION = 'Username-Password-Authentication'; + var CONNECTION = 'Username-Password-Authentication'; -$('#create-user').submit(function (event) { - if (event && event.preventDefault) { - event.preventDefault(); - } - - var userData = {}; - var userForm = $('#create-user'); - userData.email = userForm.find('input[name=email]').val(); - userData.password = userForm.find('input[name=password]').val(); - userData.color = userForm.find('input[name=color]').val(); - userData.food = userForm.find('input[name=food]').val(); - - $.ajax({ - url: 'http://localhost:3001/signup', - type: 'POST', - data: JSON.stringify(userData), - contentType: 'application/json', - dataType: 'json', - success: function(status) { - auth0.login({ - username: userData.email, - password: userData.password, - connection: CONNECTION, - scope: 'openid email color food' - }); - }, - error: function(err) { - console.error(err); + $('#create-user').submit(function (event) { + if (event && event.preventDefault) { + event.preventDefault(); } - }); - return false; -}); + var userData = {user_metadata: {}}; + userData.email = $(this).find('input[name=email]').val(); + userData.password = $(this).find('input[name=password]').val(); + userData.user_metadata.color = $(this).find('input[name=color]').val(); + userData.user_metadata.food = $(this).find('input[name=food]').val(); + + console.log(userData); + + $.ajax({ + url: 'http://localhost:3001/signup', + type: 'POST', + data: JSON.stringify(userData), + contentType: 'application/json', + dataType: 'json', + success: function (status) { + auth0.login({ + username: userData.email, + password: userData.password, + connection: CONNECTION, + scope: 'openid email color food' + }); + }, + error: function (err) { + console.error(err); + } + }); + + return false; + }); window.auth0 = new Auth0({ clientID: 'CDxL8sxaPMzl37bcQcVfS0JzdqWXFsmt', @@ -47,10 +48,12 @@ $('#create-user').submit(function (event) { if (hash && hash.error) { console.error(hash.error); } else if (hash) { - var profileList = $('#user-profile'); - profileList.find('.email').text(hash.profile.email); - profileList.find('.color').text(hash.profile.color); - profileList.find('.food').text(hash.profile.food); + auth0.getProfile(hash.id_token, function (err, profile) { + var profileList = $('#user-profile'); + profileList.find('.email').text(profile.email); + profileList.find('.color').text(profile.user_metadata.color); + profileList.find('.food').text(profile.user_metadata.food); + }); } }); diff --git a/examples/custom-signup/package.json b/examples/custom-signup/package.json new file mode 100644 index 000000000..700c209bc --- /dev/null +++ b/examples/custom-signup/package.json @@ -0,0 +1,24 @@ +{ + "name": "custom-signup", + "version": "1.0.0", + "description": "All that is needed to run this example is a web server.\r Here's an easy way to do just that:", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/auth0/node-auth0/blob/master/examples/custom-signup" + }, + "author": "Auth0", + "license": "ISC", + "dependencies": { + "auth0": "^2.1.0", + "body-parser": "^1.15.0", + "dotenv": "^2.0.0", + "express": "^4.13.4", + "morgan": "^1.7.0", + "serve-static": "^1.10.2" + } +} diff --git a/examples/custom-signup/server.js b/examples/custom-signup/server.js index f9e4df14d..dd7233989 100644 --- a/examples/custom-signup/server.js +++ b/examples/custom-signup/server.js @@ -1,43 +1,41 @@ -var Auth0 = require('auth0'); +var Auth0 = require('auth0').ManagementClient; var dotenv = require('dotenv'); var express = require('express'); +var logger = require('morgan'); +var bodyParser = require('body-parser'); var extend = require('xtend'); var app = express(); dotenv.load(); -app.use(express.logger()); +app.use(logger('combined')); app.use('/', express.static(__dirname + '/')); var api = new Auth0({ - domain: process.env.AUTH0_DOMAIN || 'contoso.auth0.com', - clientID: process.env.AUTH0_CLIENT_ID || 'CDxL8sxaPMzl37bcQcVfS0JzdqWXFsmt', - clientSecret: process.env.AUTH0_CLIENT_SECRET || 'SopWFEkN1du8b82UhYJ6zmAOr-eO7CGbNepAPZSpvfW16e37xbhOO2Kdfi1Sc-Re' + domain: process.env.AUTH0_DOMAIN, + token: process.env.AUTH0_TOKEN }); var CONNECTION = 'Username-Password-Authentication'; -app.configure(function () { - app.use(express.bodyParser()); - app.use(function (req, res, next) { - res.header("Access-Control-Allow-Origin", "*"); - res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); - next(); - }); +app.use(bodyParser.json()); +app.use(function (req, res, next) { + res.header("Access-Control-Allow-Origin", "*"); + res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + next(); }); app.post('/signup', function (req, res) { var data = extend(req.body, {connection: CONNECTION, email_verified: false}); - api.createUser(data, function (err) { if (err) { console.log('Error creating user: ' + err); - res.send(500, err); + res.status(500).send(err); return; } - res.send(200, {status: 'ok'}); + res.status(200).send({status: 'ok'}); }); }); diff --git a/examples/hapi-regular-webapp/index.js b/examples/hapi-regular-webapp/index.js index 1cc5b4747..30bb0a2ed 100644 --- a/examples/hapi-regular-webapp/index.js +++ b/examples/hapi-regular-webapp/index.js @@ -75,7 +75,7 @@ server.route({ path: '/logout', handler: function (request, reply) { request.cookieAuth.clear(); - reply('You are now logged out from this web application. If you also want to log out from Auth0, take a look at https://auth0.com/docs/logout.'); + reply('You are now logged out from this web application. If you also want to log out from Auth0, take a look at https://auth0.com/docs/logout.'); } }); diff --git a/examples/nodejs-regular-webapp/app.js b/examples/nodejs-regular-webapp/app.js index e50bc7816..47542fd2f 100644 --- a/examples/nodejs-regular-webapp/app.js +++ b/examples/nodejs-regular-webapp/app.js @@ -5,7 +5,7 @@ var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var session = require('express-session'); -var dotenv = require('dotenv') +var dotenv = require('dotenv'); var passport = require('passport'); var Auth0Strategy = require('passport-auth0'); diff --git a/examples/nodejs-regular-webapp/routes/index.js b/examples/nodejs-regular-webapp/routes/index.js index 1045242a7..bcb743f33 100644 --- a/examples/nodejs-regular-webapp/routes/index.js +++ b/examples/nodejs-regular-webapp/routes/index.js @@ -6,7 +6,7 @@ var env = { AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID, AUTH0_DOMAIN: process.env.AUTH0_DOMAIN, AUTH0_CALLBACK_URL: process.env.AUTH0_CALLBACK_URL || 'http://localhost:3000/callback' -} +}; /* GET home page. */ router.get('/', function(req, res, next) { diff --git a/examples/nodejs-regular-webapp/routes/user.js b/examples/nodejs-regular-webapp/routes/user.js index 2f4831836..2a9ad5a0e 100644 --- a/examples/nodejs-regular-webapp/routes/user.js +++ b/examples/nodejs-regular-webapp/routes/user.js @@ -1,6 +1,6 @@ var express = require('express'); var passport = require('passport'); -var ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn() +var ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn(); var router = express.Router(); /* GET user profile. */