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

-- | Login with username\/password combination
--   in case of SMS login it's phone-number\/access_token  
login :: T.Text -> T.Text -> Config -> ExceptT T.Text IO AccessToken
login user pass Config{..}= do
  resp <- liftIO $ post (T.unpack $ basePath <> "/oauth/ro") requestParams
  case AE.eitherDecode (resp ^. responseBody) of
    (Left err)  -> throwError $ T.pack $ show err
    (Right tok) -> return tok
  where
    requestParams =
      [ "client_id"     := clientId
      , "connection"    := ("sms" :: T.Text)
      , "grant_type"    := ("password" :: T.Text)
      , "username"      := user
      , "password"      := pass
      , "scope"         := ("openid" :: T.Text) -- or "openid name email"  
      ]
login :: T.Text -> T.Text -> Config -> ExceptT T.Text IO AccessToken
login user pass Config{..}= do
  resp <- liftIO $ post (T.unpack $ basePath <> "/oauth/ro") requestParams
  case AE.eitherDecode (resp ^. responseBody) of
    (Left err)  -> throwError $ T.pack $ show err
    (Right tok) -> return tok
  where
    requestParams =
      [ "client_id"     := clientId
      , "connection"    := ("sms" :: T.Text)
      , "grant_type"    := ("password" :: T.Text)
      , "username"      := user
      , "password"      := pass
      , "scope"         := ("openid" :: T.Text) -- or "openid name email"  
      ]
login user pass Config{..}= do
  resp <- liftIO $ post (T.unpack $ basePath <> "/oauth/ro") requestParams
  case AE.eitherDecode (resp ^. responseBody) of
    (Left err)  -> throwError $ T.pack $ show err
    (Right tok) -> return tok
  where
    requestParams =
      [ "client_id"     := clientId
      , "connection"    := ("sms" :: T.Text)
      , "grant_type"    := ("password" :: T.Text)
      , "username"      := user
      , "password"      := pass
      , "scope"         := ("openid" :: T.Text) -- or "openid name email"  
      ]