diff --git a/controllers/auth.php b/controllers/auth.php index b8f9e1a..c268aff 100644 --- a/controllers/auth.php +++ b/controllers/auth.php @@ -110,6 +110,13 @@ $app->get('/auth/start', function() use($app) { // Generate a "state" parameter for the request $state = IndieAuth\Client::generateStateParameter(); $_SESSION['auth_state'] = $state; + + // Store whether to return to the Pebble settings tab or the new post page after signing in + if(array_key_exists('redirect', $params) && $params['redirect'] == 'settings') { + $_SESSION['redirect_after_login'] = '/pebble/settings/finished'; + } else { + $_SESSION['redirect_after_login'] = '/new'; + } if($tokenEndpoint && $micropubEndpoint && $authorizationEndpoint) { $scope = 'post'; @@ -224,7 +231,7 @@ $app->get('/auth/callback', function() use($app) { $micropubEndpoint = IndieAuth\Client::discoverMicropubEndpoint($me); $tokenEndpoint = IndieAuth\Client::discoverTokenEndpoint($me); - $redirectToDashboardImmediately = false; + $skipDebugScreen = false; if($tokenEndpoint) { // Exchange auth code for an access token @@ -248,7 +255,7 @@ $app->get('/auth/callback', function() use($app) { // No token endpoint was discovered, instead, verify the auth code at the auth server or with indieauth.com // Never show the intermediate login confirmation page if we just authenticated them instead of got authorization - $redirectToDashboardImmediately = true; + $skipDebugScreen = true; if(!$authorizationEndpoint) { $authorizationEndpoint = 'https://indieauth.com/auth'; @@ -284,7 +291,7 @@ $app->get('/auth/callback', function() use($app) { $user->last_login = date('Y-m-d H:i:s'); // If they have logged in before and we already have an access token, then redirect to the dashboard now if($user->access_token) - $redirectToDashboardImmediately = true; + $skipDebugScreen = true; } else { // New user! Store the user in the database $user = ORM::for_table('users')->create(); @@ -303,8 +310,8 @@ $app->get('/auth/callback', function() use($app) { unset($_SESSION['auth_state']); - if($redirectToDashboardImmediately) { - $app->redirect('/new', 301); + if($skipDebugScreen) { + $app->redirect($_SESSION['redirect_after_login'], 301); } else { $html = render('auth_callback', array( 'title' => 'Sign In', @@ -314,7 +321,8 @@ $app->get('/auth/callback', function() use($app) { 'tokenEndpoint' => $tokenEndpoint, 'auth' => $token['auth'], 'response' => $token['response'], - 'curl_error' => (array_key_exists('error', $token) ? $token['error'] : false) + 'curl_error' => (array_key_exists('error', $token) ? $token['error'] : false), + 'redirect' => $_SESSION['redirect_after_login'] )); $app->response()->body($html); } diff --git a/controllers/controllers.php b/controllers/controllers.php index a8bc0a3..6ec2895 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -24,6 +24,14 @@ function require_login(&$app) { } } +function get_login(&$app) { + if(array_key_exists('user_id', $_SESSION)) { + return ORM::for_table('users')->find_one($_SESSION['user_id']); + } else { + return false; + } +} + function generate_login_token() { return JWT::encode(array( 'user_id' => $_SESSION['user_id'], @@ -51,9 +59,23 @@ $app->get('/new', function() use($app) { }); $app->get('/pebble/settings', function() use($app) { + $html = render('pebble-settings-login', array( + 'title' => 'Log In' + )); + $app->response()->body($html); +}); + +$app->get('/pebble/settings/finished', function() use($app) { if($user=require_login($app)) { + $token = JWT::encode(array( + 'user_id' => $_SESSION['user_id'], + 'me' => $_SESSION['me'], + 'created_at' => time() + ), Config::$jwtSecret); + $html = render('pebble-settings', array( - 'title' => 'Pebble Settings' + 'title' => 'Pebble Settings', + 'token' => $token )); $app->response()->body($html); } diff --git a/views/auth_callback.php b/views/auth_callback.php index 7274cd7..4102fa9 100644 --- a/views/auth_callback.php +++ b/views/auth_callback.php @@ -27,7 +27,7 @@
All required values were found! You are now signed in.
- + diff --git a/views/index.php b/views/index.php index a6cbafb..4bb2ea7 100644 --- a/views/index.php +++ b/views/index.php @@ -5,7 +5,7 @@Teacup is a simple app for tracking what you are drinking.
-To use Teacup, sign in with your domain. If your website supports Micropub, it can log posts directly to your site. Otherwise, it will post to your profile on this website.
+To use Teacup, sign in with your domain. If your website supports Micropub, it will log posts directly to your site. Otherwise, it will post to your profile on this website.