If you're not using Jumpstart Pro Rails, here are the steps needed to update your web application to work with Jumpstart Pro iOS & Android.
Your Rails API will be used for authentication and other features.
The host and URL base path are configurable in the iOS / Android app Endpoint configuration. By default, the host is http://localhost:3000
and base API uri is /api/v1
. You can change this configuration to match your application's API.
When making requests to the API, you'll want to follow these rules:
Android (Hotwire Native)
or iOS (Hotwire Native)
in the User Agent for all API requests. This tells the API the request came from Hotwire Native and should be handled accordingly.Authorization
header to authenticate user with an API tokenSome views will need a custom version for Hotwire Native. You can use Request Variants to accomplish this.
Hotwire includes a hotwire_native_app?
helper that checks the User-Agent
header for Hotwire Native. You can set the request variant for Hotwire Native requests using that.
before_action do
request.variant = :phone if hotwire_native_app?
end
The iOS / Android app uses a native header so it feels native. We add a hamburger button to this which triggers a Javascript event using the Hotwire Native Bridge.
In Rails, you'll want to hide your navbar on Hotwire Native requests. Use a request variant template for this so you can minimize the HTML used for mobile.
The menu should be hidden, but toggles when the toggle-nav-bar
event fires. You can create a simple Stimulus Controller to listen to that event and toggle the navbar visibility.
Bridge components are how your iOS / Android app and JavaScript talk to each other. Bridge components are Stimulus controllers that can be used to do things like add native submit buttons on mobile apps so they feel more integrated. Learn more in the Hotwire Native docs.
Authentication requires an API endpoint that accepts email
and password
params.
It should:
POST /api/v1/auth
Successful authentication will refresh the currently visible page.
Sign out must happen on the iOS / Android app side. That way we can make an API request to remove the NotificationToken and clear cookies in the WebView.
To intercept the Sign out link in the HTML, use the following Bridge component:
import { BridgeComponent } from "@hotwired/hotwire-native-bridge"
export default class extends BridgeComponent {
static component = "sign-out"
static values = {
path: String
}
signOut(event) {
event.preventDefault()
event.stopImmediatePropagation()
const path = this.pathValue
this.send("signOut", {path}, () => {})
}
}
The API endpoint for Sign out needs to do two things:
DELETE /api/v1/auth
Changing your password is a simple API request
PATCH /api/v1/password
Headers:
Cookie
- The user's session cookieParams:
user[current_password]
- The user's current passworduser[password]
- The new passworduser[password_confirmation]
- The new passwordReturns:
Success - 200 OK
{ success: true }
Failure - 422 Unprocessable Entity
{ error: "Password confirmation did not match" }
Registering for Push Notifications happens when the user is signed in.
The Backend needs NotificationToken model to store the token and platform for push notifications.
rails g model NotificationToken user:belongs_to platform token
An API endpoint for notification tokens should:
POST /api/v1/notification_tokens
Cookie
header to authenticate user with their session cookietoken
- device notification tokenplatform
- platform of device (iOS, fcm). "fcm" is Firebase Cloud Messaging which is used for Android.We also recommend adding class="hotwire-native"
to the html or body element in your layout if hotwire_native_app?
to implement styling for native apps. This is already implemented in Jumpstart Pro Rails and ready to use.