Format with prettier and add .prettierrc

This commit is contained in:
Joel Arvidsson 2018-02-25 16:14:17 +01:00
parent 172368f2fd
commit a751670b92
3 changed files with 101 additions and 50 deletions

5
.prettierrc Normal file
View File

@ -0,0 +1,5 @@
{
"printWidth": 80,
"singleQuote": true,
"trailingComma": "es5",
}

View File

@ -18,38 +18,43 @@ export default class KeychainExample extends Component {
}; };
save() { save() {
Keychain Keychain.setSecurePassword(
.setSecurePassword('myService', this.state.username, this.state.password) 'myService',
this.state.username,
this.state.password
)
.then(() => { .then(() => {
this.setState({ status: 'Credentials saved!' }); this.setState({ status: 'Credentials saved!' });
}) })
.catch((err) => { .catch(err => {
this.setState({ status: 'Could not save credentials, ' + err }); this.setState({ status: 'Could not save credentials, ' + err });
}); });
} }
load() { load() {
Keychain Keychain.getSecurePassword('myService')
.getSecurePassword('myService') .then(credentials => {
.then((credentials) => {
if (credentials) { if (credentials) {
this.setState({ ...credentials, status: 'Credentials loaded!' }); this.setState({ ...credentials, status: 'Credentials loaded!' });
} else { } else {
this.setState({ status: 'No credentials stored.' }); this.setState({ status: 'No credentials stored.' });
} }
}) })
.catch((err) => { .catch(err => {
this.setState({ status: 'Could not load credentials. ' + err }); this.setState({ status: 'Could not load credentials. ' + err });
}); });
} }
reset() { reset() {
Keychain Keychain.resetGenericPassword()
.resetGenericPassword()
.then(() => { .then(() => {
this.setState({ status: 'Credentials Reset!', username: '', password: '' }); this.setState({
status: 'Credentials Reset!',
username: '',
password: '',
});
}) })
.catch((err) => { .catch(err => {
this.setState({ status: 'Could not reset credentials, ' + err }); this.setState({ status: 'Could not reset credentials, ' + err });
}); });
} }
@ -58,9 +63,7 @@ export default class KeychainExample extends Component {
return ( return (
<KeyboardAvoidingView behavior="padding" style={styles.container}> <KeyboardAvoidingView behavior="padding" style={styles.container}>
<View style={styles.content}> <View style={styles.content}>
<Text style={styles.title}> <Text style={styles.title}>Keychain Example</Text>
Keychain Example
</Text>
<View style={styles.field}> <View style={styles.field}>
<Text style={styles.label}>Username</Text> <Text style={styles.label}>Username</Text>
<TextInput <TextInput
@ -68,8 +71,9 @@ export default class KeychainExample extends Component {
autoFocus={true} autoFocus={true}
autoCapitalize="none" autoCapitalize="none"
value={this.state.username} value={this.state.username}
onChange={(event) => this.setState({ username: event.nativeEvent.text })} onChange={event =>
/> this.setState({ username: event.nativeEvent.text })}
/>
</View> </View>
<View style={styles.field}> <View style={styles.field}>
<Text style={styles.label}>Password</Text> <Text style={styles.label}>Password</Text>
@ -78,22 +82,34 @@ export default class KeychainExample extends Component {
password={true} password={true}
autoCapitalize="none" autoCapitalize="none"
value={this.state.password} value={this.state.password}
onChange={(event) => this.setState({ password: event.nativeEvent.text })} onChange={event =>
/> this.setState({ password: event.nativeEvent.text })}
/>
</View> </View>
{!!this.state.status && (<Text style={styles.status}>{this.state.status}</Text>)} {!!this.state.status && (
<Text style={styles.status}>{this.state.status}</Text>
)}
<View style={styles.buttons}> <View style={styles.buttons}>
<TouchableHighlight onPress={() => this.save()} style={styles.button}> <TouchableHighlight
onPress={() => this.save()}
style={styles.button}
>
<View style={styles.save}> <View style={styles.save}>
<Text style={styles.buttonText}>Save</Text> <Text style={styles.buttonText}>Save</Text>
</View> </View>
</TouchableHighlight> </TouchableHighlight>
<TouchableHighlight onPress={() => this.load()} style={styles.button}> <TouchableHighlight
onPress={() => this.load()}
style={styles.button}
>
<View style={styles.load}> <View style={styles.load}>
<Text style={styles.buttonText}>Load</Text> <Text style={styles.buttonText}>Load</Text>
</View> </View>
</TouchableHighlight> </TouchableHighlight>
<TouchableHighlight onPress={() => this.reset()} style={styles.button}> <TouchableHighlight
onPress={() => this.reset()}
style={styles.button}
>
<View style={styles.reset}> <View style={styles.reset}>
<Text style={styles.buttonText}>Reset</Text> <Text style={styles.buttonText}>Reset</Text>
</View> </View>
@ -110,7 +126,7 @@ const styles = StyleSheet.create({
flex: 1, flex: 1,
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
backgroundColor: '#F5FCFF' backgroundColor: '#F5FCFF',
}, },
content: { content: {
width: 250, width: 250,
@ -165,5 +181,5 @@ const styles = StyleSheet.create({
fontSize: 14, fontSize: 14,
paddingHorizontal: 16, paddingHorizontal: 16,
paddingVertical: 8, paddingVertical: 8,
} },
}); });

View File

@ -8,12 +8,12 @@ type SecAccessible =
| 'AccessibleWhenPasscodeSetThisDeviceOnly' | 'AccessibleWhenPasscodeSetThisDeviceOnly'
| 'AccessibleWhenUnlockedThisDeviceOnly' | 'AccessibleWhenUnlockedThisDeviceOnly'
| 'AccessibleAfterFirstUnlockThisDeviceOnly' | 'AccessibleAfterFirstUnlockThisDeviceOnly'
| 'AccessibleAlwaysThisDeviceOnly' | 'AccessibleAlwaysThisDeviceOnly';
type Options = { type Options = {
accessible?: SecAccessible; accessible?: SecAccessible,
accessGroup?: string; accessGroup?: string,
service?: string; service?: string,
}; };
type SecAccessControl = type SecAccessControl =
@ -22,16 +22,14 @@ type SecAccessControl =
| 'TouchIDCurrentSet' | 'TouchIDCurrentSet'
| 'DevicePasscode' | 'DevicePasscode'
| 'TouchIDAnyOrDevicePasscode' | 'TouchIDAnyOrDevicePasscode'
| 'TouchIDCurrentSetOrDevicePasscode' | 'TouchIDCurrentSetOrDevicePasscode';
type LAPolicy = type LAPolicy = 'Authentication' | 'AuthenticationWithBiometrics';
| 'Authentication'
| 'AuthenticationWithBiometrics'
type SecureOptions = { type SecureOptions = {
customPrompt?: string; customPrompt?: string,
authenticationType?: LAPolicy; authenticationType?: LAPolicy,
accessControl?: SecAccessControl; accessControl?: SecAccessControl,
}; };
/** /**
@ -40,9 +38,7 @@ type SecureOptions = {
* @param {object} options LAPolicy option, iOS only * @param {object} options LAPolicy option, iOS only
* @return {Promise} Resolves to `true` when successful * @return {Promise} Resolves to `true` when successful
*/ */
export function canImplyAuthentication( export function canImplyAuthentication(options?: SecureOptions): Promise {
options?: SecureOptions
): Promise {
return RNKeychainManager.canCheckAuthentication(options); return RNKeychainManager.canCheckAuthentication(options);
} }
@ -60,7 +56,12 @@ export function setSecurePassword(
password: string, password: string,
options?: SecureOptions options?: SecureOptions
): Promise { ): Promise {
return RNKeychainManager.setSecurePasswordForService(service, username, password, options); return RNKeychainManager.setSecurePasswordForService(
service,
username,
password,
options
);
} }
/** /**
@ -89,7 +90,12 @@ export function setInternetCredentials(
password: string, password: string,
options?: Options options?: Options
): Promise { ): Promise {
return RNKeychainManager.setInternetCredentialsForServer(server, username, password, options); return RNKeychainManager.setInternetCredentialsForServer(
server,
username,
password,
options
);
} }
/** /**
@ -120,9 +126,13 @@ export function resetInternetCredentials(
function getOptionsArgument(serviceOrOptions?: string | KeychainOptions) { function getOptionsArgument(serviceOrOptions?: string | KeychainOptions) {
if (Platform.OS !== 'ios') { if (Platform.OS !== 'ios') {
return typeof serviceOrOptions === 'object' ? serviceOrOptions.service : serviceOrOptions; return typeof serviceOrOptions === 'object'
? serviceOrOptions.service
: serviceOrOptions;
} }
return typeof serviceOrOptions === 'string' ? { service: serviceOrOptions } : serviceOrOptions; return typeof serviceOrOptions === 'string'
? { service: serviceOrOptions }
: serviceOrOptions;
} }
/** /**
@ -137,7 +147,11 @@ export function setGenericPassword(
password: string, password: string,
serviceOrOptions?: string | KeychainOptions serviceOrOptions?: string | KeychainOptions
): Promise { ): Promise {
return RNKeychainManager.setGenericPasswordForOptions(getOptionsArgument(serviceOrOptions), username, password); return RNKeychainManager.setGenericPasswordForOptions(
getOptionsArgument(serviceOrOptions),
username,
password
);
} }
/** /**
@ -148,7 +162,9 @@ export function setGenericPassword(
export function getGenericPassword( export function getGenericPassword(
serviceOrOptions?: string | KeychainOptions serviceOrOptions?: string | KeychainOptions
): Promise { ): Promise {
return RNKeychainManager.getGenericPasswordForOptions(getOptionsArgument(serviceOrOptions)); return RNKeychainManager.getGenericPasswordForOptions(
getOptionsArgument(serviceOrOptions)
);
} }
/** /**
@ -159,7 +175,9 @@ export function getGenericPassword(
export function resetGenericPassword( export function resetGenericPassword(
serviceOrOptions?: string | KeychainOptions serviceOrOptions?: string | KeychainOptions
): Promise { ): Promise {
return RNKeychainManager.resetGenericPasswordForOptions(getOptionsArgument(serviceOrOptions)); return RNKeychainManager.resetGenericPasswordForOptions(
getOptionsArgument(serviceOrOptions)
);
} }
/** /**
@ -167,9 +185,13 @@ export function resetGenericPassword(
* @return {Promise} Resolves to `{ server, username, password }` if approved and * @return {Promise} Resolves to `{ server, username, password }` if approved and
* `false` if denied and throws an error if not supported on platform or there's no shared credentials * `false` if denied and throws an error if not supported on platform or there's no shared credentials
*/ */
export function requestSharedWebCredentials() : Promise { export function requestSharedWebCredentials(): Promise {
if (Platform.OS !== 'ios') { if (Platform.OS !== 'ios') {
return Promise.reject(new Error(`requestSharedWebCredentials() is not supported on ${Platform.OS} yet`)); return Promise.reject(
new Error(
`requestSharedWebCredentials() is not supported on ${Platform.OS} yet`
)
);
} }
return RNKeychainManager.requestSharedWebCredentials(); return RNKeychainManager.requestSharedWebCredentials();
} }
@ -185,9 +207,17 @@ export function setSharedWebCredentials(
server: string, server: string,
username: string, username: string,
password: string password: string
) : Promise { ): Promise {
if (Platform.OS !== 'ios') { if (Platform.OS !== 'ios') {
return Promise.reject(new Error(`setSharedWebCredentials() is not supported on ${Platform.OS} yet`)); return Promise.reject(
new Error(
`setSharedWebCredentials() is not supported on ${Platform.OS} yet`
)
);
} }
return RNKeychainManager.setSharedWebCredentialsForServer(server, username, password); return RNKeychainManager.setSharedWebCredentialsForServer(
server,
username,
password
);
} }