I am trying to develop a Rails app which will mostly communicate with Android app users. People should be able to sign up and sign in from within the app (they can only perform most requests when logged in).
I've looked through many tutorials on how to do the equivalent of "storing a session" on the app, but all of them recommend using the gem 'devise' and its :token_authenticable, which was deprecated for some time now.
I want serious advice on how to perform something equivalent. From my understanding, the client sends a request with params such as {email: "[email protected]", password: "pw12345"}, I check that they match an existing user and retrieve a string token, which this client will from now on send in every request via headers (such as {"my_app_user_email": "[email protected]", "my_app_user_token":"abcdef123456"}).
I've already set some methods with fake values, such as
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_token is a fixed string, and valid_user? compares with fixed values as well.
I guess my problem is knowing if this is the right approach, and if so, how can I make a User model that creates and validates tokens?
Extra bit of code
def verify_token # This already works by using default values in the Android app code
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) # returns true for default_user_token, for now
end