SelfLogin host me code scope = do
  let domain = fromMaybe "localhost" host
  isTestMode ← getConfOpt testMode
  --let isTestMode = False
  isHttpOnly ← not <$> getConfOpt allowJsCookieAccess
  let security = if isTestMode then "" else "; Secure; SameSite=Strict"
        ++ if isHttpOnly then "; HttpOnly" else ""
  result ← postLogin host [ ("me", fromMaybe ("https://" ++ domain) me)
                          , ("code", fromMaybe "" code)
                          , ("scope", fromMaybe "post create update delete read follow mute block channels" scope)
                          , ("redirect_uri", "https://" ++ domain ++ "/login/self")
                          , ("client_id", "https://" ++ domain ++ "/")
                          , ("grant_type", "authorization_code") ]
  throwM err303 { errHeaders = [ ("Set-Cookie", "Bearer=" ++ (cs $ accessToken result) ++ "; Path=/; Max-Age=5184000" ++ security)
                               , (hLocation, "/") ] }

getTestLogin ∷ Maybe Text → Maybe Text → Maybe Text → Sweetroll NoContent
getTestLogin host state redire
getSelfLogin ∷ Maybe Text → Maybe Text → Maybe Text → Maybe Text → Sweetroll NoContent
getSelfLogin host me code scope = do
  let domain = fromMaybe "localhost" host
  isTestMode ← getConfOpt testMode
  --let isTestMode = False
  isHttpOnly ← not <$> getConfOpt allowJsCookieAccess
  let security = if isTestMode then "" else "; Secure; SameSite=Strict"
        ++ if isHttpOnly then "; HttpOnly" else ""
  result ← postLogin host [ ("me", fromMaybe ("https://" ++ domain) me)
                          , ("code", fromMaybe "" code)
                          , ("scope", fromMaybe "post create update delete read follow mute block channels" scope)
                          , ("redirect_uri", "https://" ++ domain ++ "/login/self")
                          , ("client_id", "https://" ++ domain ++ "/")
                          , ("grant_type", "authorization_code") ]
  throwM err303 { errHeaders = [ ("Set-Cookie", "Bearer=" ++ (cs $ accessToken result) ++ "; Path=/; Max-Age=5184000" ++ security)
                               , (hLocation, "/") ] }
getSelfLogin host me code scope = do
  let domain = fromMaybe "localhost" host
  isTestMode ← getConfOpt testMode
  --let isTestMode = False
  isHttpOnly ← not <$> getConfOpt allowJsCookieAccess
  let security = if isTestMode then "" else "; Secure; SameSite=Strict"
        ++ if isHttpOnly then "; HttpOnly" else ""
  result ← postLogin host [ ("me", fromMaybe ("https://" ++ domain) me)
                          , ("code", fromMaybe "" code)
                          , ("scope", fromMaybe "post create update delete read follow mute block channels" scope)
                          , ("redirect_uri", "https://" ++ domain ++ "/login/self")
                          , ("client_id", "https://" ++ domain ++ "/")
                          , ("grant_type", "authorization_code") ]
  throwM err303 { errHeaders = [ ("Set-Cookie", "Bearer=" ++ (cs $ accessToken result) ++ "; Path=/; Max-Age=5184000" ++ security)
                               , (hLocation, "/") ] }