--------------------------------------------------------------------------------

-- | Returns the user information based on the Auth0 access token (obtained during login).
--
userInfo :: AccessToken -> Config -> ExceptT T.Text IO UserInfo
userInfo token Config{..} = do
  resp <- liftIO $ getWith opts (T.unpack $ basePath <> "/userinfo") 
  case AE.eitherDecode (resp ^. responseBody) of
    (Left  err) -> throwError $ T.pack $ show err
    (Right ui)  -> return ui
  where
    auth = BS.pack $ T.unpack $ T.concat ["Bearer ", (getToken token)]
    opts = defaults & header "Authorization" .~ [auth]
      
-- | Validates a JSON Web Token (signature and expiration)
-- and returns the user information associated with the user id (sub property) of the token.
userInfo :: AccessToken -> Config -> ExceptT T.Text IO UserInfo
userInfo token Config{..} = do
  resp <- liftIO $ getWith opts (T.unpack $ basePath <> "/userinfo") 
  case AE.eitherDecode (resp ^. responseBody) of
    (Left  err) -> throwError $ T.pack $ show err
    (Right ui)  -> return ui
  where
    auth = BS.pack $ T.unpack $ T.concat ["Bearer ", (getToken token)]
    opts = defaults & header "Authorization" .~ [auth]
      
-- | Validates a JSON Web Token (signature and expiration)
-- and returns the user information associated with the user id (sub property) of the token.
userInfo token Config{..} = do
  resp <- liftIO $ getWith opts (T.unpack $ basePath <> "/userinfo") 
  case AE.eitherDecode (resp ^. responseBody) of
    (Left  err) -> throwError $ T.pack $ show err
    (Right ui)  -> return ui
  where
    auth = BS.pack $ T.unpack $ T.concat ["Bearer ", (getToken token)]
    opts = defaults & header "Authorization" .~ [auth]
      
-- | Validates a JSON Web Token (signature and expiration)
-- and returns the user information associated with the user id (sub property) of the token.