Skip to content

Commit

Permalink
Merge pull request #101 from Amialc/master
Browse files Browse the repository at this point in the history
fixed semicolons, reworked custom-signup example
  • Loading branch information
ntotten committed May 19, 2016
2 parents ade9075 + 73e16ae commit 0f025d3
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 59 deletions.
2 changes: 2 additions & 0 deletions examples/custom-signup/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AUTH0_TOKEN=
AUTH0_DOMAIN=
13 changes: 9 additions & 4 deletions examples/custom-signup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
75 changes: 39 additions & 36 deletions examples/custom-signup/custom-signup.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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);
});
}

});
24 changes: 24 additions & 0 deletions examples/custom-signup/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
28 changes: 13 additions & 15 deletions examples/custom-signup/server.js
Original file line number Diff line number Diff line change
@@ -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'});
});
});

Expand Down
2 changes: 1 addition & 1 deletion examples/hapi-regular-webapp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="auth0.com/docs/logout">https://auth0.com/docs/logout</a>.');
reply('You are now logged out from this web application. If you also want to log out from Auth0, take a look at <a href="https://auth0.com/docs/logout">https://auth0.com/docs/logout</a>.');
}
});

Expand Down
2 changes: 1 addition & 1 deletion examples/nodejs-regular-webapp/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion examples/nodejs-regular-webapp/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion examples/nodejs-regular-webapp/routes/user.js
Original file line number Diff line number Diff line change
@@ -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. */
Expand Down

0 comments on commit 0f025d3

Please sign in to comment.