1

I am trying to implement Firebase Email-Password Authentication using Kotlin in Web Front-End. But I think we can't do it using Kotlin as Firebase Authentication for Web only supports JS as per the examples I have seen.

So, my first question is: Can we implement it using Kotlin?

If not then, how can we call JS function from Kotlin within a Kotlin file and vice-versa?

Thanks in advance.

Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59

1 Answers1

0

JavaScript and Kotlin can easily interoperate. Here I'll provide some excerpts from official documentation as well as links to that documentation.

Calling Kotlin code from JS

To prevent spoiling the global object, Kotlin creates an object that contains all Kotlin declarations from the current module. So if you name your module as myModule, all declarations are available to JavaScript via myModule object. For example:

fun foo() = "Hello" Can be called from JavaScript like this: alert(myModule.foo());

Calling JS code from Kotlin

To tell Kotlin that a certain declaration is written in pure JavaScript, you should mark it with external modifier. When the compiler sees such a declaration, it assumes that the implementation for the corresponding class, function or property is provided by the developer.

I will add here that external function can be provided not necessarily by developer themselves - it can be something that already exist in this environment - like browser API.

Community
  • 1
  • 1
shabunc
  • 23,119
  • 19
  • 77
  • 102