<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>corprewland &#187; authlogic</title>
	<atom:link href="http://www.corprew.org/blog/tag/authlogic/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.corprew.org</link>
	<description>(dis)information organization</description>
	<lastBuildDate>Fri, 19 Feb 2010 23:38:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Authlogic and the iPhone</title>
		<link>http://www.corprew.org/blog/2010/01/27/authlogic-and-objectiveresource/</link>
		<comments>http://www.corprew.org/blog/2010/01/27/authlogic-and-objectiveresource/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 00:45:45 +0000</pubDate>
		<dc:creator>corprew</dc:creator>
				<category><![CDATA[computer programming]]></category>
		<category><![CDATA[authlogic]]></category>
		<category><![CDATA[defensive programming]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[ObjectiveResource]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[UserSession]]></category>

		<guid isPermaLink="false">http://www.corprew.org/?p=235</guid>
		<description><![CDATA[So, I&#8217;ve based a couple of apps on a combination of authlogic and the iPhone Objective Resource package, and when I mentioned this on the relevant lists I got a bunch of people asking me questions, so I thought I&#8217;d post about what I did generally. There are a couple of steps, most of which [...]]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;ve based a couple of apps on a combination of <a href="http://github.com/binarylogic/authlogic">authlogic</a> and the iPhone <a href="http://iphoneonrails.com/">Objective Resource</a> package, and when I mentioned this on the relevant lists I got a bunch of people asking me questions, so I thought I&#8217;d post about what I did generally.</p>
<p>There are a couple of steps, most of which are geared towards having authlogic not produce redirects and instead produce http status codes that are interpretable to the applications.  Here&#8217;s an example of what I mean from my ApplicationController on an app</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> require_user
    <span style="color:#9966CC; font-weight:bold;">unless</span> current_user
      store_location
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;You must be logged in to access this page&quot;</span>
      respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> redirect_to new_user_session_url <span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;you must be logged in to access this page.&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unauthorized</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In the default implementation of this filter, it always returns a redirect.  ObjectiveResource can follow the redirect (if you have a recent enough version), but it doesn&#8217;t really do it a lot of good.  This way, you get a status code that you can use.</p>
<p>Since Authlogic focuses on RESTful creation of things, a lot of operations map naturally.  To create an account on the Rails app, create a User object, and to test whether the user can log in, create a user session.  My application stores credentials between iPhone App invocations, so it creates a UserSession at startup if you have stored credentials.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application <span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">// ... blah blah blah all sorts of stuff removed.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
	UserPrefsManager<span style="color: #002200;">*</span> prefs <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UserPrefsManager sharedInstance<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>prefs isLoggedIn<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		UserSession <span style="color: #002200;">*</span> us <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UserSession alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
		us.login <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>prefs username<span style="color: #002200;">&#93;</span>;
		us.password <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>prefs password<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span> err <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>us createRemoteWithResponse<span style="color: #002200;">:&amp;</span>err<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  ... my actual detection stuff removed and just the default left ...</span>
<span style="color: #11740a; font-style: italic;">//</span>
			<span style="color: #002200;">&#91;</span>AlertHelper showAlertWithError<span style="color: #002200;">:</span>err<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>prefs clearLogin<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#91;</span>ObjectiveResourceConfig setUser<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>prefs username<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>ObjectiveResourceConfig setPassword<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>prefs password<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">// ... and proceed on with your app's normal course of things ...</span>
<span style="color: #11740a; font-style: italic;">//	</span>
    <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>tabBarController.view<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>You see here that i&#8217;m creating an UserSession here and testing to see if it works and setting the ObjectiveResourceConfig properties if it does.  I have some logic where if there isn&#8217;t saved credentials you will get a dialog asking you to login or create an account (or whatever) later on, which is the point of <code>[prefs clearLogin];</code>.  This is matched by a change to the create method of the UserSessionController:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">      respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">format</span> <span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Login successful!&quot;</span>
          redirect_back_or_default account_url
        <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span> <span style="color:#9966CC; font-weight:bold;">do</span>
          render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@user_session</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:created</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>(For brevity, I&#8217;ve just shown the successful branch)</p>
<p>This should be enough to get people going &#8212; all the other changes you have to make can be figured out from these two statements, and when I have some spare time I&#8217;m going to create a version of the demo application that works with ObjectiveResource.  The only other suggestion I have is to use the withResponse: versions of everything and code defensively, and note that in some versions of ObjectiveResource getting an error from your application will cause the application to throw.  In the meantime, feel free to ask me any questions</p>
]]></content:encoded>
			<wfw:commentRss>http://www.corprew.org/blog/2010/01/27/authlogic-and-objectiveresource/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

