目標:

  • 可以登入

iOS guide 03 - 图1

iOS guide 03 - 图2

iOS guide 03 - 图3

步驟:

Step 1 :

修改 Gemfile

加入

  1. gem "motion-authentication"
  2. gem "ProMotion-XLForm"
  • bundle install
  • rake pod:install

Step 2 :

修改 app/screens/home_screen.rb

修改 on_road

  1. def on_load
  2. if Auth.signed_in?
  3. set_nav_bar_button :right, title: "Logout", action: :sign_out_button
  4. else
  5. set_nav_bar_button :right, title: "Sign In", action: :sign_in_button
  6. end
  7. @jobs = []
  8. load_jobs
  9. end

加入

  1. def sign_out_button
  2. Auth.sign_out do
  3. open_tab_bar HomeScreen.new(nav_bar: true)
  4. end
  5. end
  6. def sign_in_button
  7. open SignInScreen.new(nav_bar: true)
  8. end

Step 3 : 加入 SignInScreen

  • potion g screen sign_in
  1. class SignInScreen < PM::XLFormScreen
  2. title "Sign In"
  3. stylesheet SignInScreenStylesheet
  4. def form_data
  5. [
  6. {
  7. cells: [
  8. {
  9. title: "Email",
  10. name: :email,
  11. type: :email,
  12. placeholder: "Enter your email",
  13. required: true
  14. },
  15. {
  16. title: "Password",
  17. name: :password,
  18. type: :password,
  19. placeholder: "Enter your password",
  20. required: true
  21. },
  22. {
  23. title: "Sign In",
  24. name: :save,
  25. type: :button,
  26. on_click: lambda do |_cell|
  27. authenticate
  28. end
  29. }
  30. ]
  31. }
  32. ]
  33. end
  34. def authenticate
  35. Auth.sign_in(email: values["email"], password: values["password"]) do |response|
  36. if response.success?
  37. ApiClient.update_authorization_header(Auth.authorization_header)
  38. app.delegate.open_authenticated_root
  39. elsif response.object
  40. app.alert response.object["error"]
  41. else
  42. app.alert "Sorry, there was an error. #{response.error.localizedDescription}"
  43. end
  44. end
  45. end
  46. def will_animate_rotate(_orientation, _duration)
  47. reapply_styles
  48. end
  49. end

Step 4 : 新增 auth

  • touch app/models/auth.rb
  1. class Auth < Motion::Authentication
  2. strategy DeviseTokenAuth
  3. sign_in_url "http://localhost:3000/users/sign_in"
  4. end

Step 5 : compile

  • rake