motion-authentication aims to provide a simple, standardized authentication helper for common authentication strategies.
Currently, this library only supports iOS, but could easily support other platforms. Please submit an issue (or PR :D) for the platform you would like to support.
Need authorization? Use
Add this line to your application's
Gemfile, then run
rake pod:install to install the CocoaPod dependencies.
Start by subclassing
Motion::Authentication to create your own
Auth class. Specify your authentication strategy and your sign in URL:
class Auth < Motion::Authentication strategy DeviseTokenAuth sign_in_url "http://localhost:3000/api/v1/users/sign_in" end
DeviseTokenAuth- This authentication strategy takes
password, makes a POST request to the
sign_in_url, and expects the response to include
tokenkeys in the JSON response object.
Auth class, call
.sign_in and pass a hash of credentials:
Auth.sign_in(email: email, password: password) do |result| if result.success? # authentication successful! else app.alert "Invalid login?" end end
A successful sign in will securely store the current user's auth token.
You can check if an auth token has previously been stored by using
signed_in?. For example, in your App Delegate, you might want to open your sign in screen when the app is opened:
def on_load(options) if Auth.signed_in? open DashboardScreen else open SignInScreen end end
After signing in, and receiving the auth token, you will probably want to configure your API client to use your auth token in your API requests in an authorization header. Call
.authorization_header to return the header value specific to the strategy that you are using. Two common places would be upon sign in, and when your app is launched.
# app_delegate.rb def on_load(options) if Auth.signed_in? MyApiClient.update_auth_header(Auth.authorization_header) # ... end end # sign_in_screen.rb def on_load(options) Auth.sign_in(data) do |result| if result.success? MyApiClient.update_auth_header(Auth.authorization_header) # ... end end end
At some point, you're going to need to sign out. This method will clear the stored auth token, but also allows you to pass a block to be called after the token has been cleared.
Auth.sign_out do open HomeScreen end
motion-authentication provides a
current_user attribute. It has no effect on authentication, so you can do whatever you want with it.
Auth.sign_in(data) do |result| if result.success? Auth.current_user = User.new(result.object) end end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request