Wrap code into if supported check

This commit is contained in:
dominik-zeglen 2019-09-11 16:04:41 +02:00
parent 6d6312248d
commit 4e2ae6ad2b
2 changed files with 18 additions and 18 deletions

View file

@ -105,11 +105,9 @@ class AuthProvider extends React.Component<
if (!!token && !user) { if (!!token && !user) {
this.verifyToken(token); this.verifyToken(token);
} else { } else {
if (isCredentialsManagementAPISupported) {
loginWithCredentialsManagementAPI(this.login); loginWithCredentialsManagementAPI(this.login);
} }
} }
}
login = async (email: string, password: string) => { login = async (email: string, password: string) => {
const { tokenAuth } = this.props; const { tokenAuth } = this.props;
@ -117,10 +115,8 @@ class AuthProvider extends React.Component<
tokenAuthFn({ variables: { email, password } }).then(result => { tokenAuthFn({ variables: { email, password } }).then(result => {
if (result && !result.data.tokenCreate.errors.length) { if (result && !result.data.tokenCreate.errors.length) {
if (isCredentialsManagementAPISupported) {
saveCredentials(result.data.tokenCreate.user, password); saveCredentials(result.data.tokenCreate.user, password);
} }
}
}); });
}; };

View file

@ -4,14 +4,17 @@ export const isSupported =
navigator.credentials && navigator.credentials.preventSilentAccess; navigator.credentials && navigator.credentials.preventSilentAccess;
export function login(loginFn: (id: string, password: string) => void) { export function login(loginFn: (id: string, password: string) => void) {
if (isSupported) {
navigator.credentials.get({ password: true }).then(credential => { navigator.credentials.get({ password: true }).then(credential => {
if (credential instanceof PasswordCredential) { if (credential instanceof PasswordCredential) {
loginFn(credential.id, credential.password); loginFn(credential.id, credential.password);
} }
}); });
} }
}
export function saveCredentials(user: User, password: string) { export function saveCredentials(user: User, password: string) {
if (isSupported) {
const cred = new PasswordCredential({ const cred = new PasswordCredential({
iconURL: user.avatar ? user.avatar.url : undefined, iconURL: user.avatar ? user.avatar.url : undefined,
id: user.email, id: user.email,
@ -20,3 +23,4 @@ export function saveCredentials(user: User, password: string) {
}); });
navigator.credentials.store(cred); navigator.credentials.store(cred);
} }
}