Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NV-3043] 🚀 Feature: react-native support for Headless JavaScript Service #4499

Closed
1 of 4 tasks
tarcon opened this issue Oct 12, 2023 · 8 comments
Closed
1 of 4 tasks

Comments

@tarcon
Copy link

tarcon commented Oct 12, 2023

I'm submitting a...

  • bug report
  • feature request
  • question about the decisions made in the repository
  • question about how to use this project

Summary
The HeadlessService uses localStorage which isn't provided in react-native clients. It uses the localStorage to store the session token.

I'd like to be able to use the HeadlessService in a react-native client and need to store the token somewhere else.

Other information
The token storage seems to be the only issue that's preventing the HeadlessService from working in react-native.

To solve this, I'd suggest to provide a custom tokenStorage option in the HeadlessService constructor, so that a client can provide a react-native compatible storage.

I have a working implementation for it:
next...holi-social:novu:next

I'd gladly open a PR for that. Please assign me to this issue.

NV-3043

@tarcon tarcon changed the title react-native support for Headless JavaScript Service 🚀 Feature: react-native support for Headless JavaScript Service Oct 12, 2023
@p-fernandez p-fernandez changed the title 🚀 Feature: react-native support for Headless JavaScript Service [NV-3043] 🚀 Feature: react-native support for Headless JavaScript Service Oct 19, 2023
@p-fernandez
Copy link
Contributor

This sounds fantastic. How do you plan to solve the localStorage problem? What alternative would you use for a react native compatible solution?

@tarcon
Copy link
Author

tarcon commented Oct 20, 2023

react-native-mmkv is a synchronous store which seems compatible with the browsers local-storage interface (it doesn't work in Expo Go unfortunately)

To prove my concept, this simple variable store enables the HeadlessService to work both in react native and a Next.js app:

let token: string | null = null

export const novuTokenStorage = {
  getItem: () => token,
  removeItem: () => {
    token = null
  },
  setItem: (_: string, value: string) => {
    token = value
  },
}

new HeadlessService({
      ...
      tokenStorage: novuTokenStorage,
})
    

We could discuss the overall need to persist the token instead of just holding it in memory.

A reason to use persisted tokens is to reduce load on the backend. But the headless package currently leaves session initialization to the client and doesn't encourage its economical use.

So we might as well replace localstorage use in the HeadlessService in general to keep things simple and compatible with react-native. Maybe i am over-pragmatic about it. Let me know if I miss a good reason for persisting the token.

@p-fernandez
Copy link
Contributor

Mainly the main reason for that is to persist the Headless authentication across refreshes or pages closed without having to initialise the session. Also it avoids CSRF (https://owasp.org/www-community/attacks/csrf) but vulnerable to XSS attacks.
I understand the choice we made was based on a quick developer experience plus the minimal amount of security possible combined.

@tarcon
Copy link
Author

tarcon commented Nov 24, 2023

I won't work on this any further. I chose to use the base ApiService from the novu/client package for now as the HeadlessService was not working as I expected. It lacked a way for the client to reload notifications without having to reinitialize the client.

@tarcon tarcon closed this as completed Nov 24, 2023
@tarcon tarcon removed their assignment Nov 24, 2023
@jainpawan21 jainpawan21 reopened this Dec 13, 2023
@UncleSamtoshi
Copy link

@tarcon What was the problem you were encountering with just reinitializing the client each time?

@UncleSamtoshi
Copy link

UncleSamtoshi commented Dec 21, 2023

@tarcon What was the problem you were encountering with just reinitializing the client each time?

You can ignore this as I misread your earlier message and missed that you were having problems with reloading notifications.

@eduardo-santos-tribia
Copy link

eduardo-santos-tribia commented Jun 20, 2024

I won't work on this any further. I chose to use the base ApiService from the novu/client package for now as the HeadlessService was not working as I expected. It lacked a way for the client to reload notifications without having to reinitialize the client.

@tarcon could you provide your solution or give a more clear idea of how to solve it? What is the link with proper documentation to this "base ApiService"?

@scopsy
Copy link
Contributor

scopsy commented Oct 29, 2024

Recently published a dedicated react native package: https://docs.novu.co/inbox/react-native/quickstart

@scopsy scopsy closed this as completed Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants