Skip to content

Commit

Permalink
feat: oauth authorize
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitarcitano committed Jun 28, 2021
1 parent 8248358 commit e58bb10
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@
},
"devDependencies": {
"@commitlint/config-conventional": "^11.0.0",
"@react-native-async-storage/async-storage": "^1.15.5",
"@react-native-community/eslint-config": "^2.0.0",
"@release-it/conventional-changelog": "^2.0.0",
"@types/jest": "^26.0.0",
"@types/react": "^16.9.19",
"@types/react-native": "0.62.13",
"axios": "^0.21.1",
"commitlint": "^11.0.0",
"eslint": "^7.2.0",
"eslint-config-prettier": "^7.0.0",
Expand All @@ -65,9 +67,7 @@
"react-native": "0.63.4",
"react-native-builder-bob": "^0.18.0",
"release-it": "^14.2.2",
"typescript": "^4.1.3",
"@react-native-async-storage/async-storage": "^1.15.5",
"axios": "^0.21.1"
"typescript": "^4.1.3"
},
"peerDependencies": {
"react": "*",
Expand Down Expand Up @@ -151,5 +151,8 @@
}
]
]
},
"dependencies": {
"react-native-app-auth": "^6.4.0"
}
}
31 changes: 31 additions & 0 deletions src/auth/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { Button, View } from 'react-native';
import type { LoginProps } from './interfaces';
import { startOAuthFlow } from './service';

export function Login({
issuer,
clientId,
redirectUrl,
scopes,
additionalParameters,
}: LoginProps): JSX.Element {
async function handleLoginPress() {
const oAuthResponse = await startOAuthFlow({
issuer,
clientId,
redirectUrl,
scopes,
additionalParameters,
});

console.log(oAuthResponse);
}

return (
// eslint-disable-next-line react-native/no-inline-styles
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button title="Login" onPress={handleLoginPress} />
</View>
);
}
11 changes: 11 additions & 0 deletions src/auth/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { BuiltInParameters } from 'react-native-app-auth';

export interface OAuthConfig {
issuer: string;
clientId: string;
redirectUrl: string;
scopes: string[];
additionalParameters: BuiltInParameters & { [name: string]: string };
}

export type LoginProps = OAuthConfig;
22 changes: 22 additions & 0 deletions src/auth/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { authorize } from 'react-native-app-auth';
import type { OAuthConfig } from './interfaces';

export async function startOAuthFlow({
issuer,
clientId,
redirectUrl,
scopes,
additionalParameters,
}: OAuthConfig) {
try {
return await authorize({
issuer,
clientId,
redirectUrl,
scopes,
additionalParameters,
});
} catch (err) {
throw err;
}
}
3 changes: 2 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { apiClient } from './clients/api';
import { setStorageItem, getStorageItem } from './storage';
import { Login } from './auth';

export { getStorageItem, setStorageItem, apiClient };
export { getStorageItem, setStorageItem, apiClient, Login };
15 changes: 14 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5132,7 +5132,7 @@ interpret@^1.0.0:
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.4.0.tgz#665ab8bc4da27a774a40584e812e3e0fa45b1a1e"
integrity sha512-agE4QfB2Lkp9uICn7BAqoscw4SZP9kTE2hxiFI3jBPmXJfdqiahTbUuKGsMoN2GtqL9AxhYioAcVvgsb1HvRbA==

invariant@^2.2.4:
invariant@2.2.4, invariant@^2.2.4:
version "2.2.4"
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6"
integrity sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA==
Expand Down Expand Up @@ -7795,6 +7795,19 @@ react-is@^17.0.1:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==

react-native-app-auth@^6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/react-native-app-auth/-/react-native-app-auth-6.4.0.tgz#a392a9370983954a49c079515e3116e6699ab868"
integrity sha512-m9DOM/Bgplrk6gD+sqshsbdG9XB4eSaA4rZXuPnwTXYTcHxCy0KvJ3jRDLKDY1im78lXNnifGeHicv7chGAwQw==
dependencies:
invariant "2.2.4"
react-native-base64 "0.0.2"

[email protected]:
version "0.0.2"
resolved "https://registry.yarnpkg.com/react-native-base64/-/react-native-base64-0.0.2.tgz#c28463c2c6779ac3ec5fdd12979ebc8c5f9b410a"
integrity sha512-Fu/J1a2y0X22EJDWqJR2oEa1fpP4gTFjYxk8ElJdt1Yak3HOXmFJ7EohLVHU2DaQkgmKfw8qb7u/48gpzveRbg==

react-native-builder-bob@^0.18.0:
version "0.18.1"
resolved "https://registry.yarnpkg.com/react-native-builder-bob/-/react-native-builder-bob-0.18.1.tgz#43f238e6cb6efffa6e3d23ac449eb7392650c2c3"
Expand Down

0 comments on commit e58bb10

Please sign in to comment.