|
49 | 49 |
|
50 | 50 | $profiles = mysqli_fetch_assoc($profile_result);
|
51 | 51 |
|
52 |
| - if ($profiles && $user['email'] != $profiles[0]['email']) { |
| 52 | + if ($profiles && $user['email'] != $profiles['email']) { |
53 | 53 | header('HTTP/1.0 401 Unauthorized');
|
54 | 54 |
|
55 | 55 | $events['auth0']['method'] = 'secure';
|
|
110 | 110 |
|
111 | 111 | if (getenv('REQUEST_METHOD') == 'POST') {
|
112 | 112 | $json = file_get_contents("php://input");
|
113 |
| - $profile = json_decode($json, true); |
114 |
| - |
115 |
| - $profile['nickname'] = isset($profile['nickname']) ? "{$profile['nickname']}" : isset( |
116 |
| - $user['nickname']) ? "'{$user['nickname']}'" : "null"; |
117 |
| - $profile['given_name'] = isset($profile['given_name']) ? "'{$profile['given_name']}'" : isset( |
118 |
| - $user['given_name']) ? "'{$user['given_name']}'" : "null"; |
119 |
| - $profile['family_name'] = isset($profile['family_name']) ? "'{$profile['family_name']}'" : isset( |
120 |
| - $user['family_name']) ? "'{$user['family_name']}'" : "null"; |
| 113 | + $post_profile = json_decode($json, true); |
| 114 | + $profile; |
| 115 | + |
| 116 | + /** |
| 117 | + * TODO: Remove profile variable and replace it other variables. |
| 118 | + * E.g. $profile['nickname'] to $sql_nickname, |
| 119 | + * $profile['facebook_id'] to $sql_fecebook_id, etc. |
| 120 | + * Remember to also include the clean values after INSERT. |
| 121 | + * On UPDATE return return the returned profile plus the new clean values. |
| 122 | + */ |
| 123 | + |
| 124 | + $nickname_sql = isset($post_profile['nickname']) ? "'{$post_profile['nickname']}'" : (isset( |
| 125 | + $user['nickname']) ? "'{$user['nickname']}'" : "null"); |
| 126 | + $given_name_sql = isset($post_profile['given_name']) ? "'{$post_profile['given_name']}'" : (isset( |
| 127 | + $user['given_name']) ? "'{$user['given_name']}'" : "null"); |
| 128 | + $family_name_sql = isset($post_profile['family_name']) ? "'{$post_profile['family_name']}'" : (isset( |
| 129 | + $user['family_name']) ? "'{$user['family_name']}'" : "null"); |
| 130 | + $facebook_sql = "null"; |
| 131 | + $google_sql = "null"; |
| 132 | + $auth0_sql = "null"; |
| 133 | + $provider_sql = "null"; |
| 134 | + |
| 135 | + $profile['nickname'] = "null"; |
| 136 | + $profile['given_name'] = "null"; |
| 137 | + $profile['family_name'] = "null"; |
| 138 | + $profile['facebook_id'] = "null"; |
| 139 | + $profile['google_id'] = "null"; |
| 140 | + $profile['auth0_id'] = "null"; |
121 | 141 |
|
122 | 142 | switch ($user['identities'][0]['provider']) {
|
123 | 143 | case 'facebook':
|
124 |
| - $profile['provider'] = "facebook_id = '{$user['identities'][0]['user_id']}'"; |
| 144 | + $provider_sql = "facebook_id = '{$user['identities'][0]['user_id']}'"; |
| 145 | + $facebook_sql = "'{$user['identities'][0]['user_id']}'"; |
125 | 146 |
|
126 | 147 | break;
|
127 | 148 | case 'google-oauth2':
|
128 |
| - $profile['provider'] = "google_id = '{$user['identities'][0]['user_id']}'"; |
| 149 | + $provider_sql = "google_id = '{$user['identities'][0]['user_id']}'"; |
| 150 | + $google_sql = "'{$user['identities'][0]['user_id']}'"; |
129 | 151 |
|
130 | 152 | break;
|
131 | 153 | case 'auth0':
|
132 |
| - $profile['provider'] = "auth0_id = '{$user['identities'][0]['user_id']}'"; |
| 154 | + $provider_sql = "auth0_id = '{$user['identities'][0]['user_id']}'"; |
| 155 | + $auth0_sql = "'{$user['identities'][0]['user_id']}'"; |
133 | 156 |
|
134 | 157 | break;
|
135 | 158 | }
|
136 | 159 |
|
137 |
| - $profile['facebook_id'] = isset($user['facebook_id']) ? "'{$user['facebook_id']}'" : "null"; |
138 |
| - $profile['google_id'] = isset($user['google_id']) ? "'{$user['google_id']}'" : "null"; |
139 |
| - $profile['auth0_id'] = isset($user['auth0_id']) ? "'{$user['auth0_id']}'" : "null"; |
140 |
| - |
141 | 160 | $profile_sql = "SELECT * FROM for_profiles
|
142 |
| - WHERE email = {$profile['email']} |
| 161 | + WHERE email = '{$user['email']}' |
143 | 162 | ORDER BY nickname ASC;";
|
144 | 163 |
|
145 | 164 | $profile_result = mysqli_query($link, $profile_sql);
|
146 | 165 |
|
147 |
| - if ($profile_result) { |
| 166 | + if (! $profile_result) { |
| 167 | + header('HTTP/1.0 404 Not Found'); |
| 168 | + |
| 169 | + $events['mysql']['result'] = false; |
| 170 | + $events['mysql']['code'] = mysqli_errno($link); |
| 171 | + $events['mysql']['error'] = mysqli_error($link); |
| 172 | + |
| 173 | + echo json_encode( |
| 174 | + array( |
| 175 | + 'events' => $events |
| 176 | + )); |
| 177 | + exit(); |
| 178 | + } |
| 179 | + |
| 180 | + $profile = mysqli_fetch_assoc($profile_result); |
| 181 | + |
| 182 | + if ($profile) { |
148 | 183 | $profile_sql = "UPDATE for_profiles
|
149 |
| - SET nickname = '{$profile['nickname']}', |
150 |
| - given_name = '{$profile['given_name']}', |
151 |
| - family_name = '{$profile['family_name']}' |
152 |
| - {$profile['provider']} |
| 184 | + SET nickname = $nickname_sql, |
| 185 | + given_name = $given_name_sql, |
| 186 | + family_name = $family_name_sql, |
| 187 | + $provider_sql |
153 | 188 | WHERE email = '{$user['email']}';";
|
154 | 189 |
|
155 |
| - $profile['profile_id'] = ''; |
156 | 190 | $events['mysql']['operation'] = 'update';
|
157 |
| - |
158 |
| - $profile['profile_id'] = mysqli_fetch_assoc($profile_result)[0]['profile_id']; |
159 | 191 | } else {
|
160 | 192 | $profile_sql = "INSERT INTO for_profiles
|
161 |
| - (nickname, given_name, family_name, facebook_id, google_id, auth0_id) |
| 193 | + (email, nickname, given_name, family_name, facebook_id, google_id, auth0_id) |
162 | 194 | VALUES
|
163 |
| - ('{$profile['nickname']}', |
164 |
| - '{$profile['given_name']}', |
165 |
| - '{$profile['family_name']}', |
166 |
| - '{$profile['facebook_id']}', |
167 |
| - '{$profile['google_id']}', |
168 |
| - '{$profile['auth0_id']}');"; |
| 195 | + ('{$user['email']}', |
| 196 | + $nickname_sql, |
| 197 | + $given_name_sql, |
| 198 | + $family_name_sql, |
| 199 | + $facebook_sql, |
| 200 | + $google_sql, |
| 201 | + $auth0_sql);"; |
169 | 202 |
|
170 | 203 | $events['mysql']['operation'] = 'insert';
|
171 | 204 | }
|
172 | 205 |
|
173 | 206 | $profile_result = mysqli_query($link, $profile_sql);
|
174 | 207 |
|
175 |
| - if ($events['mysql']['operation'] == 'insert') { |
176 |
| - $profile['profile_id'] = mysqli_insert_id($link); |
177 |
| - } |
178 |
| - |
179 |
| - if (! $tag_result) { |
| 208 | + if (! $profile_result) { |
180 | 209 | header('HTTP/1.0 404 Not Found');
|
181 | 210 |
|
182 | 211 | $events['mysql']['result'] = false;
|
|
191 | 220 | exit();
|
192 | 221 | }
|
193 | 222 |
|
| 223 | + /** |
| 224 | + * One last fetch from the data base to get the updated profile. |
| 225 | + * One can update this here in PHP based on the JSON, |
| 226 | + * but I prefer to get the real thing from the data base. |
| 227 | + */ |
| 228 | + |
| 229 | + $profile_sql = "SELECT * FROM for_profiles |
| 230 | + WHERE email = '{$user['email']}' |
| 231 | + ORDER BY nickname ASC;"; |
| 232 | + |
| 233 | + $profile_result = mysqli_query($link, $profile_sql); |
| 234 | + |
194 | 235 | $events['mysql']['result'] = true;
|
195 | 236 |
|
| 237 | + $profile = mysqli_fetch_assoc($profile_result); |
| 238 | + |
196 | 239 | echo json_encode(
|
197 | 240 | array(
|
198 | 241 | 'profiles' => $profile,
|
|
0 commit comments