---
title: Register and Authenticate Profiles
---


User Profiles are core to how Sahha works. Each User Profile is associated with a health analysis and corresponding sensor, and health data. This guide will teach you how to create, authenticate and manage user profiles.

---

## Authenticating User Profiles

The Sahha SDK must be authenticated in order to connect to the Sahha API. Do this once per user profile. Once a profile is authenticated, the SDK will take care of automatically issuing and refreshing API tokens.

**But before we start authenticating User Profiles you need to know how to use the `ExternalID` field**

### Using the External ID

You will need to provide your own unique External ID to authenticate a user profile. An External ID can be any string you choose to identify a user profile within your organization. This ID must be unique for each of your users. This ID has a limit of 100 characters.

We suggest using an anonymous UUID e.g. `123e4567-e89b-12d3-a456-426614174000`

#### If your user changes devices, make sure to use the same External ID to identify them on the new device.

{% callout title="User Privacy Warning" %}
Sahha does not collect personally identifiable information from users to safeguard user data privacy and security.

**DO NOT** use an `ExternalID` that could be used to personally identify a user.

For example, do not use emails or usernames for an `ExternalID`:

- Email (Samantha.Jones@website.com)
- Username (TimmyT_123)

{% /callout %}

Now that you know how to use the ExternalID you can start authenticating User Profiles.

---

## Authenticate Profiles via SDK

