Is Kotlin actually Good ? + Authentication (Day 3) - Creating a SaaS Startup in 30 Days

sotergreco

Sotiris Kourouklis

Posted on May 4, 2024

Is Kotlin actually Good ? + Authentication (Day 3) - Creating a SaaS Startup in 30 Days

I spent the last 2 days reading about Kotlin and Spring Boot. Coming from a Laravel background I will say that is different and looks and feels a lot more stable and enterprise.

Today we are going to see:

  • Why I switched from Gradle to Maven

  • Implementing JWT Authentication in Spring Boot

  • How it feels writing Kotlin and Spring Boot

Mavena

The JVM has its own world of packages and libraries, and as I've spent a lot of time trying to make Gradle work, Maven just worked out of the box for me. Plus, this website https://mvnrepository.com/, which is the alternative to npm but for the JVM, is literally called Maven, and I believe even Gradle users use this website to search for packages.

In terms of performance, Gradle should be faster, but for me, it was the same. Also, I like the pom.xml more than the Gradle configuration.

Spring Configuration

Spring has its own set of configurations in an application.properties file, which I quite disliked, so I switched to a .yml configuration, which is a lot better.

And unlike other frameworks, Spring is highly configurable through application.yml. I could even change the way my database behaved each time I started the app.

With ddl-auto set to create-drop, each time I started my app, the database restarted, which was quite useful for development.

You can even change the logging and how it prints messages, which I am going to explain below.

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/whoyou
    username: sotiris
    password: postgres
  jpa:
    hibernate:
      ddl-auto: create-drop
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format_sql: true
        show_sql: true
logging:
  level:
    root: INFO
    org.springframework.web: DEBUG
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
Enter fullscreen mode Exit fullscreen mode

Logging

Spring Boot logging is just amazing; it doesn't only show all the SQL queries that are being run, but it also has good formatting and shows all the important information about your app without being cumbersome or bloated.

You can easily log to the console in two different ways:

LoggerFactory.getLogger(AuthService::class.java).info("jwtToken: $jwtToken")
println("jwtToken: $jwtToken")
Enter fullscreen mode Exit fullscreen mode

Low Level

Unlike Laravel or Django, implementing authentication in Spring Boot is a bit more low level. That means you have more control over what you are doing, but you need to write more code.

You basically have a boilerplate, and you need to implement Filters, which are essentially something like Middleware. You need to create the whole JWT Service by yourself, create the SecurityConfiguration by yourself, and update the Application configuration.

image by Himaanshu Shukla

Here is a list of all the files I needed to create in order to implement the Authentication:

  • AuthController.kt

    This is basically where I handle my requests and call AuthService

  • AuthenticationResponse.kt

    This is what AuthService Login and Register returns the token and refresh_token

  • AuthService.kt

    All the logic for the authentication process.

  • JwtAuthFilter.kt

    Just Like a middleware in Laravel it runs before the request to check for the Bearer token etc.

  • JwtService.kt

    All the logic for creating, validating and extracting data from the token

  • LoginRequest.kt

  • RegisterRequest.kt

  • RefreshTokenRequest.kt

    All these are data classes just like an interface in PHP on what should the request have

  • SecurityConfig.kt

  • ApplicationConfig.kt

    Just some configuration to override default processes and decide what routs should be private and public

Comparing to Laravel

Well, in Laravel, you just install Sanctum and use HashApiToken on the User Model, and create your Login and Register Routes, which are quite minimal, and boom, you are ready. But don't forget that PHP is the king of the web in terms of speed.

Final Thoughts

In conclusion, transitioning from Laravel to Kotlin with Spring Boot presents a steep learning curve, particularly in areas like configuration and authentication. Maven and Spring Boot offer robust, albeit more complex, solutions compared to Laravel's more straightforward tools like Sanctum.

The flexibility and control provided by Kotlin and Spring Boot are significant, especially for enterprise-level applications, but they come at the cost of increased complexity in setup and management.

Thanks for reading, and I hope you found this article helpful. If you have any questions, feel free to email me at kourouklis@pm.me, and I will respond.

You can also keep up with my latest updates by checking out my X here: x.com/sotergreco

💖 💪 🙅 🚩
sotergreco
Sotiris Kourouklis

Posted on May 4, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related