{"_id":"571be895395f6e0e007437d4","category":{"_id":"571d5ae118b3c10e003e55cd","version":"56a83b989ec7660d002e07c1","__v":0,"project":"56a83b979ec7660d002e07be","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-04-24T23:46:41.975Z","from_sync":false,"order":0,"slug":"tutorials","title":"Tutorials"},"parentDoc":null,"version":{"_id":"56a83b989ec7660d002e07c1","project":"56a83b979ec7660d002e07be","__v":9,"createdAt":"2016-01-27T03:38:00.333Z","releaseDate":"2016-01-27T03:38:00.333Z","categories":["56a83b989ec7660d002e07c2","56a83c282036420d002d21e1","56a96de8f834950d0037b35a","56a9706013a69a0d00a778c3","56a970ec2d8fd90d0036eec7","56a971a62bb3910d000ee934","56a973372d8fd90d0036eece","56a978dc2bb3910d000ee93f","571d5ae118b3c10e003e55cd"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"Beta","version_clean":"1.0.0","version":"1.0"},"__v":20,"user":"56a975c12d8fd90d0036eed0","project":"56a83b979ec7660d002e07be","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-04-23T21:26:45.018Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"<div class=\"corner-ribbon top-left sticky blue\"><a style=\"color: white;\" href=\"https://pathfinder.readme.io/blog/pathfinder-now-in-public-beta\">Public Beta!</a></div>\n\nThis tutorial will walk you through how to use the callbacks in the JavaScript SDK. It is expected that you have already read [Getting Started](doc:getting-started).\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Authentication\"\n}\n[/block]\nTo use server hosted authentication, just pass the google id token into the Pathfinder constructor.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var pf = new Pathfinder(MY_APP_ID, ID_TOKEN);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nTo use your own authentication, you have two options. Option 1 is to specify the authentication server url as a constructor argument.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var pf = new Pathfinder(MY_APP_ID, AUTH_INFO, \\\"my_auth_server_url\\\");\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThis causes the Pathfinder object to send a get request to my_auth_server_url, passing along AUTH_INFO, and the connection id as query parameters:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"GET HTTPS://my_auth_server.com?user_id=AUTH_INFO&connection_id=CONNECTION_ID&application_id=APP_ID\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nAUTH_INFO can be any kind of string that you can use to identify or authenticate the user with.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Overriding the authenticate function in Pathfinder\"\n}\n[/block]\nAnother way to implement custom authentication besides using constructor arguments is to assign your own authenticate function.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Pathfinder.prototype.authenticate = function(connection_id, next){\\n  // do something with connection id to prepare your authentication server\\n  next();\\n};\\n\\nvar pf = new Pathfinder(APP_ID);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nWhen you replace the authenticate function, you only need to provide the application id into the constructor argument.","excerpt":"","slug":"javascript-callbacks","type":"basic","title":"JavaScript"}
<div class="corner-ribbon top-left sticky blue"><a style="color: white;" href="https://pathfinder.readme.io/blog/pathfinder-now-in-public-beta">Public Beta!</a></div> This tutorial will walk you through how to use the callbacks in the JavaScript SDK. It is expected that you have already read [Getting Started](doc:getting-started). [block:api-header] { "type": "basic", "title": "Authentication" } [/block] To use server hosted authentication, just pass the google id token into the Pathfinder constructor. [block:code] { "codes": [ { "code": "var pf = new Pathfinder(MY_APP_ID, ID_TOKEN);", "language": "javascript" } ] } [/block] To use your own authentication, you have two options. Option 1 is to specify the authentication server url as a constructor argument. [block:code] { "codes": [ { "code": "var pf = new Pathfinder(MY_APP_ID, AUTH_INFO, \"my_auth_server_url\");", "language": "javascript" } ] } [/block] This causes the Pathfinder object to send a get request to my_auth_server_url, passing along AUTH_INFO, and the connection id as query parameters: [block:code] { "codes": [ { "code": "GET HTTPS://my_auth_server.com?user_id=AUTH_INFO&connection_id=CONNECTION_ID&application_id=APP_ID", "language": "text" } ] } [/block] AUTH_INFO can be any kind of string that you can use to identify or authenticate the user with. [block:api-header] { "type": "basic", "title": "Overriding the authenticate function in Pathfinder" } [/block] Another way to implement custom authentication besides using constructor arguments is to assign your own authenticate function. [block:code] { "codes": [ { "code": "Pathfinder.prototype.authenticate = function(connection_id, next){\n // do something with connection id to prepare your authentication server\n next();\n};\n\nvar pf = new Pathfinder(APP_ID);", "language": "javascript" } ] } [/block] When you replace the authenticate function, you only need to provide the application id into the constructor argument.