Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Attach new identities to existing, if any #45
Browse files Browse the repository at this point in the history
Fix #45
  • Loading branch information
indrif committed Jun 24, 2018
1 parent d591858 commit 1185567
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function unAuthorized(res) {
async function getUserIdentity(userIdentifier, req, access_token) {
let userId;
try {
userId = await users.getByIdentity("Dropbox", userIdentifier);
userId = await users.getByIdentity("Dropbox", userIdentifier, req.gardenSession.userIdentity);
req.gardenSession.userIdentity = userId;
await dropboxDb.storeDropboxToken(
req.gardenSession.userIdentity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async function getUserIdentity(verified, userIdentifier, req) {
}
let userId;
try {
userId = await users.getByIdentity("Google", userIdentifier);
userId = await users.getByIdentity("Google", userIdentifier, req.gardenSession.userIdentity);
} catch (err) {
logger.error(`Failed to find user identity: ${err}`);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports.authFinish = async (req, res) => {
// Setup user
console.log("Yay! Access token is ", result);
try {
userId = await providerUser.getByIdentity("Instagram", result.user.id);
userId = await providerUser.getByIdentity("Instagram", result.user.id, req.gardenSession.userIdentity);
req.gardenSession.userIdentity = userId;
} catch (err) {
logger.error(`Failed to find user identity: ${err}`);
Expand Down
47 changes: 14 additions & 33 deletions libs/provider-user/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,22 @@ const logger = require("logging");

const dbClient = require("db").create("garden");

async function attachIdentity(provider, providerIdentity) {
async function attachIdentity(provider, providerIdentity, existingUserId) {
// FIXME: Ask for username?
logger.info(`Attaching identity ${providerIdentity} to ${provider}`);
logger.info(`Attaching identity ${providerIdentity} to ${provider} (existing user id: ${existingUserId})`);
const db = await dbClient.connect();
let userId
try {
db.query("BEGIN");
const userResponse = await dbClient.query(
"INSERT INTO users(username) VALUES($1) RETURNING id",
[providerIdentity]
);
const userId = userResponse.rows[0].id;
await dbClient.query(
"INSERT INTO user_identities(user_id, provider, provider_id) VALUES($1, $2, $3)",
[userId, provider, providerIdentity]
);
await db.query("COMMIT");
return userId;
} catch (err) {
await db.query("ROLLBACK");
throw err;
} finally {
db.release();
}
}
async function attachProvider(provider, providerIdentity) {
// FIXME: Ask for username?
logger.info(`Attaching identity ${providerIdentity} to ${provider}`);
const db = await dbClient.connect();
try {
db.query("BEGIN");
const userResponse = await dbClient.query(
"INSERT INTO users(username) VALUES($1) RETURNING id",
[providerIdentity]
);
const userId = userResponse.rows[0].id;
if (existingUserId) {
userId = existingUserId;
} else {
const userResponse = await dbClient.query(
"INSERT INTO users(username) VALUES($1) RETURNING id",
[providerIdentity]
);
userId = userResponse.rows[0].id;
}
await dbClient.query(
"INSERT INTO user_identities(user_id, provider, provider_id) VALUES($1, $2, $3)",
[userId, provider, providerIdentity]
Expand All @@ -52,7 +33,7 @@ async function attachProvider(provider, providerIdentity) {
}
}

async function getByIdentity(provider, providerIdentity) {
async function getByIdentity(provider, providerIdentity, existingUserId) {
logger.info(`Get identity for ${providerIdentity} for provider ${provider}`);
const response = await dbClient.query(
"SELECT user_id FROM user_identities WHERE provider=$1 AND provider_id=$2",
Expand All @@ -62,7 +43,7 @@ async function getByIdentity(provider, providerIdentity) {
if (response.rows[0] !== undefined) {
return response.rows[0].user_id;
} else {
return attachIdentity(provider, providerIdentity);
return attachIdentity(provider, providerIdentity, existingUserId);
}
}

Expand Down

0 comments on commit 1185567

Please sign in to comment.