Skip to content

Commit 136578f

Browse files
chore: Update to use new signIn function (#6)
* chore: use only 1 permissions array Instead of requiredPermissions and optionalPermissions, we will only support a simple array of permissions. * chore: return additional fields from sign in * chore: clean up unused code * test: update test to pass
1 parent 4b4e815 commit 136578f

9 files changed

Lines changed: 77 additions & 91 deletions

File tree

README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
![Platform React Native SDK](./assets/github-reactNative-sdk-banner.png)
22

33
# YouVersion React Native SDK
4+
45
[![npm version](https://badge.fury.io/js/@youversion%2Freact-native-sdk.svg)](https://www.npmjs.com/package/@youversion/platform-sdk-reactnative)
56
[![CI Status](https://github.com/youversion/platform-sdk-reactnative/workflows/CI/badge.svg)](https://github.com/youversion/platform-sdk-reactnative/actions)
67
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
@@ -15,7 +16,6 @@ A lightweight React Native SDK for integrating YouVersion Platform features into
1516
- [Release Process](RELEASING.md)
1617
- [Changelog](CHANGELOG.md)
1718

18-
1919
> [!important]
2020
> The React Native SDK is currently **iOS-only**. Android support is under active development and not yet available for use.
2121
@@ -272,17 +272,8 @@ See `BibleReference` properties under the `BibleTextView` component section abov
272272

273273
Presents the YouVersion login flow to the user and resolves with the login result on completion.
274274

275-
```tsx
276-
import { YouVersionAPI } from '@youversion/platform-sdk-reactnative';
277-
278-
const result = await YouVersionAPI.Users.signIn(['bibles', 'highlights']);
279-
```
280-
281275
**Parameters:**
282-
An object with the following optional properties:
283-
284-
- `requiredPermissions?: SignInWithYouVersionPermission[]` - Array of permissions that must be granted by the user for successful login.
285-
- `optionalPermissions?: SignInWithYouVersionPermission[]` - Array of permissions that are requested but not required for login.
276+
An array of permissions you're requesting from the user when they sign in.
286277

287278
Enum values for `SignInWithYouVersionPermission`:
288279

@@ -293,13 +284,18 @@ Enum values for `SignInWithYouVersionPermission`:
293284
- `bibleActivity`
294285

295286
**Returns:**
296-
`Promise<YouVersionLoginResult>` - An object containing the following details:
297-
298-
| Property | Type | Description |
299-
| ------------- | ---------------------------------- | -------------------------------------------- |
300-
| `accessToken` | `string` | The access token for the authenticated user. |
301-
| `permissions` | `SignInWithYouVersionPermission[]` | The permissions granted by the user. |
302-
| `yvpUserId` | `string` | The YouVersion Platform user ID. |
287+
`Promise<SignInWithYouVersionResult>` - An object containing the following details:
288+
289+
| Property | Type | Description |
290+
| ---------------- | ---------------------------------- | -------------------------------------------- |
291+
| `accessToken` | `string` | Access token for the authenticated user. |
292+
| `permissions` | `SignInWithYouVersionPermission[]` | Permissions granted by the user. |
293+
| `yvpUserId` | `string` | YouVersion Platform user ID. |
294+
| `expiryDate` | `string` | Expiration date of the access token. |
295+
| `refreshToken` | `string` | Refresh token for renewing the access token. |
296+
| `name` | `string` | Name of the authenticated user. |
297+
| `profilePicture` | `string` | URL to the user's profile picture. |
298+
| `email` | `string` | Email address of the authenticated user. |
303299

304300
#### `signOut`
305301

example/src/screens/ProfileScreen.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,20 @@ export function ProfileScreen() {
4141

4242
async function handleSignIn() {
4343
try {
44-
await YouVersionAPI.Users.signIn({
45-
requiredPermissions: ["bibles"],
46-
optionalPermissions: ["highlights"],
47-
});
44+
const signInResult = await YouVersionAPI.Users.signIn(["bibles"]);
45+
console.log("Sign-in result:", signInResult);
46+
} catch (error) {
47+
Alert.alert("Error signing in");
48+
console.error("Error signing in:", error);
49+
return;
50+
}
4851

52+
try {
4953
const userInfo = await YouVersionAPI.Users.userInfo();
5054
setCurrentUser(userInfo);
5155
} catch (error) {
52-
Alert.alert("Error signing in");
53-
console.error("Error signing in:", error);
56+
Alert.alert("Error getting user info after sign-in");
57+
console.error("Error getting user info:", error);
5458
}
5559
}
5660

ios/RNYouVersionPlatformModule.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ public class RNYouVersionPlatformModule: Module {
1717
}
1818

1919
AsyncFunction("signIn") { (
20-
requiredPermissions: [String],
21-
optionalPermissions: [String],
20+
permissions: [String],
2221
promise: Promise
2322
) in
2423
YVPAuthAPI.signIn(
25-
requiredPermissions: requiredPermissions,
26-
optionalPermissions: optionalPermissions,
24+
permissions: permissions,
2725
promise: promise
2826
)
2927
}

ios/YVPAuthAPI.swift

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,27 @@ import YouVersionPlatform
33
import AuthenticationServices
44

55
struct YVPAuthAPI {
6-
static func signIn(requiredPermissions: [String], optionalPermissions: [String], promise: Promise) {
7-
let required: Set<SignInWithYouVersionPermission> = Set(
8-
requiredPermissions.compactMap(SignInWithYouVersionPermission.init(rawValue:))
9-
)
10-
let optional: Set<SignInWithYouVersionPermission> = Set(
11-
optionalPermissions.compactMap(SignInWithYouVersionPermission.init(rawValue:))
12-
)
6+
static func signIn(permissions: [String], promise: Promise) {
7+
let permissionsSet = Set<SignInWithYouVersionPermission>(
8+
permissions.compactMap(SignInWithYouVersionPermission.init(rawValue:))
9+
)
1310

1411
Task {
1512
do {
1613
let response = try await YouVersionAPI.Users.signIn(
17-
requiredPermissions: required,
18-
optionalPermissions: optional,
19-
contextProvider: ContextProvider()
14+
permissions: permissionsSet,
15+
contextProvider: ContextProvider()
2016
)
2117

22-
if let msg = response.errorMsg, !msg.isEmpty {
23-
promise.reject(YVPError.signInError(message: msg))
24-
return
25-
}
26-
2718
promise.resolve([
2819
"accessToken": response.accessToken,
2920
"permissions": response.permissions.map(\.rawValue),
30-
"yvpUserId": response.yvpUserId
21+
"yvpUserId": response.yvpUserId,
22+
"expiryDate": formatExpiryDate(response.expiryDate),
23+
"refreshToken": response.refreshToken,
24+
"name": response.name,
25+
"profilePicture": response.profilePicture,
26+
"email": response.email
3127
])
3228
} catch {
3329
promise.reject(error)
@@ -59,6 +55,16 @@ struct YVPAuthAPI {
5955
}
6056
}
6157

58+
private let isoFormatter: ISO8601DateFormatter = {
59+
let f = ISO8601DateFormatter()
60+
f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
61+
return f
62+
}()
63+
64+
func formatExpiryDate(_ expiryDate: Date?) -> String? {
65+
expiryDate.map { isoFormatter.string(from: $0) }
66+
}
67+
6268

6369
class ContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {
6470
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
@@ -69,7 +75,3 @@ class ContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding
6975
return window
7076
}
7177
}
72-
73-
enum YVPError : Error {
74-
case signInError(message: String)
75-
}

mocks/RNYouVersionPlatform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
HighlightResponse,
55
LanguageOverview,
66
SignInWithYouVersionPermission,
7-
YouVersionLoginResult,
7+
SignInWithYouVersionResult,
88
YouVersionUserInfo,
99
YouVersionVerseOfTheDay,
1010
} from "../src";
@@ -18,7 +18,7 @@ export function getAccessToken(): string | null {
1818
export function signIn(
1919
requiredPermissions: SignInWithYouVersionPermission[] = [],
2020
optionalPermissions: SignInWithYouVersionPermission[] = [],
21-
): Promise<YouVersionLoginResult> {
21+
): Promise<SignInWithYouVersionResult> {
2222
return Promise.resolve({
2323
accessToken: "mock-access-token",
2424
permissions: [...requiredPermissions, ...optionalPermissions],

src/__tests__/RNYouVersionPlatform/native-test.native.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,18 @@ describe("YouVersionPlatform", () => {
3131

3232
describe("YouVersionAPI.Users", () => {
3333
describe("#signIn", () => {
34-
it("passes the required and optional permissions", async () => {
35-
const result = await YouVersionAPI.Users.signIn({
36-
requiredPermissions: ["bible_activity"],
37-
optionalPermissions: ["demographics"],
38-
});
34+
it("passes the provided permissions", async () => {
35+
const result = await YouVersionAPI.Users.signIn(["bible_activity"]);
3936

40-
expect(Module.signIn).toHaveBeenCalledWith(
41-
["bible_activity"],
42-
["demographics"],
43-
);
37+
expect(Module.signIn).toHaveBeenCalledWith(["bible_activity"]);
4438

45-
expect(result).toEqual({
46-
accessToken: "mock-access-token",
47-
permissions: ["bible_activity", "demographics"],
48-
yvpUserId: "mock-yvp-user-id",
49-
});
39+
expect(result).toEqual(
40+
expect.objectContaining({
41+
accessToken: "mock-access-token",
42+
permissions: ["bible_activity"],
43+
yvpUserId: "mock-yvp-user-id",
44+
}),
45+
);
5046
});
5147
});
5248

src/api/users.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
11
import { Module } from "../native";
22
import {
33
SignInWithYouVersionPermission,
4-
YouVersionLoginResult,
4+
SignInWithYouVersionResult,
55
YouVersionUserInfo,
66
} from "../types";
77

8-
type SignInOptions = {
9-
/** Array of permissions that are required for a successful login */
10-
requiredPermissions?: SignInWithYouVersionPermission[];
11-
12-
/** Array of permissions that are optional for the user to select */
13-
optionalPermissions?: SignInWithYouVersionPermission[];
14-
};
15-
168
export const UsersAPI = {
179
/**
1810
* Presents the YouVersion login flow to the user and returns the login result upon completion.
1911
*
20-
* @param params - An object ({@link SignInOptions}) containing an array of permissions that are optional for your app and an array of permissions that are required for a successful login.
21-
* @returns A promise that resolves to the login result as a {@link YouVersionLoginResult} object.
12+
* @param permissions - An array of permissions to request during sign-in.
13+
* @returns A promise that resolves to the login result as a {@link SignInWithYouVersionResult} object.
2214
*/
23-
signIn: ({
24-
requiredPermissions: providedRequired,
25-
optionalPermissions: providedOptional,
26-
}: SignInOptions = {}): Promise<YouVersionLoginResult> => {
27-
const requiredPermissions = providedRequired || [];
28-
const optionalPermissions = providedOptional || [];
29-
30-
return Module.signIn(requiredPermissions, optionalPermissions);
15+
signIn: (
16+
permissions: SignInWithYouVersionPermission[] = [],
17+
): Promise<SignInWithYouVersionResult> => {
18+
return Module.signIn(permissions);
3119
},
3220

3321
/** Signs out the current user by clearing the access token from local storage */

src/native.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
BibleVersion,
66
HighlightResponse,
77
LanguageOverview,
8-
YouVersionLoginResult,
8+
SignInWithYouVersionResult,
99
YouVersionUserInfo,
1010
YouVersionVerseOfTheDay,
1111
} from "./types";
@@ -15,10 +15,7 @@ declare class RNYouVersionPlatformModule extends NativeModule {
1515

1616
setApiHost(apiHost: string): void;
1717

18-
signIn(
19-
requiredPermissions: string[],
20-
optionalPermissions: string[],
21-
): Promise<YouVersionLoginResult>;
18+
signIn(permissions: string[]): Promise<SignInWithYouVersionResult>;
2219

2320
signOut(): Promise<void>;
2421

src/types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { ColorValue } from "react-native";
22

3-
export type YouVersionLoginResult = {
4-
accessToken: string;
3+
export type SignInWithYouVersionResult = {
4+
accessToken?: string;
55
permissions: SignInWithYouVersionPermission[];
6-
yvpUserId: string;
6+
yvpUserId?: string;
7+
expiryDate?: string;
8+
refreshToken?: string;
9+
name?: string;
10+
profilePicture?: string;
11+
email?: string;
712
};
813

914
export type SignInWithYouVersionPermission =

0 commit comments

Comments
 (0)