The fastest way to authenticate a User Profile is via the Sahha SDK. You will need your `appID` and `appSecret` to authenticate user profiles with an External ID which you can get from your [Sahha Dashboard](https://app.sahha.ai) under the API keys tab.

{% tabs %}

{% tab label="iOS" %}

```swift title=MyApp.swift
Sahha.authenticate(appId: "APP_ID", appSecret: "APP_SECRET", externalId: "EXTERNAL_ID") { error, success in
	if let error = error {
   	print(error)
	} else if success {
 		print("You are now authenticated")
 	}
}
```

{% /tab %}

{% tab label="Android" %}

```kotlin title=MainActivity.kt
Sahha.authenticate(appId: "APP_ID", appSecret:  "APP_SECRET", externalId: "EXTERNAL_ID") { error, success ->
    if (success) greeting = "Successful"
    else greeting = error ?: "Failed"
}
```

{% /tab %}

{% tab label="Flutter" %}

```dart title=MyApp.dart
SahhaFlutter.authenticate(appId: "APP_ID", appSecret: "APP_SECRET", externalId: "EXTERNAL_ID")
  .then((success) => {
    debugPrint(success.toString())
  })
  .catchError((error, stackTrace) => {
    debugPrint(error.toString())
  });
```

{% /tab %}

{% tab label="React Native" %}

```typescript title=MyApp.tsx
Sahha.authenticate('APP_ID', 'APP_SECRET', 'EXTERNAL_ID', (error: string, success: boolean) => {
	console.log(`Success: ${success}`);
	if (error) {
		console.error(`Error: ${error}`);
	}
});
```

{% /tab %}

{% tab label="Ionic Capacitor" %}

```javascript title=MyApp.js
Sahha.authenticate({'APP_ID', 'APP_SECRET', 'EXTERNAL_ID'}).then(
  function(response) {
    console.log(response.success);
  },
  function(error) {
    console.log(error);
  }
)
```

{% /tab %}

{% /tabs %}

{% callout title="Finding your App ID and App Secret" %}

Your `appId` and `appSecret` are available in the Sahha dashboard.

[Login to the Sahha Dashboard](https://app.sahha.ai)

These values are separate from your `clientId` and `clientSecret` and should only be used to authenticate a profile via the SDK.

**DO NOT** store your app ID and App Secret in your app code. Your account could be harmed if any 3rd party gains access to these two values.

We recommend storing and accessing these values from your server on app launch.

{% /callout %}

## Authenticate Profiles via API

You can also authenticate a user profile via the API and then pass the Profile Token to the SDK.

View the API docs: [API - Authenticate User Profile](https://sandbox-api.sahha.ai/api-docs/index.html#tag/2.-Profile-Authentication)

#### Step 1) Use your `accountToken` and `externalId` to authenticate a user profile via the `profile/register` endpoint.

```json title="REQUEST"
// POST "/oauth/profile/register"
// AUTHORIZATION HEADER "Account MY_ACCOUNT_TOKEN"

// BODY
{
	"externalId": "MY_EXTERNAL_ID"
}
```

You will receive a `profileToken` and `refreshToken` in the API response.

```json title="RESPONSE"
// POST "/oauth/profile/register"
// AUTHORIZATION HEADER "Account MY_ACCOUNT_TOKEN"

// BODY
{
	"profileToken": "PROFILE_TOKEN",
	"expiresIn": "86400",
	"tokenType": "Profile",
	"refreshToken": "REFRESH_TOKEN"
}
```

#### Step 2) Pass the `profileToken` and `refreshToken` you generated via the API to the SDK.

{% tabs %}

{% tab label="iOS" %}

```swift title=MyApp.swift
Sahha.authenticate(profileToken: "PROFILE_TOKEN", refreshToken: "REFRESH_TOKEN") { error, success in
	if let error = error {
   	print(error)
	} else if success {
 		print("You are now authenticated")
 	}
}
```

{% /tab %}

{% tab label="Android" %}

```kotlin title=MainActivity.kt
Sahha.authenticate(profileToken: "PROFILE_TOKEN", refreshToken:  "REFRESH_TOKEN") { error, success ->
    if (success) greeting = "Successful"
    else greeting = error ?: "Failed"
}
```

{% /tab %}

{% tab label="Flutter" %}

```dart title=MyApp.dart
SahhaFlutter.authenticateToken(profileToken: "PROFILE_TOKEN", refreshToken: "REFRESH_TOKEN")
  .then((success) => {
    debugPrint(success.toString())
  })
  .catchError((error, stackTrace) => {
    debugPrint(error.toString())
  });
```

{% /tab %}

{% tab label="React Native" %}

```typescript title=MyApp.tsx
Sahha.authenticateToken('PROFILE_TOKEN', 'REFRESH_TOKEN', (error: string, success: boolean) => {
	console.log(`Success: ${success}`);
	if (error) {
		console.error(`Error: ${error}`);
	}
});
```

{% /tab %}

{% tab label="Ionic Capacitor" %}

```javascript title=MyApp.js
Sahha.authenticateToken({'PROFILE_TOKEN', 'REFRESH_TOKEN'}).then(
  function(response) {
    console.log(response.success);
  },
  function(error) {
    console.log(error);
  }
)
```

{% /tab %}

{% /tabs %}

---

## Deauthenticate

If you would like to change authenticated users, first deauthenticate the current user before authenticating a new user.

The SDK will take care of switching user data and automatically issuing and refreshing API tokens.

{% tabs %}

{% tab label="iOS" %}

```swift title=MyApp.swift
Sahha.deauthenticate { error, success in
	if let error = error {
   	print(error)
	} else if success {
 		print("You are now deauthenticated")
 	}
}
```

{% /tab %}

{% tab label="Android" %}

```kotlin title=MainActivity.kt
Sahha.deauthenticate { error, success ->
    if (success) farewell = "Successful"
    else farewell = error ?: "Failed"
}
```

{% /tab %}

{% tab label="Flutter" %}

```dart title=MyApp.dart
SahhaFlutter.deauthenticate()
  .then((success) => {
    debugPrint(success.toString())
  })
  .catchError((error, stackTrace) => {
    debugPrint(error.toString())
  });
```

{% /tab %}

{% tab label="React Native" %}

```typescript title=MyApp.tsx
Sahha.deauthenticate((error: string, success: boolean) => {
	console.log(`Success: ${success}`);
	if (error) {
		console.error(`Error: ${error}`);
	}
});
```

{% /tab %}

{% tab label="Ionic Capacitor" %}

```javascript title=MyApp.js
Sahha.deauthenticate().then(
	function (response) {
		console.log(response.success);
	},
	function (error) {
		console.log(error);
	}
);
```

{% /tab %}

{% /tabs %}

---

## Check Authentication

You can easily check if a profile is already authenticated via the SDK.

{% tabs %}

{% tab label="iOS" %}

```swift title=MyApp.swift
if Sahha.isAuthenticated {
  	print("Profile is ready")
} else {
 	print("You must authenticate your profile")
}
```

{% /tab %}

{% tab label="Android" %}

```kotlin title=MainActivity.kt
if Sahha.isAuthenticated {
  	print("Profile is ready")
} else {
 	print("You must authenticate your profile")
}
```

{% /tab %}

{% tab label="Flutter" %}

```dart title=MyApp.dart
SahhaFlutter.isAuthenticated()
  .then((success) => {
    debugPrint(success.toString())
  })
  .catchError((error, stackTrace) => {
    debugPrint(error.toString())
  });
```

{% /tab %}

{% tab label="React Native" %}

```typescript title=MyApp.tsx
Sahha.isAuthenticated((error: string, success: boolean) => {
	console.log(`Success: ${success}`);
	if (error) {
		console.error(`Error: ${error}`);
	}
});
```

{% /tab %}

{% tab label="Ionic Capacitor" %}

```javascript title=MyApp.js
Sahha.isAuthenticated().then(
	function (response) {
		console.log(response.success);
	},
	function (error) {
		console.log(error);
	}
);
```

{% /tab %}

{% /tabs %}

---

## Get Profile Token

You can get the currently authenticated user's profile token.

{% tabs %}

{% tab label="iOS" %}

```swift title=MyApp.swift
if let token = Sahha.profileToken {
	print("User has a token")
} else {
 	print("User does not have a token")
}
```

{% /tab %}

{% tab label="Android" %}

```kotlin title=MainActivity.kt
val token = Sahha.profileToken;
if (token != null) {
    print("Profile is ready")
} else {
    print("You must authenticate your profile")
}
```

{% /tab %}

{% tab label="Flutter" %}

```dart title=MyApp.dart
SahhaFlutter.getProfileToken().then((value) {
    if (value != null) {
    	debugPrint(value);
    } else {
       debugPrint("Token is null");
    }
})
.catchError((error, stackTrace) => {
    debugPrint(error.toString())
});
```

{% /tab %}

{% tab label="React Native" %}

```typescript title=MyApp.tsx
Sahha.getProfileToken((error: string, token?: string) => {
	if (error) {
		console.error(`Error: ${error}`);
	} else if (token) {
		console.log(`Profile Token: ${profileToken}`);
		setProfileToken(token);
	} else {
		console.log(`Profile Token: null`);
	}
});
```

{% /tab %}

{% tab label="Ionic Capacitor" %}

```javascript title=MyApp.js
Sahha.getProfileToken().then(
	function (response) {
		console.log(response.value);
	},
	function (error) {
		console.log(error);
	}
);
```

{% /tab %}

{% /tabs %}
