From 6534eeb1a2017f16c62ed27bf5ff46d51666eff8 Mon Sep 17 00:00:00 2001 From: Diogo Biazus Date: Sun, 20 Dec 2015 15:04:18 -0500 Subject: [PATCH] Adds conclusion and note about token TTL --- docs/examples/external_auth.md | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/docs/examples/external_auth.md b/docs/examples/external_auth.md index e9a191ed5..8df6d906f 100644 --- a/docs/examples/external_auth.md +++ b/docs/examples/external_auth.md @@ -106,6 +106,16 @@ class ApiTokensController < ApplicationController end ``` +
+

Token Time to Live

+

+ In the code above we leverage the HTTP time based cache headers to expire the + endpoint cache at the same time as the token. In this example we have a token + that will be refresh one hour after its issuing time. + That's why both are based on the TOKEN_TTL constant. +

+
+ We also need to create a route in the ```config/routes.rb``` file: ```ruby @@ -166,9 +176,18 @@ Now whenever you are authenticated in your Rails application you can use some Ja code to get the token and use it: ```javascript $.getJSON('/api_json').done(function(data){ - $.ajax('/orders', {'Authorization': 'Bearer ' + data.token}) - }) - .fail(function(){ - console.log('Error fetching API token'); + $.ajax('/orders', {'Authorization': 'Bearer ' + data.token}).done(function(data){ + console.log('Visible Orders: ', data); }) +}).fail(function(){ + console.log('Error fetching API token'); +}) ``` +We could also store the token to avoid having to fetch it again in the same page. + +### Conclusion + +This section explained the implementation details for building an +external authentication system working with PostgREST. +With the previous [User Management](users/) example this should give a clearer +idea of how to set up authentication for your API.