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

feat(react-native-google-signin ): added to config-plugin #226

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/react-native-google-signin/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @generated by expo-module-scripts
module.exports = require('expo-module-scripts/eslintrc.base.js');
46 changes: 46 additions & 0 deletions packages/react-native-google-signin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# @config-plugins/react-native-google-signin

Config plugin to auto configure react-native-google-signin on prebuild

# API documentation

- [Documentation for the main branch](https://github.com/expo/expo/blob/main/docs/pages/versions/unversioned/sdk/@config-plugins/react-native-google-signin.md)
- [Documentation for the latest stable release](https://docs.expo.dev/versions/latest/sdk/@config-plugins/react-native-google-signin/)

# Installation in managed Expo projects

For [managed](https://docs.expo.dev/archive/managed-vs-bare/) Expo projects, please follow the installation instructions in the [API documentation for the latest stable release](#api-documentation). If you follow the link and there is no documentation available then this library is not yet usable within managed projects — it is likely to be included in an upcoming Expo SDK release.

# Installation in bare React Native projects

For bare React Native projects, you must ensure that you have [installed and configured the `expo` package](https://docs.expo.dev/bare/installing-expo-modules/) before continuing.

### Add the package to your npm dependencies

```
npm install @config-plugins/react-native-google-signin
```

### Example

```json
{
"expo": {
"plugins": [
[
"@config-plugins/react-native-google-signin",
{
"serverClientId": "...",
"iosClientId": "...",
"iosUrlScheme": "..."
}
]
]
}
}
```


# Contributing

Contributions are very welcome! Please refer to guidelines described in the [contributing guide]( https://github.com/expo/expo#contributing).
1 change: 1 addition & 0 deletions packages/react-native-google-signin/app.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./build/withGoogleSignin");
1 change: 1 addition & 0 deletions packages/react-native-google-signin/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('expo-module-scripts/jest-preset-plugin');
39 changes: 39 additions & 0 deletions packages/react-native-google-signin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@config-plugins/react-native-google-signin",
"version": "7.0.0",
"description": "Config plugin to auto configure react-native-google-signin on prebuild",
"main": "build/withGoogleSignin.js",
"types": "build/withGoogleSignin.d.ts",
"sideEffects": false,
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/expo/config-plugins.git",
"directory": "packages/react-native-google-signin"
},
"files": [
"build",
"app.plugin.js"
],
"scripts": {
"build": "expo-module build",
"clean": "expo-module clean",
"lint": "expo-module lint",
"test": "expo-module test",
"prepare": "expo-module prepare",
"prepublishOnly": "expo-module prepublishOnly",
"expo-module": "expo-module"
},
"keywords": [
"react-native-google-signin",
"react-native",
"expo"
],
"peerDependencies": {
"expo": "^50"
},
"devDependencies": {
"expo-module-scripts": "^3.4.1"
},
"upstreamPackage": "react-native-google-signin"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class MissingParamsException extends Error {
constructor(param: string) {
super(
`Missing param "${param}" on the "@config-plugins/react-native-google-signin" plugin`,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { MissingParamsException } from "../MissingParamsException";
import {
setupGoogleSigninAndroid,
withGoogleSigninAndroid,
} from "../withGoogleSigninAndroid";

type XML = { $: { name: string }; _: string };

function fakeExportedConfig(string?: XML[]): any {
return { android: {}, modResults: { resources: { string } } };
}

describe(withGoogleSigninAndroid, () => {
it("add serverClientId to strings.xml", () => {
const config = setupGoogleSigninAndroid(fakeExportedConfig(), {
serverClientId: "xxx",
});
expect(config).toEqual(
fakeExportedConfig([{ $: { name: "server_client_id" }, _: "xxx" }]),
);
});

it(`should throw error missing "serverClientId" param`, () => {
expect(() =>
setupGoogleSigninAndroid(fakeExportedConfig(), { serverClientId: "" }),
).toThrow(MissingParamsException);
});

it(`should not throw mission param error when ios only`, () => {
expect(() => setupGoogleSigninAndroid({ ios: {} } as any, {})).not.toThrow(
MissingParamsException,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { MissingParamsException } from "../MissingParamsException";
import {
setupGoogleSigninIos,
withGoogleSigninIos,
} from "../withGoogleSigninIos";

function fakeExportedConfig(plist?: Record<string, any>): any {
return { ios: { infoPlist: plist } };
}

describe(withGoogleSigninIos, () => {
it(`add "iosClientId" and "iosUrlScheme" to Info.plist`, () => {
const config = setupGoogleSigninIos(fakeExportedConfig(), {
iosClientId: "iosClientId",
iosUrlScheme: "iosUrlScheme",
});
expect(config).toEqual(
fakeExportedConfig({
GIDClientID: "iosClientId",
CFBundleURLTypes: [{ CFBundleURLSchemes: ["iosUrlScheme"] }],
}),
);
});

it(`should throw error missing "iosClientId" param`, () => {
expect(() =>
setupGoogleSigninIos(fakeExportedConfig(), { iosUrlScheme: "xx" }),
).toThrow(MissingParamsException);
});

it(`should throw error missing "iosUrlScheme" param`, () => {
expect(() =>
setupGoogleSigninIos(fakeExportedConfig(), { iosClientId: "xx" }),
).toThrow(MissingParamsException);
});

it(`should not throw mission param error when android only`, () => {
expect(() => setupGoogleSigninIos({ android: {} } as any, {})).not.toThrow(
MissingParamsException,
);
});
});
33 changes: 33 additions & 0 deletions packages/react-native-google-signin/src/withGoogleSignin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ConfigPlugin, createRunOncePlugin } from "expo/config-plugins";

import {
AndroidParams,
withGoogleSigninAndroid,
} from "./withGoogleSigninAndroid";
import { IosParams, withGoogleSigninIos } from "./withGoogleSigninIos";

const withGoogleSigninPlugin: ConfigPlugin<AndroidParams & IosParams> = (
config,
options,
) => {
config = withGoogleSigninAndroid(config, options);
config = withGoogleSigninIos(config, options);
return config;
};

const pkg = {
// Prevent this plugin from being run more than once.
// This pattern enables users to safely migrate off of this
// out-of-tree `@config-plugins/react-native-google-signin` to a future
// upstream plugin in `react-native-google-signin`
name: "react-native-google-signin",
// Indicates that this plugin is dangerously linked to a module,
// and might not work with the latest version of that module.
version: "UNVERSIONED",
};

export default createRunOncePlugin(
withGoogleSigninPlugin,
pkg.name,
pkg.version,
);
42 changes: 42 additions & 0 deletions packages/react-native-google-signin/src/withGoogleSigninAndroid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
ConfigPlugin,
ExportedConfigWithProps,
withStringsXml,
} from "expo/config-plugins";

import { MissingParamsException } from "./MissingParamsException";
import { IosParams } from "./withGoogleSigninIos";

export type AndroidParams = {
serverClientId?: string;
};

export function setupGoogleSigninAndroid(
config: ExportedConfigWithProps,
{ serverClientId }: AndroidParams,
) {
if (!config.android) {
return config;
}

if (!serverClientId) {
throw new MissingParamsException("serverClientId");
}

const string = config.modResults.resources.string ?? [];
string.push({
$: { name: "server_client_id" },
_: serverClientId,
});
config.modResults.resources.string = string;

return config;
}

export const withGoogleSigninAndroid: ConfigPlugin<
AndroidParams & IosParams
> = (config, params) => {
return withStringsXml(config, (config) =>
setupGoogleSigninAndroid(config, params),
);
};
49 changes: 49 additions & 0 deletions packages/react-native-google-signin/src/withGoogleSigninIos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
ConfigPlugin,
ExportedConfigWithProps,
InfoPlist,
withInfoPlist,
} from "expo/config-plugins";

import { MissingParamsException } from "./MissingParamsException";

export type IosParams = {
iosClientId?: string;
iosUrlScheme?: string;
};

export function setupGoogleSigninIos(
config: ExportedConfigWithProps<InfoPlist>,
{ iosClientId, iosUrlScheme }: IosParams,
) {
if (!config.ios) {
return config;
}
if (!iosClientId) {
throw new MissingParamsException("iosClientId");
}

if (!iosUrlScheme) {
throw new MissingParamsException("iosUrlScheme");
}

if (!config.ios?.infoPlist) config.ios.infoPlist = {};

config.ios.infoPlist["GIDClientID"] = iosClientId;
config.ios.infoPlist["CFBundleURLTypes"] =
config.ios.infoPlist["CFBundleURLTypes"] ?? [];
config.ios.infoPlist["CFBundleURLTypes"].push({
CFBundleURLSchemes: [iosUrlScheme],
});

return config;
}

export const withGoogleSigninIos: ConfigPlugin<IosParams> = (
config,
params,
) => {
return withInfoPlist(config, (config) =>
setupGoogleSigninIos(config, params),
);
};
9 changes: 9 additions & 0 deletions packages/react-native-google-signin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "expo-module-scripts/tsconfig.plugin",
"compilerOptions": {
"outDir": "build",
"rootDir": "src"
},
"include": ["./src"],
"exclude": ["**/__mocks__/*", "**/__tests__/*"]
}