I have seen lots of questions and answers on how to permanently store a cookie in an iOS web application... But I can't seem to even get this one set.
The application runs beautifully in a real desktop browser, but fails when running inside of an iOS app.
I have a rails web service UI that the app communicates, but all of the app logic and code is on the client using Backbone, jQuery, etc...
A form submits the user login information to the web service via an AJAX POST:
$.ajax({url: 'http://mydomain.fake/login',
type: 'POST',
data: data,
xhrFields: { withCredentials: true },
success: successFunc,
error: errorFunc});
The server then authenticates the user and FOR NOW sends back an encrypted permanent cookie with the user's identity:
def login
@account = Account.find_by(email: params[:email]).try(:authenticate, params[:password])
unless @account.nil? || @account == false
cookies.permanent.signed[:user] = @account
return head :ok
end
head :unauthorized
end
Connecting to the app with safari and checking the response to this call I get back the appropriate cookie header:
Set-Cookie user=<encrypted string>; path=/; expires=Fri, 29 Sep 2034 01:50:31 -0000
Now when checking document.cookie
there is nothing there. Looking on in the resources on the left of Safari, there are no cookies. And when refreshing, the same symptoms as above, and the application has no idea who the user is and returns them back to the login page...
Does anyone have any idea why the application cannot store the cookie. I am not closing and re-opening the application, so this is not an issue with the known (safari deletes the cookies on close) or (applications are sandboxed) or (an expiration time on the cookie has not been set). I am thoroughly confused.
UPDATE:
I added the CORS headers to the web server, though they seem not to be needed, but no difference was achieved. I'm still at a loss for how something that usually just-works isn't, and would love any help/pointers/links/anything that might help get me back on track. I am currently stuck.
UPDATE:
I switched from using jQuery to a phonegap plugin that uses native Obj-C to make a request to the server. The request succeeded, but still no cookie is set. I am convinced I am doing something very wrong here.
UPDATE:
I now have tried using jQuery cookie to set an encrypted string sent by the server and setting it that way. It works, but only if I don't set the domain on the cookie.
$.cookie('user', data.token, {
expires: new Date(2014,12,31),
domain: 'localhost',
path: '/'});
I'm sure that this is a CORS security feature where you cannot set another domain other than the domain you are working on. But my test server is at localhost:3000
and my test app is running from localhost:63342
so I was setting the cookie domain to localhost
. This wouldn't pass my desktop browser testing so I never did try it in iOS since I can't even get it to work here.
So I guess to modify my question How should I authenticate my users to the webservice and store something (like a token) so that I can identify users on subsequent visits...
Update:
I do have these headers set in my web service. I set the Allow-Origin
to another domain I own because I can't list out every origin of every user. Unless should it be set to localhost
or something for them?
'Access-Control-Allow-Origin' => '<what should I put here>',
'Access-Control-Request-Method' => '*',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE, OPTIONS, HEAD',
'Access-Control-Allow-Headers' => 'Origin, Accept, Content-Type, Cookie'
Either way the webview produces no CORS errors, unlike actual safari or a desktop browser (even if I don't have them set at all).