I am creating an application where to login, I open a web page, with a login form.
After entering the username and password, if correct, I must return a token which I will then use to connect to the API.
This is a procedure that I have never done, because I recently started working with android studio and kotlin.
So I would like to ask you for suggestions for how to implement this.
Basically I would like to recall the web page from the application, and wait for it to return a token to me.
If I’m being honest, I personally would consider a different approach which avoids the problem all together. Unless it is important that the user lands specifically at the HTML / JS version of the login page, I would personally roll my own login Fragment / Activity and use the well-established networking abstraction libraries to handle the fetching and responses.
A common combination of libraries that work together to make this all easy are: OkHttp as the base networking abstraction, Retrofit for easily managing network requests to endpoints, and GSON (if the API is JSON-based). It sounds like your app will need that token to perform more networking requests to the API once authenticated, so you’ll probably need all those libraries anyway. If all that is in place, it would be pretty simple to add the login POST endpoint for whatever site you’re trying to authenticate with to your app.
Trying to “view a web page and handle a response from that site within the app” is going to be much more difficult to implement.
EDIT formatting and grammar
thanks for answering me.
I apologize for my english and for my few knowledge of kotlin / java, I am at the beginning.
I’ve seen what OkHttp does but it’s not what I need, and unfortunately for now there are no APIs.
Currently I have to take a responsive web page which is also used by other IOS, web and desktop applications.
The web page has a login form which then does not immediately return a token but shows selections, after making choices a toket is provided.
In practice with the same user you can access different accounts, which are chosen through the select.
So I have to show the current HTML, the user makes his choice and I take the token that is given in the redirect query string.
The solution I had requested is the one used at present by an IOS application which was not developed by me, and therefore I don’t know how it opens the browser and then returns to the app.
Yesterday I started to test the webview, to understand if all this is possible or if I can find security problems or bugs.
Oh okay, if there are no public APIs and you need that dropdown, then you are correct that my solution will not work. Using
WebView is the correct way to go from here, so you were on the right track.
I did a quick Google search and found this StackOverflow answer which might be what you are looking for: https://stackoverflow.com/a/55873797/11564711. Ignore the “First One” part of that answer and focus on the second one. It has a good example of some Kotlin and Java code which is almost what you want.
In short, create a
WebView which the user will login with, add a
WebViewClient to the
WebView which intercepts page load events. You can use those events to get the token after a successful login.