<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Laurence Sonnenberg on Medium]]></title>
        <description><![CDATA[Stories by Laurence Sonnenberg on Medium]]></description>
        <link>https://medium.com/@yukigeshiki?source=rss-1b136fde6fc0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*rNp6GeW8-pe_35FnwlyKmQ.jpeg</url>
            <title>Stories by Laurence Sonnenberg on Medium</title>
            <link>https://medium.com/@yukigeshiki?source=rss-1b136fde6fc0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 13:58:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yukigeshiki/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Integrate Firebase Authentication with Ktors Auth Feature]]></title>
            <link>https://levelup.gitconnected.com/how-to-integrate-firebase-authentication-with-ktors-auth-feature-dc2c3893a0cc?source=rss-1b136fde6fc0------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc2c3893a0cc</guid>
            <category><![CDATA[firebaseauthentication]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[ktor]]></category>
            <dc:creator><![CDATA[Laurence Sonnenberg]]></dc:creator>
            <pubDate>Tue, 19 Jan 2021 09:11:05 GMT</pubDate>
            <atom:updated>2021-01-20T13:57:44.894Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Integrate Firebase Authentication with the Ktor Auth Feature</h3><h4>Why not let the experts handle your user management?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IkqZ2kEnDdW40WfN07K5tg.jpeg" /></figure><p>If you’re like me, when developing an application, you know security needs to be a priority; you also know that it takes great depth of security knowledge to develop a solid authentication system that’s good enough to ward off todays attacks. This is why it’s often best left to the experts to develop these sorts of systems — experts like those at Google who have built <a href="https://firebase.google.com/docs/auth">Firebase’s Authentication</a> system, which is easy to implement and maintain for most small to medium sized apps… whether they be web or mobile.</p><p>I’m a huge fan of the Kotlin programming language, so I have recently started building backend applications using the open source, Jetbrains developed framework, <a href="https://ktor.io/">Ktor</a>. In this article I’m going to show you how to integrate Firebase Authentication with the Auth feature already provided by the Ktor framework.</p><p>We all know that recreating the wheel is often just a waste of developer time, so I have used the current <a href="https://github.com/ktorio/ktor/tree/master/ktor-features/ktor-auth-jwt">JWT feature</a> code as a starting point and edited it to use Firebase instead of a custom JWT implementation, so I must thank the amazing developers at Jetbrains who wrote the original code… thanks guys! :)</p><p>This article assumes basic knowledge of the Ktor framework — if you’d like to learn more about the basics though, there are loads of tutorials covering those topics online! I used quite a few of them when I was getting started recently.</p><p>First up we’re going to create a new Ktor project using the IntelliJ IDEA Ktor plugin. We’ll choose which features we’d like to use, and we’ll tell the plugin that we’d like to use the Kotlin Gradle DSL. You can choose how you’d like to deal with content negotiation, I’m using GSON. For the HTTPClient Engine I’m using Jetty, and of course we’ll also need to tick Authentication. I’ve also ticked call logging so I can see what endpoints I’ve called.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XvBhWTD7dc2GEQpgWkfK8A.png" /></figure><p>Once you’re done choosing features follow the prompts until you have an initialised project. To complete the Ktor setup replace the current build.gradle.kts file with the one below and load the changes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c954afaebeecf83e4d04c0177b7b5560/href">https://medium.com/media/c954afaebeecf83e4d04c0177b7b5560/href</a></iframe><p>Now let’s setup our Firebase Project and generate a new private key for the Firebase Admin setup. The Firebase docs are pretty good, so if you’re not sure how to create a Firebase project, head over <a href="https://firebase.google.com/docs">here</a> to find out. Once you have a new project, navigate to the project settings, then to service accounts. There you can click the generate new private key button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*igOAQS6-y2_yh6XwhUcarw.png" /></figure><p>This will download a json file which you then place in the resources directory of your project, and REMEMBER if you plan to push to a public repo, add it to your .gitignore file!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*iFivbhOB0ZeBc3lU6GPDvg.png" /></figure><p>Great, we’re setup and can start looking at the code :)</p><p>First up, let’s add a few more Kotlin files. From your root directory, create the file auth/firebase/FirebaseAuth.kt, then create object files config/firebase/AuthConfig and config/firebase/FirebaseAdmin, and lastly create data class file model/User. Once you’re done your folder structure should look like the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*w-9KrcXsISnSbxOrJbaAGw.png" /></figure><p>Let’s start with the Firebase Admin code. In the FirebaseAdmin file paste the below code (remember to change the name of the json file to the one you downloaded),</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94623d958a173a0267a04f2622251151/href">https://medium.com/media/94623d958a173a0267a04f2622251151/href</a></iframe><p>Then add the lineFirebaseAdmin.init() above the feature install code in your Application.kt file. This will use the credentials you downloaded to initialise your Firebase app.</p><p>Next up add the following code to your FirebaseAuth.kt file,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38683efd09aea247d4bde4a5bde9f95c/href">https://medium.com/media/38683efd09aea247d4bde4a5bde9f95c/href</a></iframe><p>The above code supplies an AuthenticationProvider which is then built with the nested Configuration class. This Configuration class provides access to the token and a lambda function (which we will set up later) to supply the Principle (being the User).</p><p>Next we have the Authentication.Configuration.firebase extension function which will be doing all the actual work for us. Here we build the AuthenticationProvider, we then use it to setup an interceptor; inside this interceptor, we fetch the token and check that the token is valid. Lastly we invoke the lambda to fetch our Principle, and of course, we do this within a try/catch so that we can catch and handle any errors that might occur.</p><p>Now let’s move onto the AuthConfig and User files. The AuthConfig will supply the lambda function to our Configuration so that we can fetch our Principle and the the User file will be our data model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0eb92d3a8571db91cd071dcd3b921e41/href">https://medium.com/media/0eb92d3a8571db91cd071dcd3b921e41/href</a></iframe><p>Notice that our User data class above implements the Principle interface. This is because our Configuration is expecting that type. Once you’ve added the User data class code, add the below code to your AuthConfig file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0b3bd9a849fbb3fba51d143b614cf9a/href">https://medium.com/media/a0b3bd9a849fbb3fba51d143b614cf9a/href</a></iframe><p>The above code supplies an extension function for our Configuration class, and as I mentioned earlier, this then supplies the lambda to fetch our Principle. Lastly we’ll edit our Application.kt file to use all the code we’ve added.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c07549ffb11444a7a88f9ea2d3d669dd/href">https://medium.com/media/c07549ffb11444a7a88f9ea2d3d669dd/href</a></iframe><p>In the above code you can see that when the Authentication feature is installed we’ve used our firebase extension function and handed it our configure extension function. Now when we make any calls to authenticated endpoints, our application will look for a token in the request headers, if found the token will be validated and finally our User data will be added to the call and can be retrieved by calling call.principle&lt;User&gt;().</p><p>And with all of this, we can now signup/login with Firebase on our frontend and get a bearer token to use when making secure calls to our backend. We can let Firebase take care of things like refreshing the token, sending email address validation emails and updating a User’s email or password.</p><p>I hope this has been useful :) If you’d like the full project code it can be found on my GitHub: <a href="https://github.com/Yukigeshiki/ktor-firebase-auth">https://github.com/Yukigeshiki/ktor-firebase-auth</a> and if you’ve spotted anywhere this can be improved, please let me know in the comments!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc2c3893a0cc" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/how-to-integrate-firebase-authentication-with-ktors-auth-feature-dc2c3893a0cc">How to Integrate Firebase Authentication with Ktors Auth Feature</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>