Passing User Object to next page

nativescriptcore

#1

Maybe I’m going about this wrong, but it seems like such an obvious thing, that I didn’t think it would be this hard to find information about.

So I am building an app that logs the user into a site/API once successful I navigate to a listView page which should load a list of items from the API, using a second call. On the backend we set a cookie with a session ID then on the second call the backend reads that cookie and handles any session stuff it needs to do, i.e. is the user logged in, if yes, return the list else return an error message.

The problem I have been having is after logging in and navigating to the list page the second call seems to be missing the cookies set by the first call.
I thought ok read the cookies in after the login is successful then on the second call write the cookies out, but I haven’t been able to figure out how to do this in NativeScript/TypeScript/Angular. I was following the Groceries tutorial substituting our API’s with the ones in the tutorial.

Any help for a complete nudie with TypeScript would be helpful.

Thanks,


#2

what are you using to store “cookies” ?
try to use
import { getString, setString } from “application-settings”;
setString(“cookie”, cookie);
getString(“cookie”)


#3

@bmartin
I would recommend you to make some changes in your backend such that, instead of responding with a set cookie header, it sends the variables to be set, as a JSON.

With that, you can store the response of that API, in application settings the way @ValeraF showed. or do

const appSettings = require("application-settings");
appSettings.setString("authorizationData", JSON.stringify(reponseFromYourAPI))`

and then when you make a API request to your server after logging in,
if you use accessTokens, then send the token in the authorization header,
otherwise send the authorizationData in the request body.

In your backend, make sure you read it accordingly and not using cookies.

to get back the data from appSettings do,
const authorizationData = appSettings.getString("authorizationData");

I would recommend using access tokens, and sending it in the authorization header.

If you still want to use cookies. Then you would want to try if it works when you set, withCredentials: true in the http request options. (not sure if that works in NativeScript)


#4

With NativeScript Angular, the correct implementation for authorizing and validating against an API is to do the following:

  1. Receive the Authorization Token (OAUTH token) from login request
  2. Store this token in consistent storage (local storage / app settings / sqlite / etc.)
  3. Leverage either HTTP Interceptors or manually apply the header token value before you make an API call through Http/HttpClient in Angular.

Your authorization header may look like this:

req.headers.append('Authorization', `Bearer ${this.authToken}`);

After this, a regular http call would append your correct token and validate against your API. Then you would handle the response like normal and pass it to your view.