I am trying to develop a Rails application that will mainly communicate with users of Android applications. People should be able to register and log in to the application (they can only fulfill most of the requests when they log in).
I looked through a lot of tutorials on how to make the equivalent of "session storage" in an application, but they all recommend using gem 'devise'it :token_authenticable, which has been deprecated for some time.
I need some serious advice on how to accomplish something equivalent. In my opinion, the client sends a request with parameters, such as {email: "some@email.com", password: "pw12345"}, I check that they correspond to the existing user and extract the token that this client will now send in each request through headers (for example, {"my_app_user_email": "some@email.com", "my_app_user_token":"abcdef123456"}).
I have already installed some methods with fake values, for example
def login
if valid_user?(params[:email], params[:password])
render json: {user_token: default_user_token}
else
render json: {message: 'Couldn\'t login'}, status: 401
end
end
Where default_user_tokenis a fixed string, and is valid_user?also compared with fixed values.
I believe my problem is that this is the right approach, and if so, how can I create a model Userthat creates and validates tokens?
Extra bit of code
def verify_token
email = request.headers['HTTP_MY_APP_USER_EMAIL']
token = request.headers['HTTP_MY_APP_USER_TOKEN']
user = User.find_by_email(email)
user && user.valid_token?(token)
end