0

I am a web developer and starting to learn android development.

I want to authenticate to a custom server which responds with a user object and a JWT (JSON web token) for further authentication.

I know that I need to use intents to show the login page etc.

In a web environment you have access to a session where you can store data in. But I can't find the same principle for an android application. The state of the current app. I need this because I want to store the user object and JWT for further requests and to gain access to it in other parts of the application without passing it as a parameter every time.

I guess that I just need to know the sibbling of a session in android to accomplish this.

Thanks in advance

Dirk
  • 3,095
  • 4
  • 19
  • 37

1 Answers1

0

As a non-web developer I might be wrong, but I see three solutions for this:

  1. use a set of constants. For example

    public class C { public final static boolean isAuthenticated = false; public final static String userName = "Joe Doe"; }

  2. use SharedPreferences (you can find a lot of examples around) to manually store data of your user. SharedPreferences is a handy way to store global data and if needed save it and load it back if the app is closed, to restore it. Here there is one example: Android: Keep username in session until logout

  3. use a library.

Android's ecosystem is very mature now and there are a lot of libraries that might do your case. For example the square libraries are really solid and I'm pretty sure that you can find exactly what you need: https://github.com/square/retrofit

and the lighter (and used inside retrofit) https://github.com/square/okhttp

Here there is a list of other free libraries that you can insert and that you might explore to find what best is for you on GitHub: https://github.com/search?q=http&ref=opensearch&l=Java&s=stars

And some other utilities that may come handy: https://android-arsenal.com/tag/204?sort=created&category=1

With a quick search on GitHub I also found this that explicity talks about sessions: https://github.com/softwaremill/akka-http-session .

Community
  • 1
  • 1
Beppi's
  • 2,089
  • 1
  • 21
  • 38