forked from paoyong/postgres-oauth-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
148 lines (135 loc) · 4.17 KB
/
model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var DB = require('./db').DB,
knex = DB.knex;
var User = DB.Model.extend({
tableName: 'users',
idAttribute: 'id',
Facebook: function() {
return this.hasOne(Facebook, 'id');
},
Twitter: function() {
return this.hasOne(Twitter, 'id');
},
Google: function() {
return this.hasOne(Google, 'id');
}
});
var Facebook = DB.Model.extend({
tableName: 'facebook',
idAttribute: 'id',
User: function() {
return this.belongsTo(User, 'id');
}
});
var Twitter = DB.Model.extend({
tableName: 'twitter',
idAttribute: 'id',
User: function() {
return this.belongsTo(User, 'id');
}
});
var Google = DB.Model.extend({
tableName: 'google',
idAttribute: 'id',
User: function() {
return this.belongsTo(User, 'id');
}
});
// ------------------------------
// createNewUser
// ------------------------------
// Makes a new user in the database with
// automatic incremented ID. Then, returns
// that user's ID after the user is created.
function createNewUser(callback) {
new User().save().then(function(user) {
callback(user.toJSON().id);
});
}
// ------------------------------
// grabUserCredentials
// ------------------------------
// Returns a JSON list of a single user like this:
// {
// local: {
// username: 'sampleun'
// password: 'samplepw'
// },
// facebook: {
// ...
// },
// twitter: {
// ...
// },
// google: {
// ...
// },
// }
function grabUserCredentials(userId, callback) {
// Skeleton JSON
var loginUser = {
local: {
username: null,
password: null,
},
facebook: {
id: userId,
token: null,
email: null,
name: null,
},
twitter: {
id: userId,
token: null,
twitterId: null,
displayName: null,
username: null,
},
google: {
id: userId,
token: null,
googleId: null,
email: null,
name: null,
}
};
// SQL joins to get all credentials/tokens of a single user
// to fill in loginUser JSON.
knex.select('users.id', 'users.username', 'users.password',
'facebook.token as fb_token', 'facebook.email as fb_email', 'facebook.name as fb_name',
'twitter.token as tw_token', 'twitter.display_name as tw_disp_name', 'twitter.username as tw_username', 'twitter.twitter_id as tw_id',
'google.token as g_token', 'google.google_id as g_id', 'google.email as g_email', 'google.name as g_name')
.from('users')
.leftOuterJoin('facebook', 'facebook.id', '=', 'users.id')
.leftOuterJoin('twitter', 'twitter.id', '=', 'users.id')
.leftOuterJoin('google', 'google.id', '=', 'users.id')
.where('users.id', '=', userId).then(function(row) {
row = row[0];
if (!row) {
callback('Could not find user with that ID', null);
} else {
// Fill in loginUser JSON
loginUser.local.username = row.username;
loginUser.local.password = row.password;
loginUser.facebook.token = row.fb_token;
loginUser.facebook.email = row.fb_email;
loginUser.facebook.name = row.fb_name;
loginUser.twitter.token = row.tw_token;
loginUser.twitter.twitterId = row.tw_id;
loginUser.twitter.displayName = row.tw_disp_name;
loginUser.twitter.username = row.tw_username;
loginUser.google.token = row.g_token;
loginUser.google.googleId = row.g_id;
loginUser.google.email = row.g_email;
loginUser.google.name = row.g_name;
callback(null, loginUser);
}
});
};
module.exports = {
createNewUser : createNewUser,
grabUserCredentials : grabUserCredentials,
User : User,
Facebook : Facebook,
Twitter : Twitter,
Google : Google
};