Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update model.js #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions examples/postgresql/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,75 @@
* Module dependencies.
*/

var pg = require('pg-promise')(process.env.DATABASE_URL);
var pgp = require('pg-promise')({
// initialization options
});

var db = pgp(process.env.DATABASE_URL);

/*
* Get access token.
*/

module.exports.getAccessToken = function *(bearerToken) {
var result = yield pg.query('SELECT access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id FROM oauth_tokens WHERE access_token = $1', [bearerToken]);
var token = result.rows[0];

return {
accessToken: token.access_token,
clientId: token.client_id,
expires: token.expires,
userId: token.userId
};
var token = yield db.one('SELECT access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id FROM oauth_tokens WHERE access_token = $1', [bearerToken]);
return {
accessToken: token.access_token,
clientId: token.client_id,
expires: token.expires,
userId: token.userId
};
};

/**
* Get client.
*/

module.exports.getClient = function *(clientId, clientSecret) {
var result = yield pg.query('SELECT client_id, client_secret, redirect_uri FROM oauth_clients WHERE client_id = $1 AND client_secret = $2', [clientId, clientSecret]);
var oAuthClient = result.rows[0];
var oAuthClient = yield db.oneOrNone('SELECT client_id, client_secret, redirect_uri FROM oauth_clients WHERE client_id = $1 AND client_secret = $2', [clientId, clientSecret]);

if (!oAuthClient) {
return;
}
if (!oAuthClient) {
return;
}

return {
clientId: oAuthClient.client_id,
clientSecret: oAuthClient.client_secret
};
return {
clientId: oAuthClient.client_id,
clientSecret: oAuthClient.client_secret
};
};

/**
* Get refresh token.
*/

module.exports.getRefreshToken = function *(bearerToken) {
var result = yield pg.query('SELECT access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id FROM oauth_tokens WHERE refresh_token = $1', [bearerToken]);

return result.rowCount ? result.rows[0] : false;
var tokens = yield db.any('SELECT access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id FROM oauth_tokens WHERE refresh_token = $1', [bearerToken]);
return tokens.length?tokens[0]:false;
};

/*
* Get user.
*/

module.exports.getUser = function *(username, password) {
var result = yield pg.query('SELECT id FROM users WHERE username = $1 AND password = $2', [username, password]);

return result.rowCount ? result.rows[0] : false;
var users = yield db.any('SELECT id FROM users WHERE username = $1 AND password = $2', [username, password]);
return users.length?users[0]:false;
};

/**
* Save token.
*/

module.exports.saveAccessToken = function *(token, client, user) {
var result = yield pg.query('INSERT INTO oauth_tokens(access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id) VALUES ($1, $2, $3, $4)', [
token.accessToken,
token.accessTokenExpiresOn,
client.id,
token.refreshToken,
token.refreshTokenExpiresOn,
user.id
]);

return result.rowCount ? result.rows[0] : false;
yield pg.none('INSERT INTO oauth_tokens(access_token, access_token_expires_on, client_id, refresh_token, refresh_token_expires_on, user_id) VALUES ($1, $2, $3, $4)', [
token.accessToken,
token.accessTokenExpiresOn,
client.id,
token.refreshToken,
token.refreshTokenExpiresOn,
user.id
]);

// none gets you no results, as you do not return anything.
//return result.rowCount ? result.rows[0] : false;
};