(Request request, Response response)
| 34 | // main page (http://localhost:5000/) |
| 35 | Spark.get(new Route("/") { |
| 36 | public Object handle(Request request, Response response) { |
| 37 | String token = request.session().attribute("token"); |
| 38 | // if the user has a token they're logged in |
| 39 | if (token != null) { |
| 40 | try { |
| 41 | // example request gets information about logged in user |
| 42 | Client client = Client.oauth(getApp(token)); |
| 43 | User me = client.users.me().execute(); |
| 44 | return "<p>Hello " + me.name + "</p><p><a href=\"/logout\">Logout</a></p>"; |
| 45 | } catch (IOException e) { |
| 46 | return e.getStackTrace().toString(); |
| 47 | } |
| 48 | } |
| 49 | // if we don't have a token show a "Sign in with Asana" button |
| 50 | else { |
| 51 | // get an authorization URL and anti-forgery "state" token |
| 52 | String state = UUID.randomUUID().toString(); |
| 53 | String authUrl = getApp().getAuthorizationUrl(state); |
| 54 | // persist the state token in the user's session |
| 55 | request.session().attribute("state", state); |
| 56 | // link the button to the authorization URL |
| 57 | return "<p><a href=\"" + authUrl + "\">" + |
| 58 | "<img src=\"https://github.com/Asana/oauth-examples/raw/master/public/asana-oauth-button-blue.png?raw=true\">" + |
| 59 | "</a></p>"; |
| 60 | } |
| 61 | } |
| 62 | }); |
| 63 | |
| 64 | // logout endpoint |
nothing calls this directly
no test coverage detected