Skip to content

Commit

Permalink
menome login support
Browse files Browse the repository at this point in the history
  • Loading branch information
whs committed Dec 25, 2011
1 parent 89006a0 commit b9f907b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
11 changes: 11 additions & 0 deletions config-oauth-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ var config = {

callbackAuthen: 'http://localhost:8080/oauth/facebook/authen'
},

menome: {
id: '',
secret: '',
base: 'https://menomeapi.sunburn.in.th/1/',
authorize: '/auth/authorize',
access_token: '/auth/token?grant_type=authorization_code',
version: '2.0',

callback: 'http://localhost:8080/oauth/menome/callback'
},

key: ''

Expand Down
93 changes: 92 additions & 1 deletion routers/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ var _getService = function (name) {
} else if (config.version == '2.0') {
service = new oauth2(config.id,
config.secret,
config.base);
config.base,
config.authorize,
config.access_token);
}

_services[name] = service;
Expand Down Expand Up @@ -314,6 +316,95 @@ var services = {
response.end('Hello, world');
},

'menome': function (request, response, store) {

var loginQuery = url.parse(request.url, true).query;
if (loginQuery.invite == _loadConfig().key) {

var config = _loadConfig()['menome'];
var service = _getService('menome');
response.writeHead(302, {
//'Location': config.base + "&client_id=" + config.id
'Location': service.getAuthorizeUrl({ response_type: "code" })
});
response.end();

} else {
response.writeHead(302, {
'Location': '/'
});
response.end();
}

},

'menome/callback': function (request, response, store) {
var code = url.parse(request.url, true).query.code;

var service = _getService('menome');

step(
function init() {
service.getOAuthAccessToken(code, null, this);
},
function gotAccessToken(error, access_token, refresh_token) {
if (!error) {
service.get('https://menomeapi.sunburn.in.th/1/user/user.json', access_token, this);
} else {
response.writeHead(302, {
'Location': '/'
});
response.end();
}
},
function gotUser(error, result) {

if (!error) {
var user = JSON.parse(result);
_log.trace(user);

var users = model.get('user', store.getClient());
users.find({username: user.id},
function (items) {

if (items.length == 0) {
_log.debug ('Create user: ' + user.id);
// Create user and return to index
users.create({
username: user.id,
image: user.avatar,
updated: 0,
anonymous: false },
function (error, user) {
_log.trace (user);

response.writeHead(302, {
'Location': '/index.html#user/login/' + user._id });
response.end('');
});
} else {
// Get first user and return to index
_log.debug (items[0]);

response.writeHead(302, {
'Location': '/index.html#user/login/' + items[0]._id });
response.end('');
}

});
// End verify credentials
} else {
response.writeHead(302, {
'Location': '/'
});
response.end();
}


});

},

'authenticate': function (request, response, store) {

var method = null;
Expand Down
5 changes: 5 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ <h3><a class="brand" href="#">Taskboard</a></h3>
<span>Log in with Twitter</span>
</a>
</li>
<li id="log-in-menu">
<a href="#user/private/show/menome">
<span>Log in with menome</span>
</a>
</li>
</ul>
</li>
</ul>
Expand Down

0 comments on commit b9f907b

Please sign in to comment.