# Integrate Auth0 with Ktor single-page app

**URL:** <https://community.auth0.com/t/integrate-auth0-with-ktor-single-page-app/92985>\
**Category:** Get Help\
**Tags:** api, kotlin, auth0\
**Created:** [October 24, 2022, 3:19pm UTC](https://community.auth0.com/t/integrate-auth0-with-ktor-single-page-app/92985 "2022-10-24T15:19:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jperfit](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jperfit/32/36846_2.png) [@jperfit](https://community.auth0.com/u/jperfit)\
**Post date:** [October 24, 2022, 3:19pm UTC](https://community.auth0.com/t/integrate-auth0-with-ktor-single-page-app/92985/1 "2022-10-24T15:19:07Z")

</div>

Hi, all. I’m developing a Ktor single-page web app and I intend to integrate Auth0 with it but I haven’t found enough documentation for Auth0 with Kotlin so far. I’m following the steps described on [Adding Auth0 Authorization to a Ktor HTTP API](https://auth0.com/blog/adding-auth0-authorization-to-a-ktor-http-api/), but I’m struggling to connect our web app to my Auth0 API and App. I really hope I could get some help from you.

Below is an extract of the main parts of the code. I’m getting “HTTP ERROR 401” when trying [http://localhost:8080/api/messages/protected](http://localhost:8080/api/messages/protected).

fun Route.homepage() {

```
get("/api/messages/public") {
    call.respondText(
        """{"message": "The API doesn't require an access token to share this message."}""",
        contentType = ContentType.Application.Json
    )
}

authenticate("auth0") {

    get("/api/messages/protected") {
        call.respondText(
            """{"message": "The API successfully validated your access token."}""",
            contentType = ContentType.Application.Json
        )
    }
}

```

}

fun validateCreds(credential: JWTCredential): JWTPrincipal? {

```
val containsAudience = credential.payload.audience.contains(System.getenv("AUDIENCE"))

if (containsAudience) {
    return JWTPrincipal(credential.payload)
}

return null

```

}

fun Application.module() {

```
install(ContentNegotiation) {
    json()
}

install(CORS) {
    method(HttpMethod.Get)
    method(HttpMethod.Post)
    method(HttpMethod.Delete)
    header(HttpHeaders.ContentType)  

    //anyHost()
    method(HttpMethod.Options)
    method(HttpMethod.Get)
    header("authorization")
    allowCredentials = true
    allowNonSimpleContentTypes = true

}

val jwkProvider = JwkProviderBuilder(System.getenv("ISSUER"))
    .cached(10, 24, TimeUnit.HOURS)
    .rateLimited(10, 1, TimeUnit.MINUTES)
    .build()

install(Authentication) {
    jwt("auth0") {
        verifier(jwkProvider, System.getenv("ISSUER"))
        validate { credential -> validateCreds(credential) }
    }
}

install(Compression) {
    gzip()
}

routing {
    homepage()
}

```

}

I’ve set ISSUER and AUDIENCE as environment variables

 ![Ktor-Auth0](https://global.discourse-cdn.com/auth0/original/3X/f/1/f139e565fc810a3eeb4cd538c8381c2acd4f1592.png)

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [October 25, 2022, 11:26pm UTC](https://community.auth0.com/t/integrate-auth0-with-ktor-single-page-app/92985/3 "2022-10-25T23:26:26Z")

</div>

Hey there @jperfit welcome to the community!

I admittedly don’t have a ton of experience with Ktor (think it’s awesome 😄) but I have been able to successfully validate an access token from a React SPA with a Ktor API, similar to the blog post you referenced. Nothing really stands out in the code you’ve shared.

> [@jperfit](#):
>
> Below is an extract of the main parts of the code. I’m getting “HTTP ERROR 401” when trying [http://localhost:8080/api/messages/protected](http://localhost:8080/api/messages/protected).

Given you’re receiving a 401, I’d expect to see a reason why in the logging where you have your API running. Are you able to confirm?

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mraible/32/32776_2.png) [@mraible](https://community.auth0.com/u/mraible)\
**Post date:** [October 26, 2022, 8:51pm UTC](https://community.auth0.com/t/integrate-auth0-with-ktor-single-page-app/92985/4 "2022-10-26T20:51:25Z")

</div>

Do you have a trailing slash on your Auth0 issuer’s value?

---

<div class="post-metadata">

**Author:** ![jperfit](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jperfit/32/36846_2.png) [@jperfit](https://community.auth0.com/u/jperfit)\
**Post date:** [October 27, 2022, 8:51pm UTC](https://community.auth0.com/t/integrate-auth0-with-ktor-single-page-app/92985/5 "2022-10-27T20:51:53Z")

</div>

> [@ty.frith](#):
>
> ct SPA with a Ktor API, s

Hi @mraible. I didn’t have a trailing slash, I add it and nothing changed 😕

---

<div class="post-metadata">

**Author:** ![jperfit](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jperfit/32/36846_2.png) [@jperfit](https://community.auth0.com/u/jperfit)\
**Post date:** [October 27, 2022, 8:56pm UTC](https://community.auth0.com/t/integrate-auth0-with-ktor-single-page-app/92985/6 "2022-10-27T20:56:03Z")

</div>

> [@ty.frith](#):
>
> en you’re receiving a 401, I’d expect to see a reason why in the

Hi @ty.frith. Thanks for the replay. Do you mean in my Auth0 Dashboard?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [October 27, 2022, 9:23pm UTC](https://community.auth0.com/t/integrate-auth0-with-ktor-single-page-app/92985/7 "2022-10-27T21:23:32Z")

</div>

Hey @jperfit! No problem, happy to help where I can 🙂

I’m referring to logging from the API itself - I used the KTor sample API from the guide you linked and was receiving a `401` as well at first, but when I looked at the logging from the API (I just ran it in VS Code) it was complaining about the `iss` value like:

```auto
io.ktor.auth.jwt - Token verification failed: The Claim 'iss' value doesn't match the required issuer.

```

Once I made sure the issuer I defined in my SPA code to authorize/receive tokens was _exactly_ the same as the issuer defined in the KTor API I was able to validate successfully.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [December 22, 2022, 11:48pm UTC](https://community.auth0.com/t/integrate-auth0-with-ktor-single-page-app/92985/8 "2022-12-22T23:48:39Z")

</div>


