Creating an iPhone + Rails + Application Authentication Configuration

I currently have a rails application using Devise and I can log in to my browser. Now I want to create my own iPhone application that uses this rails application as a backend. What I would like:

  • When a user first opens the iOS application, they are presented with a login screen.
  • They put them in the information, and the application tries to register them. If successful, it lists the users of the rails application (for now). Otherwise, it indicates an incorrect login.
  • The user can then click on usernames and display user profiles, etc. The application should be able to perform all CRUD operations as if they were on the site itself.

I am currently using Devise and RestKit for an iPhone application. I have enabled http_auth in my rails application and I can set the username / password and return a response from my / users page (list of my users in JSON).

Rails:

class UsersController < ApplicationController before_filter :authenticate_user! def index @users = User.all respond_to do |format| format.html # index.html.erb format.json { render json: @users } end end end 

iPhone (using Restkit):

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. RKClient *client = [RKClient clientWithBaseURL:@"http://192.168.1.105:3000" username:@"xxxx" password:@"xxxx"]; [[RKClient sharedClient] get:@"/users.json" delegate:self]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)request:(RKRequest*)request didFailLoadWithError:(NSError *)error { NSLog(@"error"); } - (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response { NSLog(response.bodyAsString); } 

I am not sure what to do next or how to approach it.

  • How do I handle the login screen? Ok, I can do an 'http: // localhost: 3000' GET using http basic auth and get the users list back, but this is how I should check if the user credentials were entered correctly? Is there a way to get an OK or NOT OK response to login?
  • What is the best way to interact with the rails app? Is it possible to do things like GET / users / 2 using RestKit? Or do I need to build some kind of api? I don’t know where to start with something like this.
+4
source share
1 answer

Why are you using basic http auth if you have one? Using the method, you can return auth errors to the json hash code and display it on the login screen.

Create an application in the classic REST style, and it will be quite comfortable for the web client and mobile client.

You can also use something like Rabl https://github.com/nesquena/rabl to create smart JSON APIs in your Rails application.

0
source

Source: https://habr.com/ru/post/1438296/


All Articles