# Appstitch

Appstitch Core is required for all integrations. It is used to initialize an app client.

## Install

{% tabs %}
{% tab title="Web / React Native " %}

```bash
npm install @appstitch/core
```

{% endtab %}

{% tab title="Flutter" %}

```yaml
appstitch_core: ^2.0.0
```

{% endtab %}
{% endtabs %}

Install Peer Dependency Appstitch Types

{% tabs %}
{% tab title="Web / React Native" %}

```
npm install @appstitch/types
```

{% endtab %}

{% tab title="Flutter" %}

```
N/A
```

{% endtab %}
{% endtabs %}

## Usage

Initialize Appstitch in your root file

{% tabs %}
{% tab title="Web / React Native" %}

```typescript
import appstitch from "@appstitch/core";

appstitch.initialize({ 
    appStitchKey:"your_stitch_key", 
    clientID : "your_client_id"
    url : "custom.domain.com" // Only required for paid plans
});
```

{% endtab %}

{% tab title="Flutter" %}

```dart
import 'package:appstitch_core/options.dart';
import 'package:appstitch_core/core.dart';


final core = Core();
final config = 
        Options(appStitchKey: "your_stitch_key", clientID: "your_client_id");

core.initialize(config);

```

{% endtab %}
{% endtabs %}

## Authentication

User authentication protects your integrations from abuse. Once a user has logged in and recieves a token, the token be be set like this&#x20;

{% tabs %}
{% tab title="Web / React Native" %}

```typescript
appstitch.setAuthToken("abc123");
```

{% endtab %}

{% tab title="Flutter" %}

```dart
core.setAuthToken("abc123");
```

{% endtab %}
{% endtabs %}

Tokens can be created in two ways&#x20;

### Appstitch Authenthication

The Appstitch Auth library can be used for passwordless user management. Passwordless authentication reduces the friction for users to sign up and ensures only users with valid emails can use your app!

### Custom Authentication

A token/GUID can be created with [Firebase Auth](https://firebase.google.com/docs/auth) or youw own user management flow. You need to create callback to verify a valid token. Your authentication callback can be set on the Appstitch [dashboard](https://connect.appstitch.dev/settings)

#### Sample Callback

{% tabs %}
{% tab title="NodeJS/Express" %}

```javascript
app.post("/authCallback", async (req, res) => {
    const { body } = req; 

    
    const isValid = await verifyToken(body.token)

    res.status(200).send({ valid : isValid})
        
})
```

{% endtab %}

{% tab title="Firebase Functions" %}

```javascript
exports.authCallback = functions.https.onRequest(async (req, res) => {

 const { body } = req; 

    
 const isValid = await admin
  .auth()
  .verifyIdToken(body.token)
  .then((decodedToken) => {
      const uid = decodedToken.uid;
      return uid != null 
  })
  .catch((error) => {
    // Handle error
    return false
  });
  
   res.status(200).send({ valid : isValid})
})
```

{% endtab %}
{% endtabs %}
