This lab will teach you how to use the Facebook login, Google Maps API, and camera through getUserMedia.
The core lesson here is that there are third party APIs that we can utilize and avoid doing work that's already been done. You'll also get more practice using callbacks as the main form of communication between client and server. Even if you're not using any of these features in your assignments, hopefully this will get you started on using creating richer web applications.
This isn't the only form callbacks take, or are used, but is an example! We'll see various forms of callbacks in the rest of the lab.
Wait.. node
just barfed... what do I do?
Try restarting your computer
{{> facebook}}
<script src="/js/facebook.js"></script>
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
function statusChangeCallback(response) {
console.log('Facebook login status changed.');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
console.log('Successfully logged in with Facebook');
FB.api('/me?fields=name,first_name,picture.width(480)', changeUser);
}
}
Wait how does checkLoginState()
get called?
If you look in your index.handlebars
, we put in the segment onlogin="checkLoginState();"
. This means after Facebook is done accepting your login API request, it will call the callback checkLoginState
for you. See how varied callbacks can be used!
It's not working!?
Check your javascript console in developer tools
How do I get the name from facebook to replace Michael Bernstein?
Remember that the response data from facebook is stored in the response
variable in changeUser
. What's in response
? Try printing it out onto the console!
How do I get the image from facebook to use in my img
?
This is going to be similar to how we did it for name. Try peeking into what's inside response
by logging it on the console. Try and see if you can get the URL to your facebook image to print onto the console.
How do I replace just the src
attribute of img
?
You can change a single attribute of an element with the jQuery call $.attr('src',
. Read more about it in the jQuery docs!
You can find your API key again by clicking 'Get a Key' again (same button as before) and selecting your app name from the dropdown, and clicking 'Enable Api'
<script src="/js/googleMaps.js"></script>
{{> googleMaps}}
function initMap() {
// Create center marker at UCSD
var ucsd_ltlng = {lat:32.880, lng:-117.236};
// Create a map object and specify the DOM element for display.
var map = new google.maps.Map(document.getElementById('map'), {
center: ucsd_ltlng,
zoom: 15
});
var marker = new google.maps.Marker({
position: ucsd_ltlng,
map: map,
title: 'UCSD'
});
}
Wait, how does initMap()
get called?
If you look in your googleMaps.handlebars
, we put in the segment &callback=initMap
. This means after Google Maps is done accepting your API request, it will call the callback initMap
for you. Another example of the varied usage of callbacks!
What exactly are we implementing here?
The video tag will show the video feed from our webcam. The canvas will show the image once we've taken the shot. Finally, the button will actually take the image!
Whoa whoa slow down. How does all this work?
Well it's slightly outside of the scope of this class, but you can read the capture
function to understand how the video, canvas, and button are hooked together. The successCallback
hooks up the video
element to show the stream, and button
's click to an event that draws the current image in video
to the canvas
. You can also see Dev.Opera - Media Capture in Mobile Browsers for a more detailed explanation.
<script src="/js/camera.js"></script>
Make sure to uncomment the camera code in your index.handlebars
Make sure to use https
Make sure to go to https://<Your Heroku App Name>.herokuapp.com as these 3rd party APIs will not work without it!
git status
git add ...
git commit -m "Third party APIs"
git push
exports.addTask = function(req, res){
var taskName = req.body.taskName;
// This will print in your terminal when a POST is made
console.log(taskName);
data.tasks.push(taskName); // Add to current data
// Lets send our task name back so our browser knows it worked out!
res.send(taskName);
}
app.post('/addTask', tasks.addTask);
<form id='taskForm' action='/addTask' method='post'>
<label for='addTask'>Add a task</label>
<input id='addTask' name='taskName' placeholder='New Task' />
<button type='submit' class='btn btn-primary'>Submit</button>
</form>
// Your code goes here
$(document).ready(function() {
$('#taskForm').submit(function(e) {
// Prevents default submit + reload (we only want the submit part)
e.preventDefault();
var taskName = $('#addTask').val();
// Send the POST request
$.post('addTask', { taskName: taskName }, postCallback);
});
});
function postCallback(res) {
$('.taskList').append('<li>' + res + '</li>'); // Add to the list
$('#addTask').val(''); // Clear form
}
<ul class='nav nav-pills nav-justified'>
<!-- href='#idName' scrolls to the div with the id idName -->
<li><a href='/#'>Home</a></li>
<li><a href='tasks'>Tasks</a></li>
<li><a href='/#facebookPhoto'>Facebook</a></li>
<li><a href='/#camera'>Camera</a></li>
<li><a href='/#googleMaps'>Google Maps</a></li>
</ul>
Make sure to use https
Make sure to go to https://<Your Heroku App Name>.herokuapp.com as these 3rd party APIs will not work without it!
git status
git add ...
git commit -m "added partials"
git push
Four stretch goals this time. Pick at least one